jitson

1.0.0 • Public • Published

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

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.0
    11
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.0
    11

Package Sidebar

Install

npm i jitson

Weekly Downloads

11

Version

1.0.0

License

MIT

Unpacked Size

7.95 kB

Total Files

5

Last publish

Collaborators

  • mafintosh