-
Notifications
You must be signed in to change notification settings - Fork 603
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
Globalize-runtime has unsafe runtime-key hash function #705
Comments
#704 is related and may have the same solution |
I ran into this issue as well a few months ago. According to this, the order should be stable as long as you create the object in the same way: http://2ality.com/2015/10/property-traversal-order-es6.html However, it might be a good idea to use json-stable-stringify to avoid mistakes like this and to avoid duplicating runtime formatters. There's also another issue. These two are identical, but they produce two different hashes:
because runtimeBind gets the original arguments as-is, e.g. here: https://github.com/globalizejs/globalize/blob/1.2.3/src/number.js#L75 I think the argument hashing happens here during compilation: |
If someone works on a fix, we could include it in the upcoming
This can be solved similarly to dateFormatter https://github.com/globalizejs/globalize/blob/master/src/date-runtime.js#L38. In other words, the default options for numberFormatter needs to be fixed from |
json-stable-stringify There's also fast-stable-stringify: https://www.npmjs.com/package/fast-stable-stringify, which is faster, but it doesn't support replacer, which I used for the fix for #704, and it doesn't check circular references or toJSON methods (I don't know if those are needed). |
I ran a benchmark on node: Finished benchmarking: nickyout/fast-stable-stringify x 15,634 ops/sec ±0.75% (94 runs sampled) (cumulative string length: 221965828) |
I wrote a new benchmark to compare various options: https://github.com/nkovacs/stringify-benchmarks Node.js:
Google Chrome:
|
Thanks @nkovacs what about file sizes? I mean how much byes would we add to the library by using them? Thanks |
This compresses down to 995 bytes with uglify.js.
Or you could just write them out, but this would be a maintenance burden:
|
The runtime key is generated from a
JSON.stringify
of the arguments (see here.Problem is that
JSON.stringify
does not guarantee an order that the properties are stringified. See here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringifyThis already bit me once where formatters were called with the exact same properties but were stringified differently. For example:
By making sure the properties are specified in the same order, it appears to work in the current browsers, but this is a bandaid because
JSON.stringify
is defined as unstable.The solution is to use a stable hash of the arguments. Here's a fairly small, stable stringify: https://github.com/substack/json-stable-stringify. Or you can non-stringify hash.
The text was updated successfully, but these errors were encountered: