Performance pitfalls in the V8 JavaScript engine

Performance pitfalls in the V8 JavaScript engine
Average rating: 0
(0 votes)

How about some news about Javascript? In recent years, JavaScript engines have improved on all fronts. The performance of JavaScript has reached a level where it can easily compete with programming languages that have traditionally been considered more appropriate for high-performance computing. This is also true for V8, the JavaScript engine inside of Chrome and Node.js. Let’s talk about performance pitfalls in the V8 JavaScript engine in more details.

JavaScript engines use techniques like speculative optimization and inline caching to speed up execution of the likely path. The compilers built into V8 – both the TurboFan optimizing compiler and the Ignition bytecode generator – are so-called method JITs, meaning the unit of compilation is always a method, aka a function in JavaScript speak. The optimizing compiler is able to include the bodies of other methods when it finds hot call sites and sees the potential for further optimizations via doing this, which is commonly referred to as inlining.

There’s an ongoing discussion in v8:8598 regarding the issue of this optimization limit. In general, there’ll always be a limit of some form, since it’s all about trade-offs in the engine. In V8’s case, the limit allows TurboFan to store various counts (i.e. the number of inputs and outputs of entities in the intermediate representation) as 16-bit integers vs. 32-bit integers. Other engines and languages have similar limits. For example, in Java the 64 KiB method limit is even part of the JVM bytecode specification.

  1. Try to avoid mixing field values of different types, i.e. don’t mix numbers, strings, objects, other primitives, unless that’s what you intended to do.
  2. Specifically, don’t pre-initialize number fields to null or undefined, but choose sensible default numbers (use NaN if in doubt).
  3. And try to initialize double fields (aka fields that are supposed to hold number values outside the small integer range) with double values outside the small integer range up-front, i.e. if in doubt put a NaN there first, and then store the actual initial value.

You should trust that the JavaScript engines usually do the right thing for you automatically. But as with everything, it’s good to be aware of some of the details, so in case you bump into problems, you’ll find your way around it, and sometimes giving the JavaScript engine an additional hint can be beneficial if the heuristics would otherwise fail badly. Find more examples here.

And read more on JavaScript loading Priorities in Chrome explained by Addy Osmani. He is a cool guy.

Rate this article, if you like it

Thanks! You’ve rated this material!

Got a project? Let's discuss it!

*By submitting this form you agree with our Privacy Policy.

Mailing & Legal Address

Syndicode Inc. 340 S Lemon Ave #3299, Walnut CA, 91789, USA

Visiting & Headquarters address
Kyiv Sofiivska 1/2a, 01001, Kyiv, Ukraine
Dnipro Hlinky 2, of. 1003, 49000, Dnipro, Ukraine
Phone (+1) 9035021111