npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

json-optimize

v0.1.1

Published

Optimize objects for smaller JSON footprint by temporarily replacing key names with shorter ones

Readme

JsonOptimize (json-optimize)

Optimize objects prior to JSON encoding by analyzing property names, and temporarily replacing them with shorter ones based on combined length and number of occurences. Corresponding function to restore the original object.

Obviously this does not always produce smaller documents, but for some cases where property names are repeated a lot of times in e.g. an array of similar documents it can help.

It's not optimized for speed or anything.

License

MIT.

But there's MsgPack?!

Sure, but this works well in combination with MsgPack also:

Example test file:

  • Original: 3 106 646
  • MsgPack'ed: 2 417 832
  • JsonOptimize'd: 2 161 369
  • JsonOptimize'd then MsgPack'ed: 1 472 453

(See other test example at the bottom of this page)

Usage

Node.js

var fs = require('fs');
var JsonOptimize = require('json-optimize');

// read some json
var doc = JSON.parse(fs.readFileSync('junk.json'));

// create optimized document
var jo = new JsonOptimize();
var optDoc = jo.pack(doc);

// translate back to original
var origDoc = jo.unpack(optDoc);

JavaScript

<script type="text/javascript" src="bower_components/json-optimize/build/json-optimize.min.js"></script>
<script type="text/javascript">
var jo = new JsonOptimize();
var bar = ..;
var opt = jo.pack(bar);
var orig = jo.unpack(opt);
</script>

API

Constructor

new JsonOptimize([options])

options is an optional object with various overrides:

  • idChars: which characters to use in ID generation (defaults: equivalent of [\x20-2F\x3A-7E\x20] [a-zA-Z]). (See note on object property order below)
  • omitUndefined: skip undefined values in the analysis process, as they will be removed by the JSON serialization anyhow (default: true)
  • excludeTypes: either an array of object types to not recurse into, or a function that will be called for each value (true: exclude, false: include). (default: [Date]).

Methods

pack(originalObject) : optimizedObject

unpack(optimizedObject) : originalObject

Implementation

The "optimized" document is basically in the form of an array with the first element being an array of the original key names, and the second element is the original document with propert names replaced.

Tests

  • npm test - regular sanity checks
  • npm run-script test-size - test performance

Notes

Circular references

There is no protection against circular references built-in. It will most likely go into an eternal loop, eventually blowing the call stack or running out of memory, whichever comes first. You should not be passing such objects into JSON.stringify() anyhow.

Object property order

According to the EcmaScript specification, the order of properties in an object is not really defined, but in most implementations (including Node.js and most browsers), the order is kept as long as the property name does not consist of only numerals, which appear in sorted order at the "beginning" of the object.

See: http://stackoverflow.com/a/5525812

I.e.: If you include '0'..'9' in the idChars option, do not expect property order to be preserved. But you shouldn't expect that anyhow.

Other example tests

Testing against a sample JSON file of all sets of MTG cards:

The question obviously is: with json + deflate/gzip being this close in byte savings, and also considerably faster, is there really anything to win from this? I don't know. I had fun writing this anyhow :P