---
title: "A chain of lost bets"
url: "https://www.mikefluff.com/en/experiments/js-ch07-lost-bets"
locale: "en"
author: "Mikhail Savchenko (Mike Fluff)"
updated: "2026-11-18"
description: "Speculative optimization and deoptimization in V8"
book: "https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems"
alternates:
  - locale: "en"
    url: "https://www.mikefluff.com/en/experiments/js-ch07-lost-bets"
    markdown: "https://www.mikefluff.com/en/experiments/js-ch07-lost-bets.md"
  - locale: "ru"
    url: "https://www.mikefluff.com/ru/experiments/js-ch07-lost-bets"
    markdown: "https://www.mikefluff.com/ru/experiments/js-ch07-lost-bets.md"
  - locale: "pt"
    url: "https://www.mikefluff.com/pt/experiments/js-ch07-lost-bets"
    markdown: "https://www.mikefluff.com/pt/experiments/js-ch07-lost-bets.md"
  - locale: "es"
    url: "https://www.mikefluff.com/es/experiments/js-ch07-lost-bets"
    markdown: "https://www.mikefluff.com/es/experiments/js-ch07-lost-bets.md"
---

# A chain of lost bets

*Speculative optimization and deoptimization in V8*

## What is deoptimization in V8?

Deoptimization is what V8 does when a bet made by its optimizing compiler loses. While a function runs in the interpreter, V8 records the types that pass through each operation. The optimizing compiler builds machine code on the assumption that those types will stay the same and puts a check wherever the assumption can fail. When a check fails, for example an integer sum overflows or an array changes its kind of elements, the machine code is thrown away and execution goes back to the interpreter. Later the function is optimized again, with the wider type information it has collected by then.

An engine like V8 runs a function in the interpreter first and writes down what passes through it. When the function gets hot, the optimizing compiler reads those notes and bets that the future will look like the past: if the array has always held small integers, it adds them with one processor instruction and leaves two checks behind, that the elements are still small integers and that the sum still fits in 31 bits. A failed check is a lost bet, and the engine falls back to the interpreter.

Press the phase button five times. The same sum() gets an array of a thousand small integers, then big integers whose sum overflows 31 bits, then doubles, then strings, then the small integers from the start. Each phase is warmed with 3000 calls, as in the book, and then timed on your own engine. The last button runs a fresh copy of the function on small integers only, for comparison.

In the book, on Node 24, a call cost 0.46–0.50 µs in the first phase, 0.92–1.04 µs after the overflow, 0.79 µs on doubles and 4.5–5.3 µs on strings. V8 logged overflow on the second phase and wrong map on each of the next three. The last phase took 1.83–2.46 µs, four to five times longer than the first on the same data.

The chapter calls this a chain of lost bets. The engine remembers everything the function was given, and after integers, doubles and strings its feedback says the next value could be anything, so the reoptimized code is general and slower. How fast a function runs depends on what it was given before, and the text of the program does not show that.

_The page has an interactive experiment that runs in a browser: https://www.mikefluff.com/en/experiments/js-ch07-lost-bets_

## Sources

- [V8 blog, Elements kinds in V8](https://v8.dev/blog/elements-kinds)
- [V8 blog, Pointer compression in V8](https://v8.dev/blog/pointer-compression)

## From the book

Beside it in the book:

> … A call now takes about two microseconds, four to five times more than at the beginning, although the data is the same. The engine’s table remembers everything that has passed through the function: integers, doubles, strings. The smart compiler reads it and this time plays it safe, building code that is ready for anything, and such code is slower. If you run the last stage in a fresh process, it again takes half a microsecond. The function’s speed now depends on what it was given before, and a deceived engine, like a person let down a few times, takes a long time to restore its former trust.

- Read the chapter: [Chapter 7. Speculative Execution](https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch07) (opening free, rest in the full book; Markdown: https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch07.md)

- Book: [JavaScript: From Closures to Autonomous Systems](https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems)
