Strict Standards: Declaration of Walker_Page::start_lvl() should be compatible with Walker::start_lvl($output) in /home/narcvs/blog.narcvs.com/wp-includes/classes.php on line 584

Strict Standards: Declaration of Walker_Page::end_lvl() should be compatible with Walker::end_lvl($output) in /home/narcvs/blog.narcvs.com/wp-includes/classes.php on line 584

Strict Standards: Declaration of Walker_Page::start_el() should be compatible with Walker::start_el($output) in /home/narcvs/blog.narcvs.com/wp-includes/classes.php on line 584

Strict Standards: Declaration of Walker_Page::end_el() should be compatible with Walker::end_el($output) in /home/narcvs/blog.narcvs.com/wp-includes/classes.php on line 584

Strict Standards: Declaration of Walker_PageDropdown::start_el() should be compatible with Walker::start_el($output) in /home/narcvs/blog.narcvs.com/wp-includes/classes.php on line 603

Strict Standards: Declaration of Walker_Category::start_lvl() should be compatible with Walker::start_lvl($output) in /home/narcvs/blog.narcvs.com/wp-includes/classes.php on line 702

Strict Standards: Declaration of Walker_Category::end_lvl() should be compatible with Walker::end_lvl($output) in /home/narcvs/blog.narcvs.com/wp-includes/classes.php on line 702

Strict Standards: Declaration of Walker_Category::start_el() should be compatible with Walker::start_el($output) in /home/narcvs/blog.narcvs.com/wp-includes/classes.php on line 702

Strict Standards: Declaration of Walker_Category::end_el() should be compatible with Walker::end_el($output) in /home/narcvs/blog.narcvs.com/wp-includes/classes.php on line 702

Strict Standards: Declaration of Walker_CategoryDropdown::start_el() should be compatible with Walker::start_el($output) in /home/narcvs/blog.narcvs.com/wp-includes/classes.php on line 727

Strict Standards: Redefining already defined constructor for class wpdb in /home/narcvs/blog.narcvs.com/wp-includes/wp-db.php on line 58

Deprecated: Assigning the return value of new by reference is deprecated in /home/narcvs/blog.narcvs.com/wp-includes/cache.php on line 99

Strict Standards: Redefining already defined constructor for class WP_Object_Cache in /home/narcvs/blog.narcvs.com/wp-includes/cache.php on line 404

Deprecated: Assigning the return value of new by reference is deprecated in /home/narcvs/blog.narcvs.com/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /home/narcvs/blog.narcvs.com/wp-includes/theme.php on line 576
ajaxlights » Blog Archive » Javascript optimization guidelines

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';

and

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';

and

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);
A.push(v);

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:

    Hey…

    Where did you learn to do html?…

  6. Sam Says:

    Hey…

    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:

    Jamie5barton…

    Jamie5barton…

  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.