Giter Site home page Giter Site logo

jitson's Introduction

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 the optimised parser fails it will fallback to JSON.parse and resample more data in future. If the optimised parser keeps failing it'll increase the sampling interval, to not waste too much time sampling and compiling.

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

jitson's People

Contributors

mafintosh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jitson's Issues

JSON.parse is much faster

As titled. I did something wrong?

const jitson = require("./");

const testPayload = JSON.stringify({ hello: "world", number: Math.random() });

// make an instance
const parse = jitson();
console.time("jitson");
for (var i = 0; i < 100; i++) {
  parse(testPayload);
}
console.timeEnd("jitson");

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

console.time("JSON.parse");
for (var i = 0; i < 100; i++) {
  JSON.parse(testPayload);
}
console.timeEnd("JSON.parse");

Result:

jitson: 3.855ms
{ type: 'object',
  ordered: true,
  properties: { hello: { type: 'string' }, number: { type: 'number' } } }
JSON.parse: 0.070ms

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.