-
-
Save Ginden/381448a17f50c7669b9a3693742e3a3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function benchmark(repeats, fn) { | |
'use strict'; | |
repeats = Math.min(repeats|0, 10); | |
const args = Array.prototype.slice.call(arguments, 2); | |
const timeStart = (window.performance || Date).now(); | |
window.empty = (function(){ | |
var last; | |
for(var i = 0; i < repeats; i++) { | |
last = fn.apply(null, args); | |
} | |
return last; | |
}()); | |
return (window.performance || Date).now()-timeStart; | |
} | |
_arr = Array.from(Array(1000), (e,i)=>({[Math.random()]: {[Math.random()]: [Math.random()]}})); | |
arr = JSON.stringify(_arr); | |
r = 1000*100; | |
console.log('Simple JSON.parse, '+r+' iterations: '+benchmark(r, ()=>JSON.parse(arr))+'ms'); | |
console.log('JSON.parse with copy, '+r+' iterations: '+benchmark(r, ()=>JSON.parse(arr, (k,v)=>{ | |
if (v && typeof v === 'object' && !Array.isArray(v)) { | |
return Object.assign(Object.create(null), v); | |
} | |
return v; | |
}))+'ms'); | |
console.log('JSON.parse with __proto__ change, '+r+' iterations: '+benchmark(r, ()=>JSON.parse(arr, (k,v)=>{ | |
if (v && typeof v === 'object' && !Array.isArray(v)) { | |
v.__proto__ = null | |
} | |
return v; | |
}))+'ms'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment