Search

1/02/2010

Extreme JavaScript Performance

Extreme JavaScript Performance

#1 avoid function calls.

function methodCall() {
function square(n) {return n*n};
var i=10000, sum=0;
while (i--) sum += square(i);
}
function inlineMethod() {
var i=1000, sum=0;
while(i--) sum += i*i;
}

#2 embrace the language

function literals() {
var a = [], o = {};
}
function classic() {
var a = new Array, o = new Object;
}

#4 cache globals

function uncached() {
var i = 10000;
while(i--) window.test = 'test';
}
function cached() {
var w = window, i = 10000;
while(i--) w.test = 'test';
}

(Firefox, Safari, Chrome)Modern JavaScript engines have JIT comilers, which don't support certain features well.
Avoid stuff that's not available in ECMA-262 5th Edition "strict" mode.


via: http://developer.yahoo.net/blog/archives/2009/11/jsconfeu_all_th.html

沒有留言: