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 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonm

v1.0.10

Published

json compressor for packing messages with memoization

Downloads

1,731

Readme

jsonm

"Listen very carefully, I shall say 'zis only once!"

jsonm is a fast and safe way to compress JSON messages using memoization. jsonm makes messages up to several orders of magnitude smaller by getting rid of repeated names and values.

jsonm is best friends with websocket libraries like socket.io. Modern browsers can gzip messages sent over websockets, and jsonm can make them even smaller for maximum responsiveness of web applications.

Installation

$ npm install --save jsonm

In the client, jsonm works well with webpack or browserify. Our npm distribution also includes a prepackaged client-side version in build/jsonm.js.

Compression Examples

jsonm packs

[
    { "firstName": "Francis", "lastName": "Doe" },
    { "firstName": "Anna", "lastName": "Smith" },
    { "firstName": "Agent", "lastName": "Smith", "isAlias": true },
    { "firstName": "Anna", "lastName": "Francis" }
]

into

[ 0,
    ["firstName", "lastName", "Francis", "Doe"],
    [3, 4, "Anna", "Smith"],
    [3, 4, "isAlias", "Agent", 8, true],
    [3, 4, 7, 5]
]

Notice how it eliminates all common substrings like "firstName" using memoization! jsonm keeps a dictionary to compress future messages even further. Send the message above a second time, and it becomes:

[0,[3,4,5,6],[3,4,7,8],[3,4,9,10,8,11],[3,4,7,5],1]

And

[
    { "firstName": "Bryan", "lastName": "Fuller" },
    { "firstName": "Anna", "lastName": "Adams" },
    { "firstName": "Tim", "lastName": "Peterson" },
    { "firstName": "Francis", "lastName": "Peterson" }
]

becomes

[0,[3,4,"Bryan","Fuller"],[3,4,7,"Adams"],[3,4,"Tim","Peterson"],[3,4,5,16]]

By avoiding repetition, jsonm can for example help decrease the size of messages sent from a web server to the client. It effectively leaves out all information the client already knows about.

Usage

jsonm is designed for sending messages between a sender and a receiver. The sender packs messages and the receiver unpacks them.

Sender, packing a message:

const packer = new jsonm.Packer();
let packedMessage = packer.pack(message);

Receiver, unpacking a message:

const unpacker = new jsonm.Unpacker();
let message = unpacker.unpack(packedMessage);

Both the packer and unpacker maintain a stateful dictionary. Don't lose them! Create a new packer for each new connection, or use packer.reset() to reset the dictionary of an existing packer.

Working with Strings

jsonm provides packString() for dealing with messages in string form.

packString() can be used to efficiently pack multi-line strings. For example, a string "foo\nbar" is packed as if ["foo", "bar"] was packed:

let packed = packer.packString("foo\nbar");
unpacker.unpack(packed); // returns "foo\nbar"

packString() can also efficiently pack JSON objects in string format, internally parsing and stringifying them:

let packed = packer.packString('{"foo":"bar"}');
unpacker.unpack(packed); // returns '{"foo":"bar"}'

Related Projects

  • JSONC: https://github.com/tcorral/JSONC
  • JSONH: https://github.com/WebReflection/JSONH
  • JSONR: https://github.com/dogada/RJSON

These projects pack uniform JavaScript objects, eliminating the need for repeating the keys of each object. As an example, JSONH can pack

[
    { "firstName": "John", "lastName": "Doe", "isAlias": false },
    { "firstName": "Anna", "lastName": "Smith", "isAlias": false },
    { "firstName": "Agent", "lastName": "Smith", "isAlias": true }
]

into

[3,"firstName","lastName","isAlias","John","Doe",false,"Anna","Smith",false,"Agent","Smith",true]

JSONC, JSONH, and JSONR don't apply memoization and only help with uniform data or data with a recurring scheme. Unlike jsom, however, they are stateless, which can make it easier to use them in some cases.

License

MIT