How can I write faster JavaScript?

Posted by a paid nerd on Stack Overflow See other posts from Stack Overflow or by a paid nerd
Published on 2011-11-29T01:26:26Z Indexed on 2011/11/29 1:49 UTC
Read the original article Hit count: 149

I'm writing an HTML5 canvas visualization. According to the Chrome Developer Tools profiler, 90% of the work is being done in (program), which I assume is the V8 interpreter at work calling functions and switching contexts and whatnot.

Other than logic optimizations (e.g., only redrawing parts of the visualization that have changed), what can I do to optimize the CPU usage of my JavaScript? I'm willing to sacrifice some amount of readability and extensibility for performance. Is there a big list I'm missing because my Google skills suck? I have some ideas but I'm not sure if they're worth it:

  • Limit function calls
  • When possible, use arrays instead of objects and properties
  • Use variables for math operation results as much as possible
  • Cache common math operations such as Math.PI / 180
  • Use sin and cos approximation functions instead of Math.sin() and Math.cos()
  • Reuse objects when passing around data instead of creating new ones
  • Replace Math.abs() with ~~
  • Study jsperf.com until my eyes bleed
  • Use a preprocessor on my JavaScript to do some of the above operations

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about Performance