├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── benchmark.js ├── package.json ├── parse.js └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | 4 | node_js: 5 | - "5" 6 | - "4" 7 | - "0.12" 8 | - "0.11" 9 | - "0.10" 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Matteo Collina 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fast-json-parse 2 | 3 | [![Build Status](https://travis-ci.org/mcollina/fast-json-parse.svg)](https://travis-ci.org/mcollina/fast-json-parse) 4 | 5 | It is equivalent to [json-parse-safe](http://npm.im/json-parse-safe), 6 | but it set both the `err` and `value` property to null. 7 | 8 | The reason why this is fast is that `try/catch` inhibits the functions 9 | in which you use them to be optimized. This assumption holds true up to 10 | Node 6, from Node 7 and forward this module is not useful anymore. 11 | 12 | ## Install 13 | 14 | ``` 15 | npm i fast-json-parse --save 16 | ``` 17 | 18 | ## Usage 19 | 20 | You can use it as a function or via a contructor, as you prefer. 21 | 22 | ### function 23 | 24 | ```js 25 | 'use strict' 26 | 27 | var parse = require('fast-json-parse') 28 | var fs = require('fs') 29 | 30 | var result = parse(fs.readFileSync('./package.json', 'utf8')) 31 | 32 | if (result.err) { 33 | console.log('unable to parse json', result.err.message) 34 | } else { 35 | console.log('json parsed successfully', result.value) 36 | } 37 | ``` 38 | 39 | ### constructor 40 | 41 | ```js 42 | 'use strict' 43 | 44 | var Parse = require('fast-json-parse') 45 | var fs = require('fs') 46 | 47 | var result = new Parse(fs.readFileSync('./package.json')) 48 | 49 | if (result.err) { 50 | console.log('unable to parse json', result.err.message) 51 | } else { 52 | console.log('json parsed successfully', result.value) 53 | } 54 | ``` 55 | 56 | ## Acknowledgements 57 | 58 | fast-json-parse is sponsored by [nearForm](http://nearform.com). 59 | 60 | ## License 61 | 62 | MIT 63 | -------------------------------------------------------------------------------- /benchmark.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var benchmark = require('benchmark') 4 | var suite = new benchmark.Suite() 5 | var fs = require('fs') 6 | var myData = fs.readFileSync('./package.json') 7 | var Parse = require('./') 8 | var safe = require('json-parse-safe') 9 | var max = 100 10 | 11 | suite.add('Parse class', function parseBench () { 12 | var parsed = new Parse(myData) 13 | if (parsed.err) { 14 | return 15 | } 16 | 17 | // add a loop to simulate some activity here 18 | for (var i = 0; i < max; i++) { 19 | var count = 0 20 | var keys = Object.keys(parsed.value) 21 | for (var k = 0; k < keys.length; k++) { 22 | count++ 23 | } 24 | } 25 | }) 26 | 27 | suite.add('Parse wrapped', function parseBench () { 28 | var parsed = Parse(myData) 29 | if (parsed.err) { 30 | return 31 | } 32 | 33 | // add a loop to simulate some activity here 34 | for (var i = 0; i < max; i++) { 35 | var count = 0 36 | var keys = Object.keys(parsed.value) 37 | for (var k = 0; k < keys.length; k++) { 38 | count++ 39 | } 40 | } 41 | }) 42 | 43 | suite.add('json-parse-safe', function parseBench () { 44 | var parsed = safe(myData) 45 | if (parsed.error) { 46 | return 47 | } 48 | 49 | // add a loop to simulate some activity here 50 | for (var i = 0; i < max; i++) { 51 | var count = 0 52 | var keys = Object.keys(parsed.value) 53 | for (var k = 0; k < keys.length; k++) { 54 | count++ 55 | } 56 | } 57 | }) 58 | 59 | suite.add('try catch here', function tryCatchBench () { 60 | var data = null 61 | try { 62 | data = JSON.parse(myData) 63 | } catch (err) { 64 | return 65 | } 66 | 67 | // add a loop to simulate some activity here 68 | for (var i = 0; i < max; i++) { 69 | var count = 0 70 | var keys = Object.keys(data) 71 | for (var k = 0; k < keys.length; k++) { 72 | count++ 73 | } 74 | } 75 | }) 76 | 77 | suite.on('complete', function print () { 78 | for (var i = 0; i < this.length; i++) { 79 | console.log(this[i].toString()) 80 | } 81 | 82 | console.log('Fastest is', this.filter('fastest').map('name')[0]) 83 | }) 84 | 85 | suite.run() 86 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fast-json-parse", 3 | "version": "1.0.3", 4 | "description": "Parse json safely and at max speed", 5 | "main": "parse.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/mcollina/fast-json-parse.git" 9 | }, 10 | "bugs": { 11 | "url": "https://github.com/mcollina/fast-json-parse/issues" 12 | }, 13 | "homepage": "https://github.com/mcollina/fast-json-parse#readme", 14 | "scripts": { 15 | "test": "standard && tap test.js" 16 | }, 17 | "pre-commit": "test", 18 | "keywords": [ 19 | "parse", 20 | "json", 21 | "fast", 22 | "safe" 23 | ], 24 | "author": "Matteo Collina ", 25 | "license": "MIT", 26 | "devDependencies": { 27 | "benchmark": "^2.1.0", 28 | "json-parse-safe": "^1.0.3", 29 | "pre-commit": "^1.1.2", 30 | "standard": "^6.0.5", 31 | "tap": "^5.5.0" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /parse.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function Parse (data) { 4 | if (!(this instanceof Parse)) { 5 | return new Parse(data) 6 | } 7 | this.err = null 8 | this.value = null 9 | try { 10 | this.value = JSON.parse(data) 11 | } catch (err) { 12 | this.err = err 13 | } 14 | } 15 | 16 | module.exports = Parse 17 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var test = require('tap').test 4 | var Parse = require('./') 5 | 6 | function successTests (value) { 7 | test('parse successfully with the function', function (t) { 8 | t.plan(2) 9 | var result = Parse(value) 10 | t.error(result.err) 11 | t.deepEqual(result.value, JSON.parse(value)) 12 | }) 13 | 14 | test('parse successfully with the constructor', function (t) { 15 | t.plan(2) 16 | var result = new Parse(value) 17 | t.error(result.err) 18 | t.deepEqual(result.value, JSON.parse(value)) 19 | }) 20 | } 21 | 22 | function failureTests (value) { 23 | var expectedErr 24 | try { 25 | JSON.parse(value) 26 | } catch (err) { 27 | expectedErr = err 28 | } 29 | 30 | test('parse unsuccessfully with the function', function (t) { 31 | t.plan(2) 32 | var result = Parse(value) 33 | t.notOk(result.value, 'no value') 34 | t.equal(result.err.message, expectedErr.message) 35 | }) 36 | 37 | test('parse successfully with the constructor', function (t) { 38 | t.plan(2) 39 | var result = new Parse(value) 40 | t.notOk(result.value, 'no value') 41 | t.equal(result.err.message, expectedErr.message) 42 | }) 43 | } 44 | 45 | successTests('{ "object": 32 }') 46 | failureTests('{ "object": 32') 47 | --------------------------------------------------------------------------------