-
Notifications
You must be signed in to change notification settings - Fork 184
Add space
to JSON.stringify() for Node 0.10 issue
#4
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ See the accompanying LICENSE file for terms. | |
|
||
'use strict'; | ||
|
||
var util = require('util'); | ||
var isRegExp = require('util').isRegExp; | ||
|
||
module.exports = serialize; | ||
|
||
|
@@ -24,6 +24,13 @@ var UNICODE_CHARS = { | |
'\u2029': '\\u2029' | ||
}; | ||
|
||
// There's an issue with Node 0.10 (V8 3.14.5.9) which causes `JSON.stringify()` | ||
// and the subsequent `str.replace()` call to take over 100x more time than when | ||
// a the `JSON.stringify()` replacer function is used and the data being | ||
// serialized is very large (500KB). A remedy to this is setting the | ||
// `JSON.stringify()` `space` option to a truthy value. | ||
var SPACE = 2; | ||
|
||
function serialize(obj) { | ||
var functions = [], | ||
regexps = [], | ||
|
@@ -37,12 +44,12 @@ function serialize(obj) { | |
return '@__FUNCTION_' + (functions.push(value) - 1) + '__@'; | ||
} | ||
|
||
if (util.isRegExp(value)) { | ||
if (typeof value === 'object' && isRegExp(value)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type-check before function call. |
||
return '@__REGEXP_' + (regexps.push(value) - 1) + '__@'; | ||
} | ||
|
||
return value; | ||
}); | ||
}, SPACE); | ||
|
||
// Protects against `JSON.stringify()` returning `undefined`, by serializing | ||
// to the literal string: "undefined". | ||
|
@@ -57,7 +64,7 @@ function serialize(obj) { | |
return UNICODE_CHARS[unsafeChar]; | ||
}); | ||
|
||
if (!(functions.length || regexps.length)) { | ||
if (functions.length === 0 && regexps.length === 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For clarity. |
||
return str; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ describe('serialize( obj )', function () { | |
}); | ||
|
||
it('should serialize JSON to a JSON string', function () { | ||
expect(serialize(data)).to.equal(JSON.stringify(data)); | ||
expect(serialize(data)).to.equal(JSON.stringify(data, null, 2)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Required now that we have spaces in the output. |
||
}); | ||
|
||
it('should deserialize a JSON string to a JSON object', function () { | ||
|
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.
Optimize away the property lookup in the replacer.