-
Notifications
You must be signed in to change notification settings - Fork 86
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
JSON.stringify doesn't preserve object equivalence #22
Comments
This cannot happen if your args are only primitive values. memoized('foo', 3, 'bar') Your order is guaranteed when you contruct the object manually. c = "bar"
a = { foo: c }
b = 20
memoized({ key1: a, key2: b }) but yes if you pass a random generated object the order is not guaranteed. |
@StarpTech any source on "order being guaranteed" for Objects? AFAIK they may explicitly be random. |
@dodekeract ES6 defines the order in which the keys of an object are enumerated: http://www.2ality.com/2015/10/property-traversal-order-es6.html |
I'd love to see a source for that statement (@rauschma?). The spec itself says otherwise:
Either way, it doesn't resolve this issue:
|
The problem is that the traversal order discussed in that article is not for all object traversals period. There is no universal order expectation for how objects will display, however a majority of engines today will show them in the order in which they were added. This is not guaranteed by any means, but pretty much all mainstream browsers follow that paradigm for simplicity and performance reasons. More details => http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order/38218582#38218582 |
@chocolateboy ES6 spec defines JSON.stringify (independent of the RFC) to use the same enumeration sequence, making the order guaranteed: You'd need to use a custom serializer to make |
@planttheidea That Stack Overflow link is talking about ES2015. ES2016 clarified the ordering for all cases to be the same. |
Add how many browsers have actually implemented the spec? There is a practicality piece in here, as implementing a custom serializer instead of leveraging JSON.stringify as the default will cause a fairly substantial performance decrease. Naturally you can do it for your specific application, but this to call the native browser implementation an issue may be too much. |
@NeoPhi Interesting. Thanks for the link!
Well, yes. While this excursion is interesting, it doesn't address this issue (other than confirming it). |
https://www.npmjs.com/package/canonicalize this fixes this problem. |
https://gist.github.com/chocolateboy/254b44382e6c34f8ce7e
The text was updated successfully, but these errors were encountered: