Hazel reports · ^report(60) · Hazel’s speed · 2026-11-20, revised 2026-11-20 14:01 MST · work in progress · after ^report(54)
How Fast Is Hazel? Five Games, Three Ways
Cyrus asked how fast Hazel is: interpreted, compiled by hazel-jit, and against plain JavaScript. Five games from the Hazel activities answer it here. Each game’s rules and strategy are a Hazel program, run three ways: interpreted by hazeljs, compiled ahead of time by hazel-jit, and rewritten in plain JavaScript, a version written to give the same answers in the same order. All three are timed on one machine, operation by operation and over whole games that Hazel plays against itself.
Geometric means over the five games of each game’s own geometric mean over its operations, measured in Node on one GitHub runner (how). The spread inside each game is wide: see the figures.
Outline#
- Every operation, three ways: the 39 operations of the five games, on one scale.
- How much faster, game by game: compiled over interpreted, and plain JavaScript over compiled.
- Where hazel-jit’s time goes: its step budget, its exact integers, the page boundary, and the shape of the program itself.
- What hazel-jit’s code pays over plain JavaScript: the overheads one by one, each with its own link: what it is, an example, whether it is measured, and what would remove it.
- The games, one by one: each strategy, where randomness enters it, what its work grows with, and its numbers.
- The same answers, three ways: how we know the three versions play the same game.
- What next, and how this was put together.
Every operation, three ways#
Compiled by hazel-jit, the five games’ rules run about 3,300 times as fast as hazeljs interprets them; written in plain JavaScript, the same games run about 13 times as fast again. So plain JavaScript is about 45,000 times as fast as Hazel’s interpreter on these games, and hazel-jit closes all but a factor of about 13 of that distance. Operation by operation the factors spread: compiled over interpreted from 841× (opening a cell on an expert Minesweeper board) to 10,082× (listing Solitaire’s legal moves), and plain JavaScript over compiled from 2× (dealing a Mahjong hand) to 137× (opening that Minesweeper cell).
hazeljs, interpreting the Hazel (build 1) hazel-jit, the Hazel compiled (build 2) plain JavaScript (build 3)
How much faster, game by game#
Compiled over interpreted varies little between the games, from about 2,000× (Minesweeper) to 5,300× (Go); plain JavaScript over compiled varies much more, from 4.6× (Mahjong) to 26× (Minesweeper). Interpreting costs about the same per step of work in every game, so compiling gains about the same everywhere. How much more plain JavaScript gains depends on what each program does with its data: least where the work is small lists copied as they change, as Mahjong’s tiles are, most where a JavaScript array or bit operation replaces a walk along a list, as on a Minesweeper board (where hazel-jit’s time goes).
Where hazel-jit’s time goes#
Taking out hazel-jit’s step budget, and making its arithmetic plain, saves at most about a quarter of its time: most of the distance to plain JavaScript is elsewhere. Without the step check in every function, the games ran in 0.76 (Mahjong) to 1.04 (Checkers, within the noise) of hazel-jit’s time; with plain-number arithmetic as well, in 0.77 to 0.91.
compiled.js only for this measurement. The answers stay the same, as each table row checks.Every compiled function starts by counting a step. hazel-jit keeps the interpreter’s step limit: each compiled function begins with if (--$R.fuel < 0 || (($R.fuel & 16383) === 0 && performance.now() > $R.deadline)) $R.out();, so a run that loops can be stopped, as the interpreter’s can (^report(54)). Build 2a takes that line out of every function.
Every +, -, * and / on Hazel’s Ints is a call into hazel-jit’s runtime. $R.iadd, $R.isub, $R.imul and $R.idiv keep Hazel’s integers exact past 253, where JavaScript’s numbers stop being exact, by switching to a BigInt there, as the interpreter’s integers are exact (^ticket(757)). Build 2b replaces them with plain number arithmetic, which these games can stand: their numbers stay far below 253.
Most of the rest is the program’s shape: what Hazel gives a program to compute with, and how hazel-jit passes values around. Hazel has lists and no arrays, numbers and no bit operations, and no way to change a value in place. So the Hazel finds the stone at a point of the Go board by walking the rows to its row and along it; tests whether a Checkers square is in a set by dividing the set by a power of two, a power it finds by comparisons; and changes one cell of a Minesweeper board by building a new row and a new list of rows. The plain JavaScript indexes an array, shifts a number, and copies only what changed. hazel-jit adds costs of its own: every function takes one argument, so a call with two makes an array to hold them (has$16([b$19, k$20])), and every labeled tuple is an array whose labels are then set on it as properties, one by one ($R.tup). Removing those would take a compiler change, not a text replacement, so this report does not measure them apart (What next, steps 2 and 3).
Converting the game between the page’s values and hazel-jit’s, on every call, costs little for a game made of a few numbers, and a lot for a big board: 0.2 µs for Checkers, 40 µs for an expert Minesweeper board, most of the 46 µs that opening one of its cells costs compiled. The page holds the game as hazeljs gives it, JavaScript arrays and objects; hazel-jit’s code holds lists as chains of {h, t} cells and labeled tuples as arrays carrying their labels. compiled.js’s adapters convert the whole game in, and the answer out, on every call (the boundary’s own rows in each game’s table). Opening a Minesweeper cell, where plain JavaScript is 137 times as fast, is mostly this: the board, 480 cells of four fields each, built twice for one cell’s change.
What hazel-jit’s code pays over plain JavaScript#
Twelve things the compiled code does that the plain JavaScript does not: seven are hazel-jit’s, in the code it writes for any Hazel program; four are Hazel’s, in what a program can say; and one is the page’s, at the boundary between them. Three are measured here (the step budget, exact integers with their division, and the boundary); the others are named from the compiled code, with what would remove each. Each item has its own link.
The examples are from checkers/compiled.js (build 2, d15466c7b1), as hazel-jit wrote them.
In the code hazel-jit writes#
- A step counted at every call.# Every compiled function starts with
if (--$R.fuel < 0 || (($R.fuel & 16383) === 0 && performance.now() > $R.deadline)) $R.out();: a decrement and a test on every call, and a read of the clock every 16,384 calls, so that a run that loops forever can be stopped, as the interpreter’s can. Measured (build 2a): without it, the games ran in 0.76 (Mahjong) to 1.04 (Checkers) of hazel-jit’s time. To remove: count steps only in functions that can call themselves, or check once per loop instead of once per call. - Exact integers: every
+,-and*a call.#a + bon Hazel’sInts compiles to$R.iadd(a, b): a call that checks that both are numbers, adds, checks the sum is within ±253, and switches to aBigIntpast that, so Hazel’s integers stay exact as the interpreter’s are. The plain JavaScript writesa + b. Measured with the next item and the step budget also out (build 2b): the games ran in 0.77 to 0.91 of hazel-jit’s time. To remove: plain arithmetic where hazel-jit can show the numbers stay small (a loop’s counter, a board’s index), with the exact path kept for the rest. - Division with a zero check, and no remainder.#
a / bcompiles to$R.idiv(a, b): a check thatbis not zero (Hazel reports dividing by zero as an error, and the compiled run must stop where the interpreter would), then(a - a % b) / bto round toward zero exactly. Hazel has no remainder operator, so the Checkers rules test a bit withq - (q / 2) * 2 == 1: four runtime calls ($R.idiv,$R.imul,$R.isub,$R.eq) where JavaScript writesq & 1. Measured with the item above, in build 2b. - Equality as a call.#
==compiles to$R.eq(a, b), a call that compares lists, tuples and constructors by their contents. For two numbers it returns at its first test (a === b), but the call is made every time. The plain JavaScript writes===. Not measured apart. To remove: compile==onInts,Bools andStrings, whose types hazel-jit knows, to===. - Every field read a call.#
g.blackcompiles to$R.dot(g, "black"): a call that checks whethergis an array, reads the property, and, for a list, maps the read over its elements (Hazel lets a projection through a list read every row’s field). The Checkers rules make 48 such reads in their text, Mahjong’s 212, each a call every time it runs. The plain JavaScript readsg.black. Not measured apart. To remove: where the type says the value is a record (it does, everywhere in these games), read the property directly. - A new array for every call’s arguments.# A Hazel function takes one argument, a tuple when it has several, so
has(b, k)compiles tohas$16([b$19, k$20]): an array made for the call, and read back out in the callee (a$18[0],a$18[1]). The plain JavaScript passes two parameters, and makes nothing. Not measured apart. To remove: compile a function whose argument is a tuple pattern to a JavaScript function of that many parameters (What next, step 3). - Records built as arrays, then labelled.# A labeled tuple, such as Checkers’
(black = …, white = …, kings = …), compiles to$R.tup([…], ["black", "white", "kings", …]): an array whose labels are then set on it as properties, one by one. Every game value the rules return is built this way, a new one for each move. The plain JavaScript writes an object literal, which the engine builds in one step and keeps in one shape. Not measured apart. To remove: build a record as an object literal with its labels, and its positions where a program reads them.
In what a Hazel program can say#
- Lists, not arrays.# Hazel has lists and no arrays, and hazel-jit makes each list a chain of
{h, t}cells. Reaching the n-th element walks n cells: the Go rules find the stone at a point by walking to its row and along it, about as many steps as the point is from the corner, where the plain JavaScript reads one array element. Building a list makes a cell for each element, and@copies its left list ($R.append). - A new copy for every change.# A Hazel value cannot be changed in place, so changing one Minesweeper cell builds a new row up to that cell and a new list of rows up to that row; every move in every game builds a new game. The plain JavaScript copies only what it must, and writes into its own copies.
- No bit operations.# Checkers keeps each side’s pieces as a 32-bit set, and Hazel has no shifts or masks, so the rules test a square by dividing the set by a power of two, which they find by comparisons (
bit(k)), then take the remainder by hand (above). The plain JavaScript shifts and masks:(b >>> k) & 1. - Helpers made again at every call.# A function defined inside another is a new closure each time the outer one runs. In hazeljs a module’s every top-level definition deepens the build’s stack, and a browser’s stack is short (
^ticket(791)), so the games keep their helpers inside the functions that use them: Mahjong’swin_ofmakes its dozen helpers again on every call, and it is called for every seat at every discard. The plain JavaScript defines each helper once. hazel-jit could hoist a helper that uses none of its outer function’s variables.
At the page’s edge#
- The game converted in and out at every call.# The page holds the game as hazeljs gives it, JavaScript arrays and objects, and
compiled.js’s adapters convert it to hazel-jit’s own values and the answer back, on every call. Measured on its own rows: 0.2 µs for Checkers’ few numbers, 1.4 µs for a Go board, 4 µs for a Solitaire game, and 40 µs for an expert Minesweeper board, most of the 46 µs that opening one of its cells costs compiled. The plain JavaScript works on the page’s own values. To remove: keep hazel-jit’s value of the game between calls and convert only what the page draws, or let hazel-jit take JavaScript arrays as its lists.
The games, one by one#
For each game: what the program decides, where randomness enters, what makes its work grow, and its numbers. Randomness in all five works the same way: Hazel has no random numbers of its own, so the page passes them in, a list of numbers drawn by JavaScript’s Math.random() as an argument. That makes every function a pure function of its arguments. Seeded numbers make whole games repeat exactly, which is how all three versions were timed playing the same games, move for move.
Solitaire (Klondike)#
Compiled, Solitaire’s rules run about 3,700 times as fast as interpreted, and plain JavaScript about 22 times as fast again: listing every legal move takes 160 ms interpreted, 16 µs compiled, 0.9 µs in plain JavaScript.
The program. Three modules (Cards, Deal, Moves; 244 lines with its vocabulary): the deck, the deal, every legal move in a fixed order, a move made, and the win. There is no opponent: the program’s work is the rules.
Randomness. Only the deal: 52 numbers from the page, each taking a card from those left at the place it falls. Every move after the deal is the player’s.
What the work grows with. Listing every legal move tries each card that can move against each of the seven piles and its foundation: the waste’s top card, each run of face-up cards in each pile, and each foundation’s top card, so about (1 + the face-up cards) × 8 checks. The Hazel walks lists to reach the n-th pile or foundation. The deal takes each card from the list of those left, walking it to the card’s place.
| Operation | Position | hazeljs | hazel-jit | no step budget | and plain numbers | plain JS | hazeljs ÷ hazel-jit | hazel-jit ÷ plain JS |
|---|---|---|---|---|---|---|---|---|
| a deal | a new game | 71 ms | 0.013 ms | 0.013 ms | 0.012 ms | 2.29 µs | 5,317× | 5.8× |
| every legal move | 25 moves in | 160 ms | 0.016 ms | 0.015 ms | 0.015 ms | 0.931 µs | 10,082× | 17× |
| a move made | 25 moves in | 11 ms | 7.34 µs | 6.42 µs | 6.40 µs | 0.194 µs | 1,450× | 38× |
| the moves from a pile | 25 moves in | 11 ms | 4.54 µs | 4.25 µs | 4.30 µs | 0.075 µs | 2,322× | 60× |
| the page boundary: the game in and out | 25 moves in | — | 4.09 µs | hazel-jit only: the game from hazeljs's shapes into hazel-jit's and back, which every call through the page's functions pays | ||||
| Whole game | Moves | hazeljs | hazel-jit | no step budget | and plain numbers | plain JS | Same end |
|---|---|---|---|---|---|---|---|
| 300 random moves | 300 | 66.2 s | 9.4 ms | 9.5 ms | 9.2 ms | 1.8 ms | yes |
Minesweeper#
The hint on an expert board takes 0.58 s interpreted, 0.23 ms compiled and 5 µs in plain JavaScript; Hazel sweeping a whole intermediate board, 115 moves, takes 122 s interpreted, 35 ms compiled and 4 ms in plain JavaScript.
The program. Three modules (Board, Play, Hint; 212 lines with its vocabulary): the mines laid after the first tap (never in its 3 by 3), each cell’s count, a cell opened (and the empty region around it), flags, a number’s neighbours opened at once, and the 💡 hint: a sure move worked out from the open numbers alone. 🤖 plays the hint’s sure moves one after another.
Randomness. The mines: one number from the page for each cell outside the first tap’s 3 by 3, by Knuth’s selection sampling, so exactly the mines asked for, each place as likely as any other. The hint is deterministic: the same board always gives the same sure move. When no number shows a sure move, the hint says so, and 🤖 guesses a closed cell picked at random by the page, not by Hazel, whose rules have no guessing.
What the work grows with. The board’s size and how much of it is open. The hint reads the board once, row by row, making each open cell with closed neighbours a number to reason from. It makes two passes over those numbers to find the mines they force, checking each number’s closed cells against the mines known so far, a list it walks. Then it looks for a number whose mines are all known. So its work grows with the numbers × their cells × the mines known, and so with the board’s size. Opening an empty cell sweeps the whole board, again and again, until no more cells open.
| Operation | Position | hazeljs | hazel-jit | no step budget | and plain numbers | plain JS | hazeljs ÷ hazel-jit | hazel-jit ÷ plain JS |
|---|---|---|---|---|---|---|---|---|
| the mines laid | beginner, 9 by 9 | 83 ms | 0.028 ms | 0.025 ms | 0.023 ms | 2.64 µs | 2,953× | 11× |
| a cell opened | beginner, 9 by 9 | 7.6 ms | 7.61 µs | 7.56 µs | 7.55 µs | 0.121 µs | 1,004× | 63× |
| the hint (a sure move) | beginner, 9 by 9 | 122 ms | 0.031 ms | 0.030 ms | 0.027 ms | 4.31 µs | 3,966× | 7.1× |
| the mines laid | intermediate, 16 by 16 | 290 ms | 0.090 ms | 0.082 ms | 0.075 ms | 8.85 µs | 3,242× | 10× |
| a cell opened | intermediate, 16 by 16 | 20 ms | 0.022 ms | 0.022 ms | 0.022 ms | 0.219 µs | 921× | 101× |
| the hint (a sure move) | intermediate, 16 by 16 | 363 ms | 0.133 ms | 0.130 ms | 0.118 ms | 8.66 µs | 2,740× | 15× |
| the mines laid | expert, 16 by 30 | 504 ms | 0.173 ms | 0.155 ms | 0.142 ms | 0.017 ms | 2,911× | 10.0× |
| a cell opened | expert, 16 by 30 | 39 ms | 0.046 ms | 0.040 ms | 0.041 ms | 0.336 µs | 841× | 137× |
| the hint (a sure move) | expert, 16 by 30 | 583 ms | 0.227 ms | 0.237 ms | 0.161 ms | 5.11 µs | 2,568× | 44× |
| the page boundary: the game in and out | beginner, 9 by 9 | — | 6.90 µs | hazel-jit only: the game from hazeljs's shapes into hazel-jit's and back, which every call through the page's functions pays | ||||
| the page boundary: the game in and out | intermediate, 16 by 16 | — | 0.021 ms | hazel-jit only: the game from hazeljs's shapes into hazel-jit's and back, which every call through the page's functions pays | ||||
| the page boundary: the game in and out | expert, 16 by 30 | — | 0.040 ms | hazel-jit only: the game from hazeljs's shapes into hazel-jit's and back, which every call through the page's functions pays | ||||
| Whole game | Moves | hazeljs | hazel-jit | no step budget | and plain numbers | plain JS | Same end |
|---|---|---|---|---|---|---|---|
| Hazel sweeping a board, beginner, 9 by 9 | 15 | 2.76 s | 1.1 ms | 1.1 ms | 1.0 ms | 1.1 ms | yes |
| Hazel sweeping a board, intermediate, 16 by 16 | 115 | 121.7 s | 35 ms | 36 ms | 32 ms | 4.1 ms | yes |
| Hazel sweeping a board, expert, 16 by 30 | 252 | — | 228 ms | 222 ms | 214 ms | 14 ms | yes |
Go#
Hazel’s choice of move takes 0.59 to 0.91 s interpreted, 0.10 to 0.16 ms compiled, and 8 to 16 µs in plain JavaScript; a whole 9 by 9 game of Hazel against itself, 89 moves, takes 71 s, 17 ms and 5.5 ms. Go gains the most from compiling (5,300×), and plain JavaScript gains 10× more.
The program. One module (Go.hz; 235 lines with its vocabulary): the board as rows of points, a group of stones and its liberties, a move (its captures, no suicide, the ko), a pass, Tromp and Taylor’s area score, and the opponent.
The strategy. Hazel values every empty point as its move, in tenths of a point: what the move takes (20 a stone), saves (15 a stone of its own groups in atari), threatens (6 a stone left in atari) and risks (−12 a stone of a group left with one liberty), its liberties, and where it is (the third line best, the edge worst). It plays the best one, or passes when none is worth more than −3 points, or when the other side has passed and it is ahead.
Randomness. Each point’s worth gets one of the page’s random numbers added before the best is picked, drawn fresh for every move, from 0 up to the most set by the Go page’s randomness slider (2 points by default; 0 to 10). It varies Hazel’s play, breaks ties, and can tip a choice between moves within that many points of each other, or tip a weak move over the −3 point floor where Hazel would have passed. At 0 Hazel plays its best move every time: two games it plays against itself at 0 come out the same, stone for stone.
What the work grows with. The points on the board, for each move. Hazel traces the board’s groups once a turn, then values each of the 81 points (49 on 7 by 7), looking up the groups beside it. A capture is valued by playing it on a copy of the board. In the Hazel, the stone at a point is found by walking the rows to its row, then along that row, so each lookup costs about as many steps as the point is far from the corner. The plain JavaScript reads the stone at a point from an array, in one step. Counting the score floods each empty region.
| Operation | Position | hazeljs | hazel-jit | no step budget | and plain numbers | plain JS | hazeljs ÷ hazel-jit | hazel-jit ÷ plain JS |
|---|---|---|---|---|---|---|---|---|
| a move played | 6 moves in | 17 ms | 4.49 µs | 4.15 µs | 3.83 µs | 1.47 µs | 3,885× | 3.1× |
| Hazel's choice of move | 6 moves in | 588 ms | 0.105 ms | 0.093 ms | 0.077 ms | 7.63 µs | 5,611× | 14× |
| the score | 6 moves in | 755 ms | 0.138 ms | 0.110 ms | 0.105 ms | 5.69 µs | 5,452× | 24× |
| a move played | 40 moves in | 188 ms | 0.029 ms | 0.026 ms | 0.023 ms | 3.89 µs | 6,370× | 7.6× |
| Hazel's choice of move | 40 moves in | 913 ms | 0.162 ms | 0.152 ms | 0.166 ms | 0.016 ms | 5,638× | 10× |
| the score | 40 moves in | 260 ms | 0.048 ms | 0.043 ms | 0.039 ms | 3.71 µs | 5,462× | 13× |
| the page boundary: the game in and out | 6 moves in | — | 1.39 µs | hazel-jit only: the game from hazeljs's shapes into hazel-jit's and back, which every call through the page's functions pays | ||||
| the page boundary: the game in and out | 40 moves in | — | 1.40 µs | hazel-jit only: the game from hazeljs's shapes into hazel-jit's and back, which every call through the page's functions pays | ||||
| Whole game | Moves | hazeljs | hazel-jit | no step budget | and plain numbers | plain JS | Same end |
|---|---|---|---|---|---|---|---|
| Hazel against Hazel on 7 by 7 | 42 | 17.2 s | 5.8 ms | 4.9 ms | 4.6 ms | 0.687 ms | yes |
| Hazel against Hazel on 9 by 9 | 89 | 71.2 s | 17 ms | 16 ms | 15 ms | 5.5 ms | yes |
Checkers (English draughts)#
Looking 4 moves ahead, Hazel’s move takes 6.2 s interpreted, 2.2 ms compiled and 0.14 ms in plain JavaScript, 2,800× and 16×; the factors hold at every depth, since the three search the same tree, each move further ahead about 3 times as much work.
The program. One module (Checkers.hz; 141 lines with its vocabulary): each side’s pieces as a set of the 32 dark squares kept in a number’s bits, the moves (jumps are compulsory and go on while they can), kings, the end (no move loses, 80 moves with no capture and no man moving draws), and the opponent.
The strategy. Alpha-beta search: Hazel tries each of its moves, then each answer, and so on, as many moves ahead as chosen, valuing the position at the end by material (a man 100, and 2 for each row it has come; a king 160) for the side to move.
Randomness. Ties only. The page passes 48 numbers, one for each move tried at the top. Of the moves with the best value, the one paired with the largest number is played. The search itself is deterministic.
What the work grows with. The search’s depth. With about 8 moves a position, looking one more move ahead costs several times as much, fewer than 8 because alpha-beta cuts off branches that cannot matter. The compiled times grow from 0.046 ms looking 1 ahead to 18.6 ms looking 6 ahead, at the position 8 moves in. Each position’s moves are found from the sets of squares. The Hazel has no bit operations, so it tests a square by dividing the set by a power of two, where the plain JavaScript shifts and masks.
| Operation | Position | hazeljs | hazel-jit | no step budget | and plain numbers | plain JS | hazeljs ÷ hazel-jit | hazel-jit ÷ plain JS |
|---|---|---|---|---|---|---|---|---|
| the moves | the start | 37 ms | 0.012 ms | 0.012 ms | 8.89 µs | 0.572 µs | 3,049× | 21× |
| the moves | 8 moves in | 37 ms | 0.012 ms | 0.012 ms | 9.66 µs | 0.607 µs | 3,031× | 20× |
| the moves | 30 moves in | 27 ms | 9.09 µs | 9.11 µs | 7.43 µs | 0.569 µs | 2,933× | 16× |
| Hazel's move, looking 1 ahead | 8 moves in | 130 ms | 0.046 ms | 0.049 ms | 0.039 ms | 1.75 µs | 2,815× | 26× |
| Hazel's move, looking 1 ahead | 30 moves in | 109 ms | 0.039 ms | 0.042 ms | 0.034 ms | 1.55 µs | 2,810× | 25× |
| Hazel's move, looking 2 ahead | 8 moves in | 635 ms | 0.226 ms | 0.237 ms | 0.187 ms | 0.013 ms | 2,812× | 18× |
| Hazel's move, looking 2 ahead | 30 moves in | 498 ms | 0.180 ms | 0.187 ms | 0.152 ms | 0.010 ms | 2,759× | 18× |
| Hazel's move, looking 3 ahead | 8 moves in | 2.12 s | 0.729 ms | 0.762 ms | 0.604 ms | 0.045 ms | 2,907× | 16× |
| Hazel's move, looking 3 ahead | 30 moves in | 1.86 s | 0.649 ms | 0.689 ms | 0.559 ms | 0.044 ms | 2,856× | 15× |
| Hazel's move, looking 4 ahead | 8 moves in | 6.20 s | 2.2 ms | 2.2 ms | 1.7 ms | 0.139 ms | 2,856× | 16× |
| Hazel's move, looking 4 ahead | 30 moves in | — | 2.3 ms | 2.3 ms | 1.8 ms | 0.156 ms | — | 15× |
| Hazel's move, looking 5 ahead | 8 moves in | — | 6.1 ms | 6.7 ms | 5.2 ms | 0.428 ms | — | 14× |
| Hazel's move, looking 5 ahead | 30 moves in | — | 6.9 ms | 7.2 ms | 5.9 ms | 0.407 ms | — | 17× |
| Hazel's move, looking 6 ahead | 8 moves in | — | 19 ms | 20 ms | 16 ms | 1.5 ms | — | 12× |
| Hazel's move, looking 6 ahead | 30 moves in | — | 20 ms | 21 ms | 18 ms | 1.6 ms | — | 13× |
| the page boundary: the game in and out | the start | — | 0.141 µs | hazel-jit only: the game from hazeljs's shapes into hazel-jit's and back, which every call through the page's functions pays | ||||
| the page boundary: the game in and out | 8 moves in | — | 0.224 µs | hazel-jit only: the game from hazeljs's shapes into hazel-jit's and back, which every call through the page's functions pays | ||||
| the page boundary: the game in and out | 30 moves in | — | 0.221 µs | hazel-jit only: the game from hazeljs's shapes into hazel-jit's and back, which every call through the page's functions pays | ||||
| Whole game | Moves | hazeljs | hazel-jit | no step budget | and plain numbers | plain JS | Same end |
|---|---|---|---|---|---|---|---|
| Hazel against Hazel, looking 2 ahead (to 60 moves) | 60 | 24.7 s | 9.8 ms | 11 ms | 9.9 ms | 0.855 ms | yes |
| Hazel against Hazel, looking 4 ahead (a whole game) | 185 | — | 760 ms | 825 ms | 697 ms | 58 ms | yes |
| Hazel against Hazel, looking 6 ahead (a whole game) | 213 | — | 5.38 s | 5.38 s | 4.54 s | 385 ms | yes |
Mahjong (Hong Kong rules)#
Mahjong is where plain JavaScript gains least, 4.6 times over compiled: a Hazel player’s turn takes 63 ms interpreted, 23 µs compiled and 2.3 µs in plain JavaScript, and the claims on a discard 60 ms, 21 µs and 9 µs. Eight hands of Hazel playing all four seats take 36 s, 20 ms and 10 ms.
The program. One module (Mahjong.hz; 316 lines with its vocabulary): the deal, a turn (a draw and a discard), the claims on a discard (a chow, a pung, a kong, a win) and which comes first, a winning hand and its faan, the points, and what each Hazel player discards and claims. 🤖 plays your seat by the step’s kind 9, as the Hazel players play theirs.
The strategy. A Hazel player wins when it can, declares a kong when it has one, and otherwise discards the tile that helps its hand least: its other copies, its neighbours in its suit, and a little for the middle of the suit or a scoring honour. On another seat’s discard it claims a win, a kong of three, a pung of a pair that is an honour or has few neighbours, or, as the next seat, a chow that breaks no pair.
Randomness. Only the shuffle: 136 numbers from the page, one a tile as its sort key. The players are deterministic: the same table always gives the same discard and the same claims.
What the work grows with. Whether a hand wins, asked again and again. Every step on another seat’s discard asks, for each of the three other seats, whether that discard would complete its hand. That means trying every way the hand makes a pair and four sets, a recursive search over pungs and chows. A player’s own turn asks it once more, then weighs each kind of tile in its hand.
| Operation | Position | hazeljs | hazel-jit | no step budget | and plain numbers | plain JS | hazeljs ÷ hazel-jit | hazel-jit ÷ plain JS |
|---|---|---|---|---|---|---|---|---|
| a hand dealt | the first hand | 180 ms | 0.042 ms | 0.039 ms | 0.039 ms | 0.021 ms | 4,255× | 2.0× |
| a Hazel player's turn (a step) | early in the hand | 63 ms | 0.023 ms | 0.014 ms | 0.014 ms | 2.34 µs | 2,777× | 9.7× |
| your options | your turn | 28 ms | 9.39 µs | 6.67 µs | 7.14 µs | 1.60 µs | 2,962× | 5.9× |
| your turn, played by Hazel (a step) | your turn | 62 ms | 0.019 ms | 0.015 ms | 0.016 ms | 2.46 µs | 3,246× | 7.8× |
| the claims on a discard (a step) | another's discard | 60 ms | 0.021 ms | 0.016 ms | 0.016 ms | 8.80 µs | 2,890× | 2.4× |
| the page boundary: the game in and out | early in the hand | — | 2.62 µs | hazel-jit only: the game from hazeljs's shapes into hazel-jit's and back, which every call through the page's functions pays | ||||
| Whole game | Moves | hazeljs | hazel-jit | no step budget | and plain numbers | plain JS | Same end |
|---|---|---|---|---|---|---|---|
| Hazel playing all four seats, one hand | 101 | 4.62 s | 5.6 ms | 4.5 ms | 5.2 ms | 2.1 ms | yes |
| Hazel playing all four seats, eight hands | 746 | 35.6 s | 20 ms | 22 ms | 23 ms | 10 ms | yes |
The same answers, three ways#
The three versions of each game give the same answers, value for value, and in the same order where the order matters: the moves listed, the move chosen, the faan counted. Two Node scripts in hazeljs-examples checked it before anything was timed. node/compare-compiled.mjs plays seeded games and compares every answer of the compiled rules with hazeljs’s, and, with --plain, with the plain JavaScript’s:
| Game | hazel-jit against hazeljs | plain JS against hazel-jit | What the plain check covers |
|---|---|---|---|
| Checkers | 469 answers, 0 differ | 8,969 answers, 0 differ | 30 random games: every position’s moves, Hazel’s choice looking 1 and 2 ahead (3 and 4 every tenth move), each move made |
| Solitaire | 722 answers, 0 differ | 48,040 answers, 0 differ | 40 deals, drawing one and three: every position’s moves, the moves from every place, each move made, the win |
| Minesweeper | 571 answers, 0 differ | 15,137 answers, 0 differ | 90 boards at the three levels: every hint (all four kinds of answer, 125 of them “take this flag off”), each move it names, random flags kept, chords |
| Go | 157 answers, 0 differ | 5,160 answers, 0 differ | 20 games of Hazel against itself on 9 by 9: every choice, a random point played (legal or not), each move or pass, the score every tenth turn |
| Mahjong | 1,719 answers, 0 differ | 15,386 answers, 0 differ | 80 hands: every step and your options, with you discarding at random, making claims, or played by Hazel; every third deal with tied shuffle keys |
Then the timing script checked again as it went: each operation’s answer the same all three ways (and both variants), and each whole game ending in the same state. None differed: every operation’s answer was the same all five ways (the three builds and the two variants), and every whole game ended in the same state.
The plain JavaScript keeps the Hazel’s algorithms and its order, not its data structures. Where the Hazel walks a list, the JavaScript indexes an array. Where it divides by a power of two, the JavaScript shifts. Where it looks a value up in a list, the JavaScript uses a Set or a Map. A JavaScript programmer writing the same game would make those choices, and Matthew asked for the JavaScript to be equivalent in the game, its logic and its strategy. Two places keep Hazel’s exact order where a simpler JavaScript would differ. Mahjong’s shuffle sorts the keys with the Hazel’s own merge sort, whose halves are taken alternately and reversed, so tied keys fall the same way. Minesweeper’s hint keeps the order in which the Hazel lists each number’s closed neighbours, because the first sure move found is the one it names.
What next#
- Time every hazel-jit change this way. The workflow
three-way timingin hazeljs-examples runs on every change to the rules, the compiled programs or the plain JavaScript. Run against each new hazel-jit commit, its results would chart how the distance to plain JavaScript closes, as Matthew put it: “now, and in the future, as the hazel components become better in performance”. - Split the program’s shape from the compiler. A third JavaScript version of one game, written the Hazel’s way (lists of cons cells, division for bits, a tuple for each call), would show how much of the gap to plain JavaScript is Hazel’s vocabulary and how much is hazel-jit’s code. Checkers and Go are the clearest cases.
- Calls without a tuple. Every multi-argument call in the compiled code allocates an array for its arguments and reads them back out. Compiling a known function of n arguments to a JavaScript function of n parameters would remove that allocation.
- A cheaper page boundary for big values. Minesweeper’s board crosses whole on every call, in and out: 40 µs on an expert board, most of what opening a cell costs. A page could keep hazel-jit’s own value of the game between calls and convert only what it draws, or hazel-jit could take JavaScript arrays as its lists.
- The same three ways in a browser, and on a phone. Each page’s ⚙️ Advanced already times its rules, interpreted or compiled. Adding plain JavaScript there would give these numbers on the devices the activities are played on.
How this was put together#
On 2026-11-20, for ^ticket(797). The five games are hazeljs-examples’ own: their rules are the rules/*.hz the pages run, compiled for 0.0.34 (^ticket(794)). The plain JavaScript (<game>/plain.js) was written for this report and is never loaded by the pages.
| Build | What runs the rules | From |
|---|---|---|
| 1 · hazeljs | Hazel’s interpreter, as the pages ran their rules until 0.0.34: the game’s rules/*.hz built by Hazel.runProgram | hazeljs 0.0.4 from npm |
| 2 · hazel-jit | The same Hazel compiled ahead of time to JavaScript by hazel-jit, wrapped by tools/make-compiled.py with adapters for the page’s values (compiled.js) | hazelgrove/hazel trial/794-games-aot: Solitaire 5405a2e87d, Minesweeper 5405a2e87d, Go 5405a2e87d, Checkers d15466c7b1, Mahjong 70d0178872 |
| 2a · no step budget | Build 2 with the step budget’s check taken out of every compiled function | made from build 2 by node/three-way.mjs, for timing only |
| 2b · and plain numbers | Build 2a with Hazel’s exact-integer + - * / replaced by plain number arithmetic | made from build 2a likewise |
| 3 · plain JS | The same game, rules and strategy written in plain JavaScript (plain.js) | hazeljs-examples 40fd48b |
The machine. AMD EPYC 9V74 (4 vCPUs), Node 22.23.4, Ubuntu: one GitHub-hosted runner, all five games one after another in one job, so all builds were timed on the same machine: run 44813544646 of the workflow three-way timing, 2026-11-20, 13:41 to 13:51 MST.
The timing. node/three-way.mjs <game> builds the rules with hazeljs as the page does, loads compiled.js and plain.js, and makes the two variants of compiled.js by text replacement. It plays a seeded game to make each operation’s positions (with the compiled rules; all ways answer the same there). Then it times each operation each way: a call too fast for the clock is run in batches of as many calls as take at least a millisecond, and the median batch, per call, is reported. hazeljs is timed at least 3 times and for at least 1.5 s, the others at least 30 times and for at least 0.3 s, after 20 calls to warm up. Whole games are played from the same seed each way: hazeljs once, the others 5 times, keeping the median. Every operation goes through the same functions the page calls, with the page’s values. For hazel-jit that includes converting them in and out (the page boundary, timed by itself on its own rows).
Not timed in a browser. These are Node numbers, on a server processor. Chrome runs the same JavaScript engine as Node (V8); Firefox and Safari have their own, and on a phone every time will be several times longer. The ratios between the three ways are what this report is about.
The data#
- The numbers, for programs too: every table in this report, and what its numbers measure, is also in this page as JSON, in its
<script type="application/json" id="report-data">element, so a reader or an agent that fetches the page alone has the dataset. The same, with any raw files that survive, is in060/data/.