Friday, December 9, 2011

Efficient Javascript Tips

Opera Developer Blog recently put up a post on Efficient Javascript Tips. It's worth reading if you  develop with Javascript.

  1. Avoid using eval or the Function constructor
    1. Rewrite that eval
    2. If you want a function, use a function
  2. Avoid using with
  3. Don't use try-catch-finally inside performance-critical functions
  4. Isolate uses of eval and with
  5. Avoid using global variables
  6. Beware of implicit object conversion
  7. Avoid for-in in performance-critical functions
  8. Use strings accumulator-style
  9. Primitive operations can be faster than function calls
  10. Pass functions, not strings, to setTimeout() and setInterval()


  1. Repaint and reflow
    1. Keeping the number of reflows to a minimum
    2. Minimal reflow
  2. Document tree modification
  3. Modifying an invisible element
  4. Taking measurements
  5. Making several style changes at once
  6. Trading smoothness for speed
  7. Avoid inspecting large numbers of nodes
  8. Improve speed with XPath
  9. Avoid modifications while traversing the DOM
  10. Cache DOM values in script variables

Document loading

  1. Avoid keeping alive references from one document to another
  2. Fast history navigation
  3. Use XMLHttpRequest
  4. Create SCRIPT elements dynamically
  5. location.replace() keeps the history under control

No comments: