function methodCall(){ function square(n){ return n*n }; var i=10000, sum = 0; while(i-‐-‐) sum += square(i); } function inlinedMethod(){ var i=10000, sum = 0; while(i-‐-‐) sum += i*i; }
function methodCall(){ function square(n){ return n*n }; var i=10000, sum = 0; while(i-‐-‐) sum += square(i); } function inlinedMethod(){ var i=10000, sum = 0; while(i-‐-‐) sum += i*i; }
function methodCall(){ function square(n){ return n*n }; var i=10000, sum = 0; while(i-‐-‐) sum += square(i); } function inlinedMethod(){ var i=10000, sum = 0; while(i-‐-‐) sum += i*i; }
function literals(){ var a = [], o = {}; } function classic(){ var a = new Array, o = new Object; }
1 * stringcoerces the string into a float, result = 12.5 double bitwise NOT* floors the number > ~~(1 * "12.5") 12 *good overview on http://tr.im/bitwise
var test = ''; for (var i = 0;i<10000;i++) test = test + str; var test = '', i = 10000; while(i-‐-‐) test = test + str;
for loop while loop 0.12s 0.12s 0.13s 0.13s 0.6s 0.6s 0.04s 0.04s
for loop while loop 0.12s 0.12s 0.13s 0.13s 0.6s 0.6s 0.04s 0.04s
for loop while loop 0.12s 0.12s 0.13s 0.13s 0.6s 0.6s 0.04s 0.04s
for loop while loop 0.12s 0.12s 0.13s 0.13s 0.6s 0.6s 0.04s 0.04s
for loop while loop 0.12s 0.12s 0.13s 0.13s 0.6s 0.6s 0.04s 0.04s
var test = ''; for (var i = 0;i<10000;i++) test = test + str; var test = '', i = 10000; while(i-‐-‐) test = test + str; 3 expressions in “for” 1 expression in “while” (when i equals 0, expression will be false)
function unrolledLoop(){ var j=0; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; j++; }
function uncached(){ var i = 10000; while(i-‐-‐) window.test = 'test'; } function cached(){ var w = window, i = 10000; while(i-‐-‐) w.test = 'test'; }
var b = false, n = 99; function(){ return n*n && b; } function(){ return b && n*n; }
var b = false, n = 99; function(){ return n*n && b; } function(){ return b && n*n; } b is false, so n*n doesn’t need to get evaluated
>>> var n = 1; undefined >>> if(true && (n=2)) ...; >>> n 2 >>> if(true || (n=3)) ...; >>> n 2 not a pure engine optimization, the execution actually stops here, n=2 needs to be evaluated, so n is set to 2 here it doesn’t (expression must be true), so n is NOT set to 3
function(){ var obj = { prop: 'test', str: '' }; with(obj){ var i = 10000; while(i-‐-‐) str += prop; return str; } } function(){ var obj = { prop: 'test', str: '' }, i = 10000; while(i-‐-‐) obj.str += obj.prop; return obj.str; }
with(obj){ p }obj.p 0.071s 0.012s 0.039s 0.028s 0.078s 0.078s 0.077s 0.006s
with(obj){ p }obj.p 0.071s 0.012s 0.039s 0.028s 0.078s 0.078s 0.077s 0.006s
with(obj){ p }obj.p 0.071s 0.012s 0.039s 0.028s 0.078s 0.078s 0.077s 0.006s
with(obj){ p }obj.p 0.071s 0.012s 0.039s 0.028s 0.078s 0.078s 0.077s 0.006s
with(obj){ p }obj.p 0.071s 0.012s 0.039s 0.028s 0.078s 0.078s 0.077s 0.006s
var a = 0; function(){ try{ a += 1; } catch(e) {} } function(){ a += 1; }
function () { return 2 * 3; } function () { return 2 * 3; }
function () { return 2 * 3; } function () { return 2 * 3; } function () { return 2 * 3; }
function () { return 2 * 3; } function () { return 2 * 3; } function () { return 2 * 3; } function () { return 6; }
function () { return 2 * 3; } function () { return 2 * 3; } function () { return 2 * 3; } function () { return 6; } WTF?