google chrome - Why Javascript Eval is slower, when it shouldn't be -
google chrome - Why Javascript Eval is slower, when it shouldn't be -
so i'm trying create own js framework own pleasure (and convert usefullness) , of time focus on generating js dynamicly (since having higher level js language-in-the-middle bad idea, imo)
and got 1 trouble. trying following
var f = null; var converted = "f = function(){ <do here>"; for(var x in list) converted+=list[x]; converted+="};"; eval(converted); so pretty much writes constructed function f, can later execute.
but here's grab - runs slower, if write code of f in file, strange. allow me explain why it's unusual me: when run eval each time, chrome precompiler (or whatever browser precompiler - chrome target) can't cache-compile code, because expects, alter each run.
however, when save function, creates new vm machine file, because after saved function's code, can't alter it. why, when such eval, runs slower normal execution? mean after code executed more once. why should matter?
ps: method had shown above, works faster eval-ing each time. don't understand why stopwatches show time inbetween, again, ignoring first 1-10 calls
pps: test case: http://jsperf.com/evaluated-function-vs-real-function/2
eval unoptimizable because compiler doesn't know it's doing. if save function, compiler has opt out lot of optimizations, because changing code in way might break eval function.
this why, usually, when need eval, in function : way, compiler can sure didn't modify local scope in eval, , optimizes lot better.
js vms lot heuristics. seek guess want do, , optimize general case. eval (or new function) prevent them lot of that.
function.new might little faster, because compiler know won't seek modify scope.
also ! note eval might deed little differently you're used to. example, eval('a') , (0, eval)('a') not same :
i'll demonstrate this
window.a = 5; void function () { var = 1; eval('a = 2'); console.log(a); console.log(window.a); }(); this print 1 5
window.a = 5; void function () { var = 1; (0,eval)('a = 2'); // <- line has changed console.log(a); console.log(window.a); }(); whereas, print 1 2.
you can read : global eval, options.
javascript google-chrome eval
Comments
Post a Comment