diff --git a/benchmarks/.npmrc b/benchmarks/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/benchmarks/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/benchmarks/ignore.js b/benchmarks/ignore.js index 49c36f8..eda3e87 100755 --- a/benchmarks/ignore.js +++ b/benchmarks/ignore.js @@ -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) }) diff --git a/benchmarks/no__proto__.js b/benchmarks/no__proto__.js index 74763dc..f2724fe 100755 --- a/benchmarks/no__proto__.js +++ b/benchmarks/no__proto__.js @@ -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) }) diff --git a/benchmarks/package.json b/benchmarks/package.json index a6c243e..b12151b 100644 --- a/benchmarks/package.json +++ b/benchmarks/package.json @@ -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" diff --git a/benchmarks/remove.js b/benchmarks/remove.js index cd7ad60..af900db 100755 --- a/benchmarks/remove.js +++ b/benchmarks/remove.js @@ -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) }) diff --git a/benchmarks/throw.js b/benchmarks/throw.js index 92bcfc2..235da29 100755 --- a/benchmarks/throw.js +++ b/benchmarks/throw.js @@ -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) diff --git a/benchmarks/valid.js b/benchmarks/valid.js new file mode 100644 index 0000000..c221487 --- /dev/null +++ b/benchmarks/valid.js @@ -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 +}