-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Update JSON response stringify for better performance #3014
Conversation
Interesting. I don't mind the optimization but if should be implemented using an if statement, not apply. That should also fix the exception your code is throwing. |
Thanks, it's done. |
I've made a jsPerf for this finding: I think it's a v8 bug, opened issue here. |
It is confirmed by the v8 team that this is the expected behavior. |
Update JSON response stringify for better performance
Most libraries use `JSON.stringify` with all three arguments [1] to allow for configuration, even if `replacer` and `space` are falsey, causing the optimized native stringifying to be missed. This commit allows for the common case where `replacer` and `space` are not used to be fast. [1]: hapijs/hapi#3014 BUG=v8:4730 LOG=N R=yangguo@chromium.org Review URL: https://codereview.chromium.org/1710933002 Cr-Commit-Position: refs/heads/master@{#34174}
Passing here by change: you should use http://npm.im/fast-safe-stringify by @davidmarkclements. It "fixes" circular references and it is incredibly fast. |
Just to clarify, it's not as fast as using JSON.stringify as-is, but as far as I know it's the fastest "safe stringifer" - particularly for large objects fast-safe-stringify is 2x faster for small objects, and 6x faster for large objects than json-stringify-safe If you have no concerns around circular references then to my knowledge, directly using JSON.stringify is as fast as you can get atm. But yeah I noticed the same degradation with additional params, which led to the technique used in fast-safe-stringify |
Also there is a try catch, which is not needed with fast-safe-stringify
|
@mcollina That's actually a pretty good point– the try/catch deoptimizes that internal |
Thanks but not going to add external deps. |
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions. |
Hi, I've been doing some profiling to a REST API I've been working on and noticed that, for large JSON responses, a lot of time was being spent stringifying the actual responses. After some digging, I found out that the performance of the native V8 JSON.stringify degrades very quickly when the method is called with more than 1 argument (the Javascript object to stringify itself), even if the other arguments are null or undefined.
I've done some simple but yet conclusive tests with JSON.stringify and got some results to share:
Hapi is currently stringifying all JSON response objects calling JSON.stringify always with all the 3 arguments, even if the last 2 are null. I updated the JSON.stringify call in Hapi to use this last 2 arguments only when they have non falsy values.