# JavaScript Performance Pitfalls in V8

DevFeed: [JavaScript Performance Pitfalls in V8](<https://devfeed.tech/articles/javascript-performance-pitfalls-in-v8-21878.md>)

Original publisher: [Read original article](<https://ponyfoo.com/articles/javascript-performance-pitfalls-v8>)

Author: benedikt.meurer@googlemail.com (Benedikt Meurer)

Published: 2019-03-05T16:32:03Z

Content type: article

Language: en

Sources: [Pony Foo](<https://devfeed.tech/sources/pony-foo.md>)

Topics: [JavaScript](<https://devfeed.tech/topics/javascript.md>), [V8](<https://devfeed.tech/topics/v8.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [Optimization](<https://devfeed.tech/topics/optimization.md>), [inlining](<https://devfeed.tech/topics/inlining.md>), [Programming](<https://devfeed.tech/topics/programming.md>)

Tags: [bytecode](<https://devfeed.tech/tags/bytecode.md>), [compilation](<https://devfeed.tech/tags/compilation.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [function](<https://devfeed.tech/tags/function.md>), [inlining](<https://devfeed.tech/tags/inlining.md>), [internals](<https://devfeed.tech/tags/internals.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [performance](<https://devfeed.tech/tags/performance.md>), [v8](<https://devfeed.tech/tags/v8.md>)

## AI overview

This article explains two performance pitfalls in the V8 JavaScript engine. It discusses engine heuristics such as speculative optimization and inline caching, then describes V8's method-based JIT compilation, optimization limits, and inlining.

## Source excerpt

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 not only true for V8, the JavaScript engine inside of Chrome and Node.js, but for all major JavaScript engines, including ChakraCore, the engine inside of Edge, JavaScriptCore, the engine inside of Safari, and SpiderMonkey, the engine inside of Firefox. Not only did the peak performance improve, but engines also managed to deliver more consistent and predictable levels of performance. Given that JavaScript is a highly dynamic language, all of this performance is based on choosing the right heuristics in the engine. JavaScript engines use techniques like speculative optimization and inline caching to speed up execution of the likely path. But heuristics can also easily work against you, and it helps to be aware of them. So today I'm gonna share some background on two subtle performance pitfalls in the V8 JavaScript engine. Being aware of these potential pitfalls might help you to resolve issues if you get bitten by these more easily. Optimization limit 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 potential for further optimizations via doing this, which is commonly referred to as inlining. Contrast this with other runtimes that use so-called tracing JITs - LuaJIT and PyPy are popular examples here - where the unit of optimization is an arbitrary sequence of consecutive bytecodes that have been executed repeatedly previously. In both method JITs as well as tracing JITs, there's always an upper limit on the size of the input th