Skip to content

Conversation

anonrig
Copy link
Contributor

@anonrig anonrig commented Oct 10, 2025

Replacing delete X.y with X.y = undefined avoids killing any optimizations done by V8 to access the properties of an object faster.

Subset of #1002

Copy link

changeset-bot bot commented Oct 10, 2025

🦋 Changeset detected

Latest commit: e86d4c6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@opennextjs/aws Patch
app-pages-router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

pkg-pr-new bot commented Oct 10, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/aws@1007

commit: e86d4c6

// Next.js doesn't parse body if the property exists
// https://github.com/dougmoscrop/serverless-http/issues/227
delete req.body;
req.body = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment above explicitely says "the property exists"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth an upstream pr to next.js tho I suspect it's been tried before.

The key challenge with using delete here is that it immediately puts req into a slower / non-optimized mode for all property accesses. Whether or not that moves the needle or not in performance depends on usage pattern. In this particular case, let's leave this as delete req.body and see if we can try to fix upstream.

but as a general rule, we should be avoiding delete like the plague.

continue;
}
const keyLower = key.toLowerCase();
if (keyLower === "set-cookie") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that a change in behavior?

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's take more time to evaluate the changes in this PR (see my inline comments).

It should not have a huge impact anyway

// We need to remove the set-cookie header from the parsed headers because
// it does not handle multiple set-cookie headers properly
delete parsedHeaders[SET_COOKIE_HEADER];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this is removed but not replaced?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I see, you're removing it in the util.ts change instead.

if (keyLower === "set-cookie") {
// We need to remove the set-cookie header from the parsed headers because
// it does not handle multiple set-cookie headers properly
continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

side note... I keep thinking that it would be nice if Object.entries(...) (and the iterable pattern in general) would accept a filter function...

Object.entries(headers, filter);

...that could optimize this pattern more and make it so the collection only had to be iterated through once... That's for another time tho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants