-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decouple canonicalStringify
from ObjectCanon
#11254
Decouple canonicalStringify
from ObjectCanon
#11254
Conversation
🦋 Changeset detectedLatest commit: d0aa030 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
size-limit report 📦
|
/release:pr |
A new release has been made for this PR. You can install it with |
// The .slice(0) is necessary so that we do not modify the original keys array | ||
// by calling keys.sort(), and also so that we always return a new (!==) | ||
// sorted array when keys was not already sorted. | ||
return node.sorted || (node.sorted = keys.slice(0).sort()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fun TC39/ECMAScript fact: thanks to this proposal which recently reached stage 4, we will eventually be able to use keys.toSorted()
instead of keys.slice(0).sort()
. "Eventually" because even evergreen browsers take some time to ship new features.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least it's one of those things that can easily be polyfilled in userland, so we can make that switch in two years or so :)
4f3d137
to
9211fd4
Compare
Co-authored-by: Lenz Weber-Tronic <[email protected]>
Co-authored-by: Lenz Weber-Tronic <[email protected]>
The lookupSortedKeys function is not intended to be used directly, but seemed worth unit testing nevertheless.
d7d190f
to
1b4aad3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is gonna have a big impact, especially in SSR scenarios.
Thanks for taking the time to go over this!
cfb5b42
to
d0aa030
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great to me! Nothing to add that @phryneas hasn't already. 🎉
Then let's get this into the 3.9 branch so we can have a few people try it out soon :) |
Because the
ObjectCanon
happens to produce canonical objects whose keys have been sorted alphabetically/lexicographically, it seemed convenient and efficient for implementing a stableJSON.stringify
utility (see PR #8222), but the additional memory overhead of fully canonizing (duplicating) the objects was probably not justified, especially since (as this PR suggests) an independent implementation ofcanonicalStringify
can be even faster while using much less memory.