|
| 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