Skip to content

Instantly share code, notes, and snippets.

@jnvm
Created July 18, 2016 04:19
Show Gist options
  • Save jnvm/f63e28f55794a3a17fa30497b56d43bd to your computer and use it in GitHub Desktop.
Save jnvm/f63e28f55794a3a17fa30497b56d43bd to your computer and use it in GitHub Desktop.
var durations={}
,os={}
function aSet(){
return new Promise((res,rej)=>{
var N = n = 10,
m = 6
while(m--) {
//increase keys
N *= 10
n = N
if(!durations[N]) durations[N]=[]
//construct
if(!os[N]){
var o = {}
while(n--) o[btoa(n)] = Math.random()
os[N]=o
}
var o=os[N]
// random indices to check
var checks=" ".repeat(100).split("").map(x=>btoa(~~(Math.random()*N)))
//time checks, being sure to do something unoptimizable w/each val
var last=0
,t=Date.now()
,result=checks.reduce((sum,i)=>sum+o[i],0)
,dt=Date.now()-t
durations[N].push(dt)
console.log(`${N} took ${dt}`)
o={}
}
res()
})
}
//perform as promises to not thrash ui thread
var repeat=10
,p=aSet()
while(repeat--) p.then(aSet)
p.then(()=>{
//analyze
console.log(durations,
Object.keys(durations).reduce((set,n)=>{
return set.concat((durations[n].reduce((sum,v)=>sum+v)/durations[n].length).toFixed(3)+"ms for "+n )
},[])
.join("\n")
)
})
/*
some results:
0.273ms for 100
0.000ms for 1000
0.000ms for 10000
0.182ms for 100000
0.000ms for 1000000
0.000ms for 10000000
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment