Skip to content

JavaScript Performance For Madmen

kevingadd edited this page Jul 10, 2012 · 58 revisions

JavaScript performance is terrifying and unpredictable. If you want things to run fast, you'll need a dowsing rod, but these test cases might help:

Performance minutiae for individual JS runtimes

SpiderMonkey/TraceMonkey/IonMonkey/JaegerMonkey (Mozilla Firefox)

  • Functions containing try/finally are run by the interpreter.

V8 (Google Chrome)

  • See http://www.youtube.com/watch?v=XAqIpGU8ZZk for various tips
  • This page describes how to pass flags to the V8 runtime when starting chrome. There are some flags you can pass to cause the V8 runtime to tell you when it fails to optimize a function. Unfortunately, these do not seem to be documented on the wiki, so see the below blog post...
  • This series of blog posts goes into depth on various V8 performance gotchas and describes how to diagnose some of the problems.
  • V8 cannot represent integers larger than 31 bits as an integer (they get promoted to floats).
  • Floating-point values are almost universally stored in the heap by V8 (which means each one is an allocation).
  • Any function containing a try/catch or try/finally is not optimized.
  • Functions that are too long (including comments and newlines/whitespace) are not inlined and may not be optimized.

Vaguely useful profiling tools

Mozilla Firefox

  • SPS: Currently only supports native profiling; JS profiling support forthcoming. Lets you share profiles on the web with other people! Amazing! It's a sampling profiler, so accuracy is poor, but reportedly you can adjust the sampling rate to get much better accuracy.
  • [JIT Inspector](https://addons.mozilla.org/en-US/firefox/addon/jit-inspector/: Completely inscrutable unless you read this PDF, at which point it is only partially inscrutable. Also, doesn't display actual numbers anywhere or let you save profiles...) Activating this deoptimizes your JS!
  • Firebug: If you can get the profiler to work instead of crashing the browser entirely, apparently it's pretty good. I've never gotten it to work.

Google Chrome

  • Web Inspector's Profiles tab: This is a sampling profiler with poor accuracy that often omits entire native call paths from your profiles, so the data is often a lie. Simply opening the Web Inspector deoptimizes your JS, and activating the profiler double-deoptimizes it.
  • chrome://tracing/: You can instrument your JS to show up here via console.time/console.timeEnd. Can only trace a few seconds at a time, but is fairly accurate. You can save/load traces.
  • WebGL Inspector: Let's pretend for a moment that WebGL performance is JS performance, since it sort of is. WebGL inspector gives you pretty accurate timings and recordings for your WebGL calls, so you can combine it with the built-in profiler to understand why your renderer is 50x slower than the one you wrote in Python.

Clone this wiki locally