GithubHelp home page GithubHelp logo

web5design / json-- Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alfred-nsh/json--

0.0 2.0 0.0 60 KB

A JSON parser that can be faster than v8 parser, by only parsing what it needs.

License: MIT License

json--'s Introduction

JSON--

This is a streaminig json parser, that only parsers what you tell it, based on dominictarr's JSONStream idea and creationix's jsonparse code(mostly his, didn't change much).

Install

npm install json--

How to use?

var JSONParser = require('json--'),
    parser = new JSONParser(['rows', true, 'name']);

parser.onValue = console.log.bind(console);
parser.write(new Buffer('{"rows":[{'));
parser.write(new Buffer('"lolz": "wow", "name" : "sweet"'));
parser.write(new Buffer('}]}'));

Note that the parser.write only accepts a Buffer instance.

API

require('json--') will return a JSONParser class.

new JSONParser(path)

  • Array<Any> path: Should contain at least one element. If it's a function, it will be called with the key name and it's value will be converted to a Boolean to see if the key is needed or not.

It will return an instance of json--.

jsonParser.onValue(value)

This function will be called on any value that matches the path completely.

jsonParser.write(buffer)

  • Buffer buffer

How fast is it?

Here's some numbers for parsing a subset of npm registry. The test file is test.js and the json file is npm.json. Tested under Ubuntu 12.04 64bit.

In Node 0.6.19:

JSON--, path: rows: 3892ms
JSON--, path: rows,true: 4661ms
JSON--, path: rows,true,doc: 2433ms
JSON--, path: rows,true,doc,_id: 543ms
toString: 180ms
JSON: 627ms

In Node v0.8.6:

JSON--, path: rows: 4702ms
JSON--, path: rows,true: 4049ms
JSON--, path: rows,true,doc: 2417ms
JSON--, path: rows,true,doc,_id: 825ms
toString: 190ms
JSON: 653ms

The toString is the amount of the time that it takes for the buffer to be converted to utf8(JSON.parse needs it to be in utf8 string), but JSON-- only operates on a buffer(it converts it internally).

What about memory?

As of memory, since it all keeps what it needs to emit, only the objects in the specified path(unlike creationix's jsonparse, which kept everything to have a complete root object.), it uses much less memory.

Why so fast?

The trick that made this sped up is that, it only parses the parts that are in the path, and it skips the parts that is not in the specified path with a simple code without verifying it. This is very experimental, and this skipping might result in an undefined state if it tries to parse an invalid JSON string.

What's up with the name?

Since it doesn't parses fully, its not a complete JSON parser, so decrease one from it. If you have a better name, I'm open to suggestions.

json--'s People

Contributors

alfred-nsh avatar

Watchers

 avatar  avatar

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.