13 JavaScript Performance Tips

Daniel Clifford recently gave a great talk at Google I/O 2012 called “Breaking the JavaScript Speed Limit with V8”. In it he goes in depth to explain 13 simple optimizations you can do in your JavaScript code to help Chrome’s V8 JavaScript engine compile / run your JavaScript code faster. In the talk he gives a lot of great explanations as to what they are and why they help, but if you just want the quick and dirty list, here goes:

  1. Initialize all object members in constructor functions
  2. Always initialize object members in the same order
  3. Prefer numeric values that can be represented as 31-bit signed integers
  4. Use contiguous keys starting at 0 for Arrays
  5. Don’t pre-allocate large Arrays (>64K elements) to their maximum size, instead grow as you go
  6. Don’t delete elements in arrays, especially numeric arrays
  7. Don’t load uninitialized or deleted elements
  8. Initialize using array literals for small fixed-sized arrays
  9. Preallocate small arrays to correct size before using them
  10. Don’t store non-numeric values (objects) in numeric arrays
  11. Prefer monomorphic over polymorphic wherever possible
  12. Don’t use try {} catch {} blocks
  13. Avoid hidden class changes in functions after they are optimized

If you have time to check out the full video and/or slides I would highly recommend it. I know I for one will check back to the list often and am trying to watch for these gotchas in my code going forward.

This entry was posted in Javascript, Performance and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *