Skip to content

add safeParse to benchmarks #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmarks/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 4 additions & 1 deletion benchmarks/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ suite
.add('JSON.parse', () => {
JSON.parse(internals.text)
})
.add('secure-json-parse', () => {
.add('secure-json-parse parse', () => {
sjson.parse(internals.text, { protoAction: 'ignore' })
})
.add('secure-json-parse safeParse', () => {
sjson.safeParse(internals.text)
})
.add('reviver', () => {
JSON.parse(internals.text, internals.reviver)
})
Expand Down
5 changes: 4 additions & 1 deletion benchmarks/no__proto__.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ suite
.add('JSON.parse', () => {
JSON.parse(internals.text)
})
.add('secure-json-parse', () => {
.add('secure-json-parse parse', () => {
sjson.parse(internals.text)
})
.add('secure-json-parse safeParse', () => {
sjson.safeParse(internals.text)
})
.add('reviver', () => {
JSON.parse(internals.text, internals.reviver)
})
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"name": "benchmarks",
"version": "1.0.0",
"scripts": {
"valid": "node valid.js",
"ignore": "node ignore.js",
"no_proto": "node no__proto__.js",
"remove": "node remove.js",
"throw": "node throw.js",
"all": "node --version && npm run ignore && npm run no_proto && npm run remove && npm run throw"
"all": "node --version && npm run valid && npm run ignore && npm run no_proto && npm run remove && npm run throw"
},
"dependencies": {
"benchmark": "^2.1.4"
Expand Down
5 changes: 4 additions & 1 deletion benchmarks/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ suite
.add('JSON.parse', () => {
JSON.parse(internals.text)
})
.add('secure-json-parse', () => {
.add('secure-json-parse parse', () => {
sjson.parse(internals.text, { protoAction: 'remove' })
})
.add('secure-json-parse safeParse', () => {
sjson.safeParse(internals.text)
})
.add('reviver', () => {
JSON.parse(internals.text, internals.reviver)
})
Expand Down
5 changes: 4 additions & 1 deletion benchmarks/throw.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ suite
JSON.parse(internals.invalid)
} catch (ignoreErr) { }
})
.add('secure-json-parse', () => {
.add('secure-json-parse parse', () => {
try {
sjson.parse(internals.text)
} catch (ignoreErr) { }
})
.add('secure-json-parse safeParse', () => {
sjson.safeParse(internals.text)
})
.add('reviver', () => {
try {
JSON.parse(internals.text, internals.reviver)
Expand Down
49 changes: 49 additions & 0 deletions benchmarks/valid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict'

const Benchmark = require('benchmark')
const sjson = require('..')

const internals = {
text: '{ "a": 5, "b": 6, "c": { "d": 0, "e": "text", "f": { "g": 2 } } }',
proto: '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }'
}

const suite = new Benchmark.Suite()

suite
.add('JSON.parse', () => {
JSON.parse(internals.text)
})
.add('JSON.parse proto', () => {
JSON.parse(internals.proto)
})
.add('secure-json-parse parse', () => {
sjson.parse(internals.text)
})
.add('secure-json-parse parse proto', () => {
sjson.parse(internals.text, { constructorAction: 'ignore', protoAction: 'ignore' })
})
.add('secure-json-parse safeParse', () => {
sjson.safeParse(internals.text)
})
.add('secure-json-parse safeParse proto', () => {
sjson.safeParse(internals.proto)
})
.add('JSON.parse reviver', () => {
JSON.parse(internals.text, internals.reviver)
})
.on('cycle', (event) => {
console.log(String(event.target))
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })

internals.reviver = function (key, value) {
if (key === '__proto__') {
return undefined
}

return value
}