Understand V8 to Make JavaScript Run Faster

V8 is Google’s open source high-performance JavaScript engine and it is also what Node.js based on. V8 is written in C++ and can run standalone, or can be embedded into any C++ application. V8 compiles JavaScript to native machine code (IA-32, x86-64, ARM, or MIPS CPUs) before executing it. Daniel Clifford’s great Google IO talk provides an in-depth explanation on how V8 works and how to avoid the speed traps.

V8 has two compilers, a base or “full” compiler and an optimizing compiler. The base compiler directly generates execution code without performing any type analysis. During runtime V8 creates hidden classes for fast property access and uses Inline Caching (IC) to refine knowledge about types. The optimizing compiler re-compiles hot functions based on heuristics of the code’s execution profile after the type information is collected from ICs. The efficient garbage collection ensures fast object allocation, short garbage collection pauses, and no memory fragmentation.Image

Tips and tricks to avoid the speed trap and improve JavaScript performance in V8

  1. Initialize all object members in constructor functions (fixed object layout simplifies property access; an object changes its hidden class when a new property is added)
  2. Initialize object members in the same order (to avoid different trees of hidden classes)V8-hidden class
  3. Prefer numeric values that can be represented as 31-bit signed integers (to avoid conversion to a boxed double from SMI – SMall Integer)V8-tagging
  4. Use contiguous keys starting at 0 for Arrays (there two types of array storage in V8, fast elements use linear storage, dictionary elements use hash table storage, sparse arrays will make V8 to switch to dictionary element mode)
  5. Don’t pre-allocate large Arrays (e.g. > 64K elements) to their maximum size, instead grow as you go
  6. Don’t delete elements in arrays (to avoid making key sets sparse ), especially numeric arrays
  7. Don’t load uninitialized or deleted elementsV8-2
  8. Pre-allocate small arrays to correct size before using themImage
  9. Don’t store non-numeric values (objects) in numeric arrays (to avoid re-boxing)
  10. Initialize using array literals for small fixed-sized arraysV8-Array literal
  11. Prefer monomorphic over polymorphic whenever possible
  12. Avoid hidden class changes in functions in the late time running of the code, i.e., after they are optimized
  13. Avoid de-optimization or bail-outV8-bailout workaround
  14. Reduce to pure JavaScript (no DOM, V8 is only for JavaScript)

Helpful resources


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s