Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fast-stable-stringify

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-stable-stringify

Deterministic stringification for when performance matters

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
61K
decreased by-80.78%
Maintainers
1
Weekly downloads
 
Created
Source

fast-stable-stringify

Build Status Sauce Test Status

The test only succeeds when mine is faster than substack's in a particular browser.

The most popular repository providing this feature is substack's json-stable-stringify. The intent if this library is to provide a faster alternative for when performance is more important than features. It assumes you provide basic javascript values without circular references, and returns a non-indented string. It currently offers a performance boost in popular browsers of about 50%. Less so for modern desktop browsers (30%), more so for modern (mobile) safari (100%). For most legacy browsers, about 75%.

Usage:

var stringify = require('fast-stable-stringify');
stringify({ d: 0, c: 1, a: 2, b: 3, e: 4 }); // '{"a":2,"b":3,"c":1,"d":0,"e":4}'

Just like substack's, it does:

  • handle all variations of all basic javascript values (number, string, boolean, array, object, null)
  • handle undefined in the same way as JSON.stringify
  • work without native access to JSON.stringify
  • not support ie8 (and below) with complete certainty. At least, his build failed on ie8.

Unlike substack's, it does:

  • not implement the 'replacer' or 'space' arguments of the JSON.stringify method
  • not check for circular references
  • not check for .toJSON() methods on objects

Upgrade warning (0.1.1 to 0.2.0):

Version 0.1.1 would sometimes return invalid JSON strings when given object properties with the value undefined. The response was still consistent and unique given the other properties, so I suspect in most cases this should have caused no problems. However, if your own project is storing the results of this library (0.1.1), do note that the results will be slightly different starting with 0.2.0.

Test results

Tested validity (answer equal to substack's) and benchmark (faster than substack's). A test passes only if it has the same output as substack's but is faster (as concluded by benchmark.js).

To (hopefully) prevent certain smart browsers from concluding the stringification is not necessary because it is never used anywhere, I summed up all the lengths of the resulting strings of each benchmark contestant and printed it along with the result data.

Latest (interpreted) result

Benchmark commit 14ad70c5e1nickyout/fast-stable-stringifysubstack/json-stable-stringifylast time*fastest*
safari 5 on Windows 2008x 4,317 ops/secx 1,534 ops/sec+162%+181%
safari 10 on Mac 10.12x 6,830 ops/secx 3,430 ops/sec+99%
opera 12 on Windows 2003x 3,084 ops/secx 1,938 ops/sec+66%+59%
microsoftedge 14 on Windowsx 5,717 ops/secx 4,513 ops/sec+26%
iphone 9.2 on Mac 10.10x 6,643 ops/secx 3,829 ops/sec+73%
ipad 9.2 on Mac 10.10x 8,563 ops/secx 3,374 ops/sec+153%
internet explorer 9 on Windx 6,236 ops/secx 2,954 ops/sec+97%+111%
internet explorer 11 on Winx 5,236 ops/secx 3,729 ops/sec+34%+40%
internet explorer 10 on Winx 7,520 ops/secx 3,435 ops/sec+119%+118%
firefox 20 on Windows 10x 4,263 ops/secx 2,038 ops/sec+108%+109%
chrome 57 on Windows 2008x 9,116 ops/secx 7,191 ops/sec+26%
chrome 26 on Windows 10x 5,696 ops/secx 3,314 ops/sec+47%+71%
android 6.0 on Linuxx 6,199 ops/secx 4,775 ops/sec+29%

* I did (nickyout / substack) - 1 in percentages

Arguably faster than last time, but more importantly, most latest versions of the most popular browsers get a bump in speed. I'll call that a win.

See caniuse browser usage for the 'most popular browsers'.

Click the build status badge to view the original output.

Also

It exposes the way strings are escaped for JSON:

var stringify = require('./'),
	stringSearch = stringify.stringSearch,
	stringReplace = stringify.stringReplace,
	str = "ay\nb0ss";
str.replace(stringSearch, stringReplace); // 'ay\\nb0ss'

It does NOT add the quotes before and after the string needed for JSON.stringify-ing strings. Fortunately, that isn't hard:

'"' + str.replace(stringSearch, stringReplace) + '"'; // '"ay\\nb0ss"'

Running tests

For testing in node, do:

npm test

I used zuul for testing on saucelabs. It's a very easy to use tool, but because their library is about 150MB I did not include it in the devDepencencies. I suggest installing it globally if you want to test:

# install zuul
npm install -g zuul
# then, to run all tests
zuul -- test/index.js

TODO

  • Test more unicode chars

Keywords

FAQs

Package last updated on 03 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc