Skip to content

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

Merged
merged 1 commit into from
Aug 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ See the accompanying LICENSE file for terms.

'use strict';

var util = require('util');
var isRegExp = require('util').isRegExp;
Copy link
Collaborator Author

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.


module.exports = serialize;

Expand All @@ -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 = [],
Expand All @@ -37,12 +44,12 @@ function serialize(obj) {
return '@__FUNCTION_' + (functions.push(value) - 1) + '__@';
}

if (util.isRegExp(value)) {
if (typeof value === 'object' && isRegExp(value)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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".
Expand All @@ -57,7 +64,7 @@ function serialize(obj) {
return UNICODE_CHARS[unsafeChar];
});

if (!(functions.length || regexps.length)) {
if (functions.length === 0 && regexps.length === 0) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity.

return str;
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 () {
Expand Down