Last active
July 22, 2019 03:58
-
-
Save tompng/f20916024c6001afbd635af8b8dd8362 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 gencode(code, n){ | |
code.push(`const a${Math.random()*9999|0}=${Math.random()}`) | |
if(n==0) { | |
code.push('console.log(123)') | |
return | |
} | |
const rnd = Math.random() | |
if(rnd < 0.5){ | |
code.push('for(let i=0;i<10;i++){') | |
gencode(code, n-1) | |
code.push('}') | |
}else{ | |
code.push('if(Math.random()<0.5){') | |
gencode(code, n-1) | |
code.push('}else{') | |
gencode(code, n-1) | |
code.push('}') | |
} | |
} | |
const vars=[] | |
for(let i=0;i<200;i++)vars.push(`let i${i}=${i}`) | |
const code=[] | |
gencode(code, 10) | |
f=eval(`()=>{${vars.join(';')};return x=>{${code.join(';')};return x+1}}`) | |
g=eval('()=>()=>{}') | |
benchmark=(h)=>{const t=new Date();for(let i=0;i<100000000;i++)h();console.log(new Date()-t)} | |
t=new Date();for(i=0;i<100000000;i++)f();console.log(new Date()-t) // 2110 | |
t=new Date();for(i=0;i<100000000;i++)g();console.log(new Date()-t) // 263 // 8倍速い | |
benchmark(f) // 2029 | |
benchmark(g) // 2012 あれ? (JITがうまく効くかどうかの差?) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment