jitson

Just-In-Time JSON.parse compiler

  • jitson

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
jitson
17721.0.04 years ago4 years agoMinified + gzip package size for jitson in KB

Readme

jitson
Just-In-Time JSON.parse compiler
npm install jitson

Works by schema sampling the incoming data and if the schema is stable, it will compile a fast parser for it using turbo-json-parse

Usage

const jitson = require('jitson')

// make an instance
const parse = jitson()

for (var i = 0; i < 10; i++) {
  console.log(parse(JSON.stringify({hello: 'world', number: Math.random()})))
}

// Check if the compiler found a matching schema.
// If so it is using an optimised parser to parse the JSON
console.log(parse.schema)

API

const parse = jitson(opts)

Create a new JSON parser.
Options include
{
  sampleInterval: 100 // sample the schema everytime we parse 100 objects
}

It keeps a small internal cache around of old schemas that is used to produce a better parser. If the cache is empty it will sample right away as well.
Any additional options are forwarded to turbo-json-parse when it triggers a parser compilation.
It works the best if you try to only pass data to it that has a schema so make an instance for each of your http endpoints for example

const object = parse(src)

Similar to JSON.parse. Will schema sample the input once in a while to check if it has a stable schema. If so it'll optimise the parser.
If you are parsing from Node.js buffers make sure to pass that as the src instead of toString()ing it first as that will produce a faster parser when compiling.

License

MIT