Quench.
INSTRUCTIONBUDGETS

blog / · 4 min read · measurement

3 × C + 6: an instruction budget for every JavaScript operation

Every primitive operation in Quench's interpreter now has a ceiling on the machine instructions it may execute, derived from the same operation written in C and enforced by a gate that only lets the number go down.

Wall-clock time is a bad unit for deciding whether a change to an interpreter is good. On our shared build machine the same binary moves by 15 to 20% between runs. A 3% improvement is invisible, and so is a 3% regression, which is worse.

Instructions executed do not have that problem. Under callgrind, with address randomization off, the same binary on the same input gives the same count. So Quench now decides interpreter changes by counting.

The budget

For each primitive operation there is a JavaScript loop that performs it and a C function that does the same thing. The C version is compiled and counted. The budget for the interpreter is then:

budget = 3 × C + 6     (machine instructions per operation)

The 3 says an interpreter may spend about three times what C does on the operation itself: testing a tag, doing the work, storing the result with its tag. The 6 is the fixed cost of fetching and dispatching one bytecode instruction.

Measuring one operation takes a single callgrind run of about 16 seconds. To remove startup and loop overhead, the harness runs the loop for n and 2n iterations and takes the difference:

cost per operation = (instructions(2n) − instructions(n)) / n

The first run

55 / 59
operations

over budget at baseline

438
worst case

string compare, budget 18

16 s
to measure

one operation, exactly

Fifty-five of 59 operations were over. The worst offenders:

Interpreter instructions per operation against budget, baseline 17 November 2026measuredbudget (3 × C + 6)string compare43818truthy (int)30424call method29639Math.imul24827call direct22430array store (obj)19115field store12215
Baseline on the trunk at commit 761a91b9b2. Budgets come from the C twin of each operation.

Some of these are startling. Testing whether an integer is truthy, if (n), took 304 instructions. In C that is a compare and a branch.

What 304 instructions of "if (n)" looked like

Integers were falling through to a fully generic conversion. The generic path looked the value up as if it might be an object with a custom conversion, a string, a BigInt, and so on, before concluding that a non-zero integer is true.

The fix handles integers directly, ahead of the generic path.

Instructions per operation, first day of budget worktruthy (int) before304truthy (int) after48.9int multiply (overflow) before285int multiply (overflow) after142Math.imul before221Math.imul after166typeof before143typeof after95
Before and after counts for four operations on 17 November 2026. Several are still above budget; their ceilings ratchet down as they improve.

How the gate works

Budgets live in one file in the repository, one line per operation. A gate step runs every operation under callgrind and fails if any count is above its line.

A new operation starts with its measured count as the ceiling, even if that is ten times the budget. From then on the ceiling can only be lowered. Improve an operation and you lower its line in the same change. Nobody can make an operation slower without the gate failing, and no improvement can be lost later.

This is the same shrink-only idea the project uses for skipped tests, oversized functions and hand-written opcode logic. It turns "we should fix that someday" into a number that can never get worse.

Budgets at every level

Single operations are the bottom of a ladder. The same method applies upward:

LevelUnitReference
Operationsone bytecode handlerC twin, 3 × C + 6
Primitives per tierone language feature, in each tierinterpreter ≥ b-jit ≥ u-jit, u-jit within 5x of C
Kernelsa small program such as nbodyC program, instructions per iteration
Benchmarksa JetStream 3 testbest of Node and Bun

At the kernel level the gaps are larger and more interesting. A regular-expression kernel ran 1,371 instructions per iteration against 40 in C. A string-building kernel ran 1,332 against 14. The nbody pair computation ran 1,479 against 21 in C. Those have ceilings now too.

Where instruction counts mislead

Instructions are not time, and it would be dishonest to pretend otherwise.

On one SHA-256 benchmark Quench came out 1.72 times better than Bun by instruction count and only 1.26 times better by wall clock. The difference was the instruction cache: Quench took 26.6 million first-level cache misses where Bun took 7.5 million. The interpreter was executing fewer instructions spread over more code.

So the rule has two parts. Instruction counts decide whether an interpreter change is kept, because they are exact. Wall-clock time, measured as the median of interleaved pinned runs, decides whether we claim anything in public. And code layout is treated as part of the result, not as noise.

What it found on the first day

The budget run also turned up a real bug. With a forced collection between measurements, a captured variable in three of the tests read back as the wrong kind of value. It reproduced on the trunk with the JIT off. Counting instructions had nothing to do with it, except that it made us run 59 small programs in a way nobody had run them before.

That is the usual pattern with measurement. You build it to answer one question and it answers a different one first.

Try it

Quench builds from source with Cargo and runs TypeScript, JavaScript and WebAssembly.

Build and run

Work on it

Interpreter, JIT, AOT, GC, Node APIs. Every claim on this blog has a measurement you can rerun.

Where to start

Follow it

New posts land here as the engine moves. Star the repository or subscribe to the feed.

Star on GitHub Feed