Javascript optimization guidelines

May 19th, 2008

The Opera dev guys have put together a great list of JS optimization primers. Most of them I already follow, but a few of them were pleasant surprises.

The ones I already follow and which require little or no explanation or justification are:

  • Avoid using global variables
  • Avoid for-in in performance-critical functions
  • Pass functions, not strings, to setTimeout() and setInterval()
  • Isolate uses of eval and with
  • Don’t use try-catch-finally inside performance-critical functions
  • Avoid using with
  • Avoid using eval or the Function constructor
But these were the gems:
String Value to Object conversion when calling methods
The difference between 

var s1 = '0123456789';


var s2 = new String('0123456789');

is that s2 is an object with a bunch of associated methods, e.g. charAt and split, while s1 is just a value without these methods. When you call a string method on a string value, e.g. s1, the runtime engine has to convert that value into a temporary string object, call that method, return the value, and then discard the temporary object.
Who would have thought?
String concatenation
Similarly, the difference between

a1 += 'x' + 'y';


a2 += 'x';
a2 += 'y';

is that the concatenation of a1 requires a temporary variable to store ‘xy’ in before this gets appended to a1. That extra memory and garbage collection cycles is saved in the creation of a2.
Primitive operators can be faster than method calls
If you really need the very last ounce of performance, instead of writing

var min = Math.min(a,b);

use the slightly less convenient but faster

var min = a < b ? a : b;
A[A.length] = v;

This one certainly makes sense - but as always: first get it right by using whatever means you feel most comfortable with. Then if you really need it, get primitive.

9 Responses to “Javascript optimization guidelines”

  1. my blog Says:

    check this out…

    this is mine…

  2. Affiliate Programs Says:

    Affiliate Programs…

    Affiliate Programs…

  3. government student grants Says:

    government grant money…

    Hows it goin? Just read your info. Gotta say - you have great info on your site. You should check out my government land grants page if you have a minute….

  4. Wave Invites Says:

    Wave Invites…

    What a refreshing topic! I really enjoyed your point of view and can’t wait to read some of your upcomming posts. I’m going to link to your blog if you don’t mind as I think my readers might find some value in your topics….

  5. Dave Says:


    Where did you learn to do html?…

  6. Sam Says:


    Hi, I don’t want to change the conversation, but how precisely do you write code to create a web site like this web page? Is it extremely difficult? Where precisely do you start?…

  7. BucknerSYLVIA27 Says:

    Do you know that this is correct time to receive the credit loans, which will make your dreams come true.

  8. Jamie5barton Says:



  9. Getting online car loan rates Says:

    Car radio install…

    Car radio install base,give you same guide about car radio mounting,car radio repair and removal….

Leave a Reply

You must be logged in to post a comment.