diff --git a/index.js b/index.js index fa364c3..69381c5 100644 --- a/index.js +++ b/index.js @@ -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)) { 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) { return str; } diff --git a/test/unit/serialize.js b/test/unit/serialize.js index c694a65..14011b0 100644 --- a/test/unit/serialize.js +++ b/test/unit/serialize.js @@ -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)); }); it('should deserialize a JSON string to a JSON object', function () {