Skip to content
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

Improve perf via isJSON option #17

Merged
merged 3 commits into from
May 31, 2016
Merged

Improve perf via isJSON option #17

merged 3 commits into from
May 31, 2016

Conversation

ericf
Copy link
Collaborator

@ericf ericf commented May 25, 2016

This adds some micro-optimizations along with a new macro optimization: {isJSON: true} option.

Passing a replacer function to JSON.stringify() slows it down dramatically. If possible, we should avoid this this path, and the new isJSON option is the signal that the object passed-in contains no functions or regexp values. But still protect against XSS by properly escaping for a <script> context.

Node v0.12.10
simpleObj:
JSON.stringify( simpleObj ) x 1,303,349 ops/sec ±0.66% (99 runs sampled)
JSON.stringify( simpleObj ) with replacer x 386,634 ops/sec ±0.72% (96 runs sampled)
serialize( simpleObj ) x 284,535 ops/sec ±0.92% (98 runs sampled)
serialize( simpleObj, {isJSON: true} ) x 951,798 ops/sec ±0.92% (96 runs sampled)

ericf added 2 commits May 25, 2016 19:06
This adds some micro-optimizations along with a new macro
optimization: `{isJSON: true}` option.

Passing a `replacer` function to `JSON.stringify()` slows it down
dramatically. If possible, we should avoid this this path, and the
new `isJSON` option is the signal that the object passed-in contains
no functions or regexp values.

Node v0.12.10
simpleObj:
JSON.stringify( simpleObj ) x 1,303,349 ops/sec ±0.66% (99 runs sampled)
JSON.stringify( simpleObj ) with replacer x 386,634 ops/sec ±0.72% (96 runs sampled)
serialize( simpleObj ) x 284,535 ops/sec ±0.92% (98 runs sampled)
**serialize( simpleObj, {isJSON: true} ) x 951,798 ops/sec ±0.92% (96 runs sampled)**
@yahoocla
Copy link

CLA is valid!

@ericf ericf mentioned this pull request May 25, 2016
@ericf
Copy link
Collaborator Author

ericf commented May 25, 2016

Might want to re-think the name of the option given #16 would require the replacer function.

@redonkulus
Copy link
Collaborator

👍

ericf added a commit to YahooArchive/express-state that referenced this pull request May 26, 2016
This adds support for the forthcoming `isJSON` option being added to
`serialize-javascript` to help speed up serialization for pure JSON
data.

yahoo/serialize-javascript#17
@ericf ericf merged commit 2d5c259 into master May 31, 2016
@ericf ericf deleted the perf branch May 31, 2016 21:49
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