Writing efficient JavaScript for V8

Daniel Clifford from the V8 team, gave a presentation in Google I/O 2012 about optimizing JavaScript for V8 to make it run faster. I am posting notes which I took while watching the video, so that I can refer to them at a later point of time (It is easier to search when it is not on paper 🙂 ). Also it might help someone to get the outline of the talk before actually watching the video. The original video runs for about 45 minutes.

Also be warned that the following is my own interpretation of the video and I might have missed or could have interpreted some point differently 🙂

Breaking the JavaScript speed limit for V8

The talk was basically built on the premises that if you understand how V8 compiles and optimizes your JavaScript code then you can write efficient JavaScript code (at least for V8). Daniel explained a few optimization techniques that V8 uses and also gave suggestions for writing JavaScript code that will make use of these optimization techniques employed by V8.

I am not going to get into the details of these optimization techniques, but just going to give the list the suggestions.


V8 internally creates hidden classes for each JavaScript object at runtime. Objects with the same hidden class can use the same optimized generated code. So the suggestions are

  • Initialize all object members in the constructor functions
  • Initialize all object members in the same order.
  • Avoid adding new properties at runtime

Code sample


V8 internally uses 31 bit signed integers (refer to the video for the full explanation). So the suggestion is

  • Prefer numeric values that can be represented as 31 bit signed integers


V8 uses two types of objects to represent arrays

  • Fast Elements – linear storage for compact key sets
  • Dict Elements – hash table storage.

Out of these two, Fast Elements are generally preferred. So the suggestions are

  • Use contiguous keys starting at 0 for arrays
  • Don’t pre-allocate large arrays, instead grow them
  • Don’t delete elements in an array if possible
  • Don’t load uninitialized or deleted elements
  • Use array literals instead for small fixed arrays
  • Preallocate small arrays to correct size before using them
  • Don’t store non-numeric values (objects) in numeric arrays

Code sample

Other suggestions

  • Prefer monomorphic operations over polymorphic operations
  • Don’t have performance sensitive code inside a try..catch block. Instead use a separate function and then enclose the function inside try..catch block.

Some V8 logging commands


And finally here is the full video.

So my dear readers what you think about my notes. Also let me know if you like notes for videos in this format. If there is a demand, then I can post some of my notes on other videos which I have already viewed.


Related posts


0 Comments so far

Follow up comments through RSS Feed | Post a comment

Leave a Reply

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