Skip to content

Tokenize the space value when stringify-ing #7

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

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ See the accompanying LICENSE file for terms.
var isRegExp = require('util').isRegExp;

// Generate an internal UID to make the regexp pattern harder to guess.
var UID = Math.floor(Math.random() * 0x10000000000).toString(16);
var UID = Math.floor(Math.random() * 0x10000000000).toString(36);
Copy link
Member

Choose a reason for hiding this comment

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

wat

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the max radix and the largest magnitude random number that produces an 8 char string. I need 8 chars so I can wrap it with @ special char to end up with a 10 char string which is the max length for space param to JSON.stringify().

var PLACE_HOLDER_REGEXP = new RegExp('"@__(FUNCTION|REGEXP)-' + UID + '-(\\d+)__@"', 'g');

var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g;
Expand All @@ -29,8 +29,10 @@ var UNICODE_CHARS = {
// 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;
// `JSON.stringify()` `space` option to a truthy value which is a token that is
// later removed to keep the file size down.
var SPACE = '@' + UID + '@';
var SPACE_REGEXP = new RegExp('\\n(@' + UID + '@)+', 'g');

module.exports = function serialize(obj) {
var functions = [];
Expand Down Expand Up @@ -58,6 +60,8 @@ module.exports = function serialize(obj) {
return String(str);
}

str = str.replace(SPACE_REGEXP, '');

// Replace unsafe HTML and invalid JavaScript line terminator chars with
// their safe Unicode char counterpart. This _must_ happen before the
// regexps and functions are serialized and added back to the string.
Expand Down
6 changes: 5 additions & 1 deletion test/unit/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ describe('serialize( obj )', function () {
});

it('should serialize JSON to a JSON string', function () {
expect(serialize(data)).to.equal(JSON.stringify(data, null, 2));
var serialized = serialize(data);
var stringified = JSON.stringify(data);

expect(serialized).to.be.a('string');
expect(JSON.parse(serialized)).to.deep.equal(JSON.parse(stringified));
});

it('should deserialize a JSON string to a JSON object', function () {
Expand Down