performance - Does JavaScript cache/optimize code if repeated multiple times? -
i'm writing small framework test speed of javascript functions. when repeatedly call same methods same parameter, gives me strange results:
function execution time isevenbitwise 38.00000000046566 isevenmodulo 38.00000000046566 isevenpointless 38.00000000046566
here functions:
var myfunctions = { isevenbitwise: function(number) { return !(number & 1); }, isevenmodulo: function(number) { return (number % 2 == 0); }, isevenpointless: function(number) { return 1; } }
the code runs functions:
performancetest.prototype.measuretime = function() { (var indextests = 0; indextests < this.testcount; indextests++) { var results = []; (var currentfunction in this.functions) { var contextfunction = this.functions[currentfunction]; var starttime = performance.now(); (var = 0; < this.iterationspertest; i++) { var heh = contextfunction.apply(this, arguments) } var executiontime = performance.now() - starttime; var result = {}; result.testname = currentfunction; result.executiontime = executiontime; results.push(result); } this.testresults.push(results); } }
does javascript interpreter cache/optimize code? if so, how work? or there else happening i'm not aware of?
edit: seems occur in chrome, firefox works fine these results:
function execution time isevenbitwise 9.652258097220447 isevenmodulo 37.546061799704376 isevenpointless 8.512472488871936
after looking @ code going make guess chrome being smart doing. seeing this:
var starttime = performance.now(); (var = 0; < this.iterationspertest; i++) { var heh = contextfunction.apply(this, arguments) } var executiontime = performance.now() - starttime;
it correctly assessing contextfunction has no side effects, recognising heh
variable exists within loop scope , never used , optimising entire loop away because doesn't anything.
Comments
Post a Comment