Skip to content

Instantly share code, notes, and snippets.

@Ginden Ginden/ben.js Secret
Created Sep 28, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.