Javascript Performance Lessons from Qooxdoo

Posted on 05/12/2008 @ 06:40PM

While googling for some information on improving Javascript performance, I came across this Javascript Best Practices document from the Qooxdoo people. A few of the (summarized) points that I found useful:

Temporary references

The following code is wasteful since it dereferences anObject.aReference.anotherReference 4 times:

anObject.aReference.anotherReference.someThing = true;
anObject.aReference.anotherReference.someThingElse = 3.141;
anObject.aReference.anotherReference.callSomeMethod();
anObject.aReference.anotherReference.callAnotherMethod();

Using a temporary reference eliminates the redundancy and speeds things up:

var tmpReference = anObject.aReference.anotherReference;
tmpReference.someThing = true;
tmpReference.someThingElse = 3.141;
tmpReference.callSomeMethod();
tmpReference.callAnotherMethod();

Loop-optimization

 

This loop has to calculate someArray.length for each iteration:

for(var i=0; i < someArray.length; i++){ }


Rewriting it like this will only do the calculation once:

for(var i=0, l = someArray.length; i < l; i++){ }

Read the entire article here.