Skip to content

Commit

Permalink
chore: update benchmarks and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
pooya parsa committed May 20, 2020
1 parent c739715 commit d49b56b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ console.log(destr('{ "deno": "yay" }'))

## Why?

⚠️ Before reading cool features, please note that `destr` is not always faster! When parsing a standard JSON string it is about **3 times slower** mainly because of transform to avoid [prototype pollution](https://hueniverse.com/a-tale-of-prototype-poisoning-2610fa170061) which can lead to serious security issues if not being sanetized.

In the other words, `destr` is better when input is not always a json string or from untrsuted source like request body.
Please note that `destr` is little bit slower when parsing a standard JSON string mainly because of transform to avoid [prototype pollution](https://hueniverse.com/a-tale-of-prototype-poisoning-2610fa170061) which can lead to serious security issues if not being sanetized. In the other words, `destr` is better when input is not always a json string or from untrsuted source like request body.

**Fast fallback to input if is not string:**

Expand All @@ -53,10 +51,10 @@ destr()
```

```js
// JSON.parse x 5,363,773 ops/sec ±0.31% (96 runs sampled)
// JSON.parse x 5,324,474 ops/sec ±0.65% (94 runs sampled)
JSON.parse(3.14159265359)

// destr x 660,537,795 ops/sec ±0.06% (86 runs sampled)
// destr x 657,187,095 ops/sec ±0.06% (98 runs sampled)
destr(3.14159265359)
```

Expand All @@ -71,21 +69,21 @@ destr('TRUE')
```

```js
// JSON.parse x 10,432,994 ops/sec ±0.23% (94 runs sampled)
// JSON.parse x 10,407,488 ops/sec ±0.30% (97 runs sampled)
JSON.parse('true')

// destr x 652,107,152 ops/sec ±0.11% (94 runs sampled
// destr x 88,634,032 ops/sec ±0.32% (95 runs sampled)
destr('true')
```

**Fallback to original value if parse fails (empty or any plain string):**

```js
// Uncaught SyntaxError: Unexpected token s in JSON at position 0
// JSON.parse (try-catch) x 248,749 ops/sec ±1.66% (93 runs sampled)
// JSON.parse (try-catch) x 248,212 ops/sec ±1.22% (84 runs sampled
JSON.parse('salam')

// destr x 32,415,523 ops/sec ±0.57% (94 runs sampled)
// destr x 30,867,179 ops/sec ±0.49% (94 runs sampled)
destr('salam')
```

Expand Down
3 changes: 3 additions & 0 deletions bench.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs')
const { Suite } = require('benchmark')
const sjson = require('secure-json-parse')
const destr = require('./dist')

const { log } = console
Expand All @@ -17,13 +18,15 @@ function createSuite (name) {
function bench (name, val) {
const suite = createSuite(name)
suite.add('JSON.parse', () => { JSON.parse(val) })
suite.add('sjson', () => { sjson.parse(val) })
suite.add('destr', () => { destr(val) })
suite.run()
}

function benchTryCatch (name, val) {
const suite = createSuite(name)
suite.add('JSON.parse (try-catch)', () => { try { JSON.parse(val) } catch (err) { return val } })
suite.add('sjson (try-catch)', () => { try { sjson.parse(val) } catch (err) { return val } })
suite.add('destr', () => { destr(val) })
suite.run()
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"bili": "latest",
"eslint": "latest",
"rollup-plugin-typescript2": "latest",
"secure-json-parse": "^2.1.0",
"standard-version": "latest",
"typescript": "latest"
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4498,6 +4498,11 @@ sax@~1.2.4:
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==

secure-json-parse@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/secure-json-parse/-/secure-json-parse-2.1.0.tgz#ae76f5624256b5c497af887090a5d9e156c9fb20"
integrity sha512-GckO+MS/wT4UogDyoI/H/S1L0MCcKS1XX/vp48wfmU7Nw4woBmb8mIpu4zPBQjKlRT88/bt9xdoV4111jPpNJA==

"semver@2 || 3 || 4 || 5", semver@^5.4.1, semver@^5.5.0, semver@^5.6.0:
version "5.7.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
Expand Down

0 comments on commit d49b56b

Please sign in to comment.