Skip to content

Commit 397ac6d

Browse files
authored
add safeParse to benchmarks (#73)
* add safeParse to benchmarks * pin tsd * pin dependencies
1 parent c4a7d71 commit 397ac6d

File tree

7 files changed

+68
-5
lines changed

7 files changed

+68
-5
lines changed

benchmarks/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

benchmarks/ignore.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ suite
1313
.add('JSON.parse', () => {
1414
JSON.parse(internals.text)
1515
})
16-
.add('secure-json-parse', () => {
16+
.add('secure-json-parse parse', () => {
1717
sjson.parse(internals.text, { protoAction: 'ignore' })
1818
})
19+
.add('secure-json-parse safeParse', () => {
20+
sjson.safeParse(internals.text)
21+
})
1922
.add('reviver', () => {
2023
JSON.parse(internals.text, internals.reviver)
2124
})

benchmarks/no__proto__.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ suite
1414
.add('JSON.parse', () => {
1515
JSON.parse(internals.text)
1616
})
17-
.add('secure-json-parse', () => {
17+
.add('secure-json-parse parse', () => {
1818
sjson.parse(internals.text)
1919
})
20+
.add('secure-json-parse safeParse', () => {
21+
sjson.safeParse(internals.text)
22+
})
2023
.add('reviver', () => {
2124
JSON.parse(internals.text, internals.reviver)
2225
})

benchmarks/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
"name": "benchmarks",
33
"version": "1.0.0",
44
"scripts": {
5+
"valid": "node valid.js",
56
"ignore": "node ignore.js",
67
"no_proto": "node no__proto__.js",
78
"remove": "node remove.js",
89
"throw": "node throw.js",
9-
"all": "node --version && npm run ignore && npm run no_proto && npm run remove && npm run throw"
10+
"all": "node --version && npm run valid && npm run ignore && npm run no_proto && npm run remove && npm run throw"
1011
},
1112
"dependencies": {
1213
"benchmark": "^2.1.4"

benchmarks/remove.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ suite
1313
.add('JSON.parse', () => {
1414
JSON.parse(internals.text)
1515
})
16-
.add('secure-json-parse', () => {
16+
.add('secure-json-parse parse', () => {
1717
sjson.parse(internals.text, { protoAction: 'remove' })
1818
})
19+
.add('secure-json-parse safeParse', () => {
20+
sjson.safeParse(internals.text)
21+
})
1922
.add('reviver', () => {
2023
JSON.parse(internals.text, internals.reviver)
2124
})

benchmarks/throw.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ suite
1919
JSON.parse(internals.invalid)
2020
} catch (ignoreErr) { }
2121
})
22-
.add('secure-json-parse', () => {
22+
.add('secure-json-parse parse', () => {
2323
try {
2424
sjson.parse(internals.text)
2525
} catch (ignoreErr) { }
2626
})
27+
.add('secure-json-parse safeParse', () => {
28+
sjson.safeParse(internals.text)
29+
})
2730
.add('reviver', () => {
2831
try {
2932
JSON.parse(internals.text, internals.reviver)

benchmarks/valid.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict'
2+
3+
const Benchmark = require('benchmark')
4+
const sjson = require('..')
5+
6+
const internals = {
7+
text: '{ "a": 5, "b": 6, "c": { "d": 0, "e": "text", "f": { "g": 2 } } }',
8+
proto: '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }'
9+
}
10+
11+
const suite = new Benchmark.Suite()
12+
13+
suite
14+
.add('JSON.parse', () => {
15+
JSON.parse(internals.text)
16+
})
17+
.add('JSON.parse proto', () => {
18+
JSON.parse(internals.proto)
19+
})
20+
.add('secure-json-parse parse', () => {
21+
sjson.parse(internals.text)
22+
})
23+
.add('secure-json-parse parse proto', () => {
24+
sjson.parse(internals.text, { constructorAction: 'ignore', protoAction: 'ignore' })
25+
})
26+
.add('secure-json-parse safeParse', () => {
27+
sjson.safeParse(internals.text)
28+
})
29+
.add('secure-json-parse safeParse proto', () => {
30+
sjson.safeParse(internals.proto)
31+
})
32+
.add('JSON.parse reviver', () => {
33+
JSON.parse(internals.text, internals.reviver)
34+
})
35+
.on('cycle', (event) => {
36+
console.log(String(event.target))
37+
})
38+
.on('complete', function () {
39+
console.log('Fastest is ' + this.filter('fastest').map('name'))
40+
})
41+
.run({ async: true })
42+
43+
internals.reviver = function (key, value) {
44+
if (key === '__proto__') {
45+
return undefined
46+
}
47+
48+
return value
49+
}

0 commit comments

Comments
 (0)