Hazel reports · ^report(63) · the hazeljs package · 2026-11-23, revised 2026-11-24 11:30 MST · work in progress · after ^report(54) and ^report(60)

Hazel as an npm Package: What to Decide Next

On 2026-11-23 Matthew asked for one PR that tracks everything the hazeljs npm package depends on: Hazel’s editor in the package, working offline, with fonts and icons whose licenses allow it, and hazel-jit, “so that hazel code can be parsed and jitted from the npm package API as well”. That PR is hazelgrove/hazel#2857, and hazeljs 0.0.11 is built from it. This report is for Matthew and Cyrus, to see the design space before it goes to dev: what the package is, the PRs it comes from, how its branch differs from dev, how fast each way of running a program is on every game in hazeljs-examples, and the decisions to make next.

Outline#

  1. What the package is: three ways for a page to run Hazel code, and Hazel’s own editor, offline.
  2. The PRs: one PR holds it all; the parts it came from are still open.
  3. How the branch differs from dev: 8,577 lines added, nearly all in new files, and the move to Jane Street Core.
  4. Every game, four ways: the 17 games’ 45 operations, interpreted, through the jit, compiled ahead of time, and in plain JavaScript.
  5. Where the jit’s time goes: before the converters, on most calls, converting the values in and out cost more than the compiled code itself.
  6. The converters, a day later: the jit 9.0× as fast, and about even with code compiled ahead of time.
  7. A game’s own run of states: played as a page plays them, the jit takes 0.91× the time of code compiled ahead of time, and 0.76× with its memos kept as links on the objects; with nothing remembered, 2.2× its own.
  8. Building the rules: the jit builds faster than the interpreter; ahead of time, nothing is built in the page.
  9. Against plain JavaScript: what hazel-jit’s code still costs over a hand-written game.
  10. What to decide before dev: ten questions, each with its options.
  11. What next for speed, and how this was put together.

What the package is#

hazeljs 0.0.11 gives a page three ways to run a Hazel program, and Hazel’s own editor. The first two come in one file, hazel-lib.js (2.9 MB, Hazel compiled to JavaScript by js_of_ocaml): Hazel.run and runProgram interpret a program; Hazel.jit and jitProgram compile it to JavaScript with hazel-jit in the page and run that. The third needs no Hazel in the page at all: Hazel.compileProgram gives the JavaScript and its runtime once, ahead of time, to keep in a file, which is how every game in hazeljs-examples ships. The editor is Hazel’s web app, built from the same commit, framed by the package’s hazel-listings.js; its fonts and icons are in the package with their licenses, so it loads nothing from another host.

a pageimport "hazeljs"hazel-lib.js, 2.9 MBHazel.run, runProgram: the interpreter runs the programevery game: 0.5 to 6 s to build, then each call interpretedhazel-lib.js, the same fileHazel.jit, jitProgram: compiled to JavaScript in the pageeach call's values cross through Hazel's own, both wayscompiled.js, 32 to 124 kBHazel.compileProgram, once, ahead of time: no Hazel in the pagethe game's values converted by converters written by handeditor/, 68 files, 11 MBHazel's own editor, framed by hazel-listings.jsoffline: its fonts and icons in the package
How a page uses hazeljs 0.0.11. The values a page passes and gets back have the same shapes in the first two ways: a constructor as {tag, value}, a list as an array, a labeled tuple as an object, a function as a JavaScript function. Code compiled ahead of time has hazel-jit’s own shapes inside, and each game converts them with a few lines written by hand.

0.0.11 is pushed on hazelgrove/hazeljs’s editor-in-package branch and is not on npm. Its hazel-lib.js is byte for byte the file Hazel’s own deploy of the same commit serves, and the package’s Verify workflow rebuilds it on GitHub and checks that. Its install checks run on Node 18, 20 and 22, in TypeScript, and in a headless Firefox.

The PRs#

One PR, #2857, holds everything the package is built from; the PRs its parts came from are still open, and none is merged. #2857 merges the library and embed line (#2711, #2719, #2814, stacked on each other), the font (#2837) and the icons (#2842), adds hazel-jit’s compiler, and merges dev. The package’s package.json records the commit, the branch and the PR it was built from.

PRbranchstatelineswhat it gives the package
#2857hazeljs into devdraft+8,577 / −59The one PR: everything below that the package uses, merged with dev and ported to Jane Street Core, plus hazel-jit's compiler and the code font's license.
#2711js-library into devdraft+510 / −3The library, first version: Hazel.analyze and Hazel.run from any page.
#2719js-library-embed into js-librarydraft+4,168 / −109Programs of named modules (analyzeProgram, runProgram), and Hazel in another page's frame.
#2814embed-editable into js-library-embeddraft+948 / −72Embed mode: editable cells, one module at a time, go to definition across cells.
#2837ui-font-installed into devopen+110 / −5IBM Plex Sans as the interface font, in place of a Helvetica Neue that Hazel had no license to ship.
#2842ui-icons-shipped into devopen+222 / −6Material Icons shipped with Hazel, so nothing loads from Google.
#2817trial/694-view-jit into integration/livelits-modular-editorsdraft+15,915 / −1,174hazel-jit on the livelits line: livelit views compiled. #2857's compiler is this one's, from trial/794-games-aot, with the livelit-only forms taken out.
#2828fix/758-sint-32-bits into devdraft+233 / −76SInt with 64 bits everywhere, overflow an error. Not in #2857 yet.

Two more are named in what follows: hazelgrove/hazel#2832, the interpreter’s answer that grows to gigabytes when a program’s answer is a function reaching several modules (why the astrology chart below is interpreted as a whole program each time), and hazelgrove/hazeljs#2, the issue that put the editor in the package (0.0.9).

How the branch differs from dev#

The branch is 50 commits ahead of dev and 0 behind. It changes 41 files, adding 8,577 lines and removing 59: almost all of it new files, and 2,276 lines of it tests. Only 179 added lines touch what dev’s own code does; the rest is new modules and files that dev does not have.

hazel-jit2,215 lines, 2 filesthe library1,088 lines, 5 filesprograms of modules683 lines, 1 filesembed mode1,702 lines, 11 filesfonts, icons, licenses434 lines, 12 fileschanges to dev’s own code179 lines, 4 filestests2,276 lines, 6 files
Lines the hazeljs branch (98c482395d) adds over dev (21da4ae66b), by part. The fonts are binary files and are not counted in lines: two Helvetica Neue files removed (258 kB), two IBM Plex Sans files and Material Icons added (170 kB).
partfileslines addedlines removed
hazel-jit22,2150
the library51,0881
programs of modules16830
embed mode111,7025
fonts, icons, licenses1243411
changes to dev’s own code417932
tests62,27610
all418,57759

What changes for dev’s own app. The interface font is IBM Plex Sans instead of Helvetica Neue, on every page. The icons load from Hazel’s own files instead of from Google. Hazel’s list of error messages can also give each with the id of the term it is on (the library’s errors per module use it). An editor rooted at a module reads its text as a module body, and the fast parser retries a module body whose last member is still being written. In the interpreter, a labeled tuple whose fields arrive in another order than its type’s is put in its type’s order before it runs; only a value made in JavaScript ever arrives that way. Embed mode (the frame, editable cells, go to definition across cells) is reached through #embed and changes nothing in the normal app.

Jane Street Core. dev moved to Core in hazelgrove/hazel#2470 through #2477, and -open Core applies to a whole library at once, so the branch’s own files were ported in the same merge. One difference no type error showed: Core’s List.init calls its function from the last index down, where OCaml’s own went up from 0. The jit finds a compiled tuple’s labels by the value at each index, using each label up as it goes, so in reverse it swapped fields that JavaScript’s == takes as equal: (b = false, a = 0, c = 5) came back as {a: false, b: 0, c: 5}. Comparing Gnome dig’s compiled run with its interpreted run in Node found it; 98c482395d fixes it, with a test. The parts’ own PRs (#2711, #2719, #2814) are still written for OCaml’s standard library.

Every game, four ways#

Through the package’s jit, the 17 games run 317× as fast as through its interpreter (the geometric mean over 45 operations), from 4.2× to 4,955×. Compiled ahead of time, the same code runs 2,392× as fast as the interpreter. The spread is the point. On the 11 operations that are mostly the program’s own work, a robot choosing or Hazel searching for a move, the jit is 1,063× to 4,955× as fast as the interpreter and within 1.7× of code compiled ahead of time. On the other 34, where the call itself is short, it is 4.2× to 1,563× as fast as the interpreter. All four ways gave the same answer on every operation. (These are build 2’s jit numbers; the jit with direct converters, build 5, is in the converters, a day later.)

10 ns1 µs0.1 ms10 ms1 s100 sOraclesI Ching: a cast by coinsI Ching, a cast by coins: interpreted 0.78 msI Ching, a cast by coins: jit 4.1 µsI Ching, a cast by coins: compiled 78 nsI Ching: a cast by yarrowI Ching, a cast by yarrow: interpreted 1.2 msI Ching, a cast by yarrow: jit 5.8 µsI Ching, a cast by yarrow: compiled 337 nsI Ching: a readingI Ching, a reading: interpreted 11 msI Ching, a reading: jit 14 µsI Ching, a reading: compiled 2.2 µsTarot: a deal, OneTarot, a deal, One: interpreted 2.5 msTarot, a deal, One: jit 10 µsTarot, a deal, One: compiled 290 nsTarot: a deal, CelticCrossTarot, a deal, CelticCross: interpreted 30 msTarot, a deal, CelticCross: jit 56 µsTarot, a deal, CelticCross: compiled 6.2 µsTarot: a deal, YearAheadTarot, a deal, YearAhead: interpreted 46 msTarot, a deal, YearAhead: jit 80 µsTarot, a deal, YearAhead: compiled 10 µsAstrology: a chart (interpreted: the whole…Astrology, a chart (interpreted: the whole program built each time): interpreted 990 msAstrology, a chart (interpreted: the whole program built each time): jit 0.97 msAstrology, a chart (interpreted: the whole program built each time): compiled 0.18 msBoard and card games, each with a plain JavaScript versionCheckers: the movesCheckers, the moves: interpreted 20 msCheckers, the moves: jit 15 µsCheckers, the moves: compiled 6.3 µsCheckers, the moves: plain JS 336 nsCheckers: Hazel's move, looking 1 aheadCheckers, Hazel's move, looking 1 ahead: interpreted 34 msCheckers, Hazel's move, looking 1 ahead: jit 27 µsCheckers, Hazel's move, looking 1 ahead: compiled 13 µsCheckers, Hazel's move, looking 1 ahead: plain JS 528 nsCheckers: Hazel's move, looking 2 aheadCheckers, Hazel's move, looking 2 ahead: interpreted 55 msCheckers, Hazel's move, looking 2 ahead: jit 31 µsCheckers, Hazel's move, looking 2 ahead: compiled 23 µsCheckers, Hazel's move, looking 2 ahead: plain JS 1.4 µsCheckers: Hazel's move, looking 3 aheadCheckers, Hazel's move, looking 3 ahead: interpreted 149 msCheckers, Hazel's move, looking 3 ahead: jit 74 µsCheckers, Hazel's move, looking 3 ahead: compiled 65 µsCheckers, Hazel's move, looking 3 ahead: plain JS 3.5 µsCheckers: Hazel's move, looking 4 aheadCheckers, Hazel's move, looking 4 ahead: interpreted 375 msCheckers, Hazel's move, looking 4 ahead: jit 0.18 msCheckers, Hazel's move, looking 4 ahead: compiled 0.17 msCheckers, Hazel's move, looking 4 ahead: plain JS 12 µsGo: a move playedGo, a move played: interpreted 14 msGo, a move played: jit 83 µsGo, a move played: compiled 4.6 µsGo, a move played: plain JS 1.7 µsGo: Hazel's choice of moveGo, Hazel's choice of move: interpreted 446 msGo, Hazel's choice of move: jit 0.17 msGo, Hazel's choice of move: compiled 0.12 msGo, Hazel's choice of move: plain JS 8.5 µsGo: the scoreGo, the score: interpreted 700 msGo, the score: jit 0.20 msGo, the score: compiled 0.17 msGo, the score: plain JS 7.3 µsMinesweeper: 9 by 9: the mines laidMinesweeper, 9 by 9: the mines laid: interpreted 81 msMinesweeper, 9 by 9: the mines laid: jit 0.42 msMinesweeper, 9 by 9: the mines laid: compiled 34 µsMinesweeper, 9 by 9: the mines laid: plain JS 3.6 µsMinesweeper: 9 by 9: a cell openedMinesweeper, 9 by 9: a cell opened: interpreted 10 msMinesweeper, 9 by 9: a cell opened: jit 0.60 msMinesweeper, 9 by 9: a cell opened: compiled 12 µsMinesweeper, 9 by 9: a cell opened: plain JS 474 nsMinesweeper: 9 by 9: the hint (a sure move)Minesweeper, 9 by 9: the hint (a sure move): interpreted 134 msMinesweeper, 9 by 9: the hint (a sure move): jit 0.35 msMinesweeper, 9 by 9: the hint (a sure move): compiled 43 µsMinesweeper, 9 by 9: the hint (a sure move): plain JS 3.4 µsMinesweeper: 16 by 30: the mines laidMinesweeper, 16 by 30: the mines laid: interpreted 442 msMinesweeper, 16 by 30: the mines laid: jit 3.7 msMinesweeper, 16 by 30: the mines laid: compiled 0.20 msMinesweeper, 16 by 30: the mines laid: plain JS 23 µsMinesweeper: 16 by 30: a cell openedMinesweeper, 16 by 30: a cell opened: interpreted 32 msMinesweeper, 16 by 30: a cell opened: jit 7.5 msMinesweeper, 16 by 30: a cell opened: compiled 50 µsMinesweeper, 16 by 30: a cell opened: plain JS 531 nsMinesweeper: 16 by 30: the hint (a sure mo…Minesweeper, 16 by 30: the hint (a sure move): interpreted 576 msMinesweeper, 16 by 30: the hint (a sure move): jit 2.0 msMinesweeper, 16 by 30: the hint (a sure move): compiled 0.22 msMinesweeper, 16 by 30: the hint (a sure move): plain JS 6.6 µsMahjong: a hand dealtMahjong, a hand dealt: interpreted 108 msMahjong, a hand dealt: jit 0.10 msMahjong, a hand dealt: compiled 40 µsMahjong, a hand dealt: plain JS 17 µsMahjong: a stepMahjong, a step: interpreted 1.8 msMahjong, a step: jit 75 µsMahjong, a step: compiled 2.2 µsMahjong, a step: plain JS 8 nsMahjong: your optionsMahjong, your options: interpreted 18 msMahjong, your options: jit 34 µsMahjong, your options: compiled 5.6 µsMahjong, your options: plain JS 1.3 µsSolitaire: a dealSolitaire, a deal: interpreted 41 msSolitaire, a deal: jit 0.12 msSolitaire, a deal: compiled 9.9 µsSolitaire, a deal: plain JS 1.8 µsSolitaire: every legal moveSolitaire, every legal move: interpreted 44 msSolitaire, every legal move: jit 100 µsSolitaire, every legal move: compiled 6.4 µsSolitaire, every legal move: plain JS 332 nsSolitaire: a move madeSolitaire, a move made: interpreted 1.7 msSolitaire, a move made: jit 0.16 msSolitaire, a move made: compiled 3.0 µsSolitaire, a move made: plain JS 92 nsA puzzleMahjong solitaire: a dealMahjong solitaire, a deal: interpreted 16.2 sMahjong solitaire, a deal: jit 15 msMahjong solitaire, a deal: compiled 12 msMahjong solitaire: Hazel's choice of pairMahjong solitaire, Hazel's choice of pair: interpreted 16 msMahjong solitaire, Hazel's choice of pair: jit 3.5 msMahjong solitaire, Hazel's choice of pair: compiled 1.4 µsArcade games, a step at a timePinball: a stepPinball, a step: interpreted 86 msPinball, a step: jit 59 µsPinball, a step: compiled 35 µsPinball: a boardPinball, a board: interpreted 20 msPinball, a board: jit 0.31 msPinball, a board: compiled 35 µsDownhill: a stepDownhill, a step: interpreted 2.9 msDownhill, a step: jit 38 µsDownhill, a step: compiled 4.9 µsDownhill: the robot's choiceDownhill, the robot's choice: interpreted 3.8 msDownhill, the robot's choice: jit 11 µsDownhill, the robot's choice: compiled 1.9 µsBricks: a stepBricks, a step: interpreted 1.9 msBricks, a step: jit 55 µsBricks, a step: compiled 2.9 µsBricks: the robot's choiceBricks, the robot's choice: interpreted 0.47 msBricks, the robot's choice: jit 17 µsBricks, the robot's choice: compiled 648 nsSnake: a stepSnake, a step: interpreted 6.1 msSnake, a step: jit 62 µsSnake, a step: compiled 3.8 µsSnake: the robot's choice (42 long)Snake, the robot's choice (42 long): interpreted 677 msSnake, the robot's choice (42 long): jit 0.14 msSnake, the robot's choice (42 long): compiled 0.16 msLight trails: a stepLight trails, a step: interpreted 17 msLight trails, a step: jit 63 µsLight trails, a step: compiled 6.5 µsLight trails: the robot's choice, looking …Light trails, the robot's choice, looking 150: interpreted 4.1 sLight trails, the robot's choice, looking 150: jit 1.0 msLight trails, the robot's choice, looking 150: compiled 1.0 msStar wave: a stepStar wave, a step: interpreted 27 msStar wave, a step: jit 81 µsStar wave, a step: compiled 14 µsStar wave: the robot's choiceStar wave, the robot's choice: interpreted 68 msStar wave, the robot's choice: jit 37 µsStar wave, the robot's choice: compiled 21 µsButterfly garden: a stepButterfly garden, a step: interpreted 9.2 msButterfly garden, a step: jit 0.23 msButterfly garden, a step: compiled 27 µsButterfly garden: the robot's choiceButterfly garden, the robot's choice: interpreted 224 msButterfly garden, the robot's choice: jit 0.14 msButterfly garden, the robot's choice: compiled 69 µsGnome dig: a stepGnome dig, a step: interpreted 97 msGnome dig, a step: jit 0.36 msGnome dig, a step: compiled 43 µsGnome dig: the robot's choiceGnome dig, the robot's choice: interpreted 3.8 sGnome dig, the robot's choice: jit 1.0 msGnome dig, the robot's choice: compiled 0.88 msinterpretedjitcompiled ahead of timeplain JS
Builds 1 to 4 (not a build of the app: Node 22 on GitHub’s runners, one runner per game). Each time is one call, after the rules were built: building, compiling and loading are not in it (they are here). Each operation of the 17 games, timed four ways: the package’s interpreter (grey), the package’s jit (orange), the package’s compiler ahead of time (blue), and plain JavaScript (green, 5 games). A log scale: each step is a hundred times the one before. Hover a mark for its time.

The jit and ahead of time run the same compiled code; the gap between them is how the values get in and out of it, not compiling, and not the code. Both come from one compiler: Hazel.jitProgram and Hazel.compileProgram run the same analysis and the same compile_elab on a program, and produce the same JavaScript. The jit makes it as the page loads (117 ms to 3.7 s for these games) and the ahead-of-time file was made before, so neither way’s compiling is in a call’s time. What a call does:

So where a call works long on a small value (a robot choosing, Hazel searching for a move), the conversions are a small part of it and the two ways come within 1.7× of each other; where a call is short and its value large (an expert Minesweeper board of 480 cells, for one opened cell), the conversions are most of the jit’s time. Where the jit’s time goes splits each operation that way, and the converters, a day later times the jit with the conversion made direct (build 5).

For one game, Gnome dig: a step of the game takes 97 ms interpreted, 0.36 ms through the jit and 43 µs ahead of time; the robot choosing its move takes 3.8 s, 1.0 ms and 0.88 ms. At 60 frames a second a frame has 16.7 ms, so interpreted, one step is six frames and the robot’s choice almost four seconds; through the jit, both fit in a frame many times over.

Every operation’s numbers
operationinterpretedjitahead of timeplain JSjit over interpretedahead of time over jit
Oracles
I Ching: a cast by coins0.78 ms4.1 µs78 ns–189×53×
I Ching: a cast by yarrow1.2 ms5.8 µs337 ns–212×17×
I Ching: a reading11 ms14 µs2.2 µs–773×6.6×
Tarot: a deal, One2.5 ms10 µs290 ns–237×36×
Tarot: a deal, CelticCross30 ms56 µs6.2 µs–533×9.0×
Tarot: a deal, YearAhead46 ms80 µs10 µs–584×7.8×
Astrology: a chart (interpreted: the whole program built each time)990 ms0.97 ms0.18 ms–1,022×5.5×
Board and card games, each with a plain JavaScript version
Checkers: the moves20 ms15 µs6.3 µs336 ns1,323×2.3×
Checkers: Hazel's move, looking 1 ahead34 ms27 µs13 µs528 ns1,230×2.1×
Checkers: Hazel's move, looking 2 ahead55 ms31 µs23 µs1.4 µs1,761×1.3×
Checkers: Hazel's move, looking 3 ahead149 ms74 µs65 µs3.5 µs2,002×1.1×
Checkers: Hazel's move, looking 4 ahead375 ms0.18 ms0.17 ms12 µs2,080×1.0×
Go: a move played14 ms83 µs4.6 µs1.7 µs165×18×
Go: Hazel's choice of move446 ms0.17 ms0.12 ms8.5 µs2,560×1.5×
Go: the score700 ms0.20 ms0.17 ms7.3 µs3,430×1.2×
Minesweeper: 9 by 9: the mines laid81 ms0.42 ms34 µs3.6 µs190×12×
Minesweeper: 9 by 9: a cell opened10 ms0.60 ms12 µs474 ns17×50×
Minesweeper: 9 by 9: the hint (a sure move)134 ms0.35 ms43 µs3.4 µs386×8.1×
Minesweeper: 16 by 30: the mines laid442 ms3.7 ms0.20 ms23 µs119×19×
Minesweeper: 16 by 30: a cell opened32 ms7.5 ms50 µs531 ns4.2×150×
Minesweeper: 16 by 30: the hint (a sure move)576 ms2.0 ms0.22 ms6.6 µs288×9.0×
Mahjong: a hand dealt108 ms0.10 ms40 µs17 µs1,056×2.5×
Mahjong: a step1.8 ms75 µs2.2 µs8 ns24×34×
Mahjong: your options18 ms34 µs5.6 µs1.3 µs534×6.1×
Solitaire: a deal41 ms0.12 ms9.9 µs1.8 µs348×12×
Solitaire: every legal move44 ms100 µs6.4 µs332 ns438×16×
Solitaire: a move made1.7 ms0.16 ms3.0 µs92 ns10×53×
A puzzle
Mahjong solitaire: a deal16.2 s15 ms12 ms–1,063×1.2×
Mahjong solitaire: Hazel's choice of pair16 ms3.5 ms1.4 µs–4.7×2,462×
Arcade games, a step at a time
Pinball: a step86 ms59 µs35 µs–1,467×1.7×
Pinball: a board20 ms0.31 ms35 µs–63×8.9×
Downhill: a step2.9 ms38 µs4.9 µs–75×7.8×
Downhill: the robot's choice3.8 ms11 µs1.9 µs–344×5.9×
Bricks: a step1.9 ms55 µs2.9 µs–35×19×
Bricks: the robot's choice0.47 ms17 µs648 ns–28×26×
Snake: a step6.1 ms62 µs3.8 µs–100×16×
Snake: the robot's choice (42 long)677 ms0.14 ms0.16 ms–4,955×0.9×
Light trails: a step17 ms63 µs6.5 µs–264×9.7×
Light trails: the robot's choice, looking 1504.1 s1.0 ms1.0 ms–3,941×1.0×
Star wave: a step27 ms81 µs14 µs–331×5.6×
Star wave: the robot's choice68 ms37 µs21 µs–1,838×1.7×
Butterfly garden: a step9.2 ms0.23 ms27 µs–40×8.4×
Butterfly garden: the robot's choice224 ms0.14 ms69 µs–1,563×2.1×
Gnome dig: a step97 ms0.36 ms43 µs–267×8.3×
Gnome dig: the robot's choice3.8 s1.0 ms0.88 ms–3,742×1.2×

Where the jit’s time goes#

With build 2 (before the converters: with them, this crossing all but went), on 34 of the 45 operations, more of the jit’s time went to converting the values in and out than to the compiled code itself. The largest: Minesweeper, 16 by 30: a cell opened, 7.5 ms through the jit against 50 µs ahead of time. A call through the jit converts its arguments from the page’s JavaScript values into Hazel’s own values, using the function’s declared types, then from those into hazel-jit’s shapes; calls the compiled function; and converts its answer back the same two steps. A game’s whole state crosses on every call, both ways. Code compiled ahead of time does the same work with converters written by hand for the game’s values, straight from one shape to the other, so it pays for the crossing too, but far less.

Mahjong solitaire: Hazel's choice of pair100% crossingjit 3.5 ms, ahead of time 1.4 µsMinesweeper: 16 by 30: a cell opened99% crossingjit 7.5 ms, ahead of time 50 µsI Ching: a cast by coins98% crossingjit 4.1 µs, ahead of time 78 nsSolitaire: a move made98% crossingjit 0.16 ms, ahead of time 3.0 µsMinesweeper: 9 by 9: a cell opened98% crossingjit 0.60 ms, ahead of time 12 µsTarot: a deal, One97% crossingjit 10 µs, ahead of time 290 nsMahjong: a step97% crossingjit 75 µs, ahead of time 2.2 µsBricks: the robot's choice96% crossingjit 17 µs, ahead of time 648 nsBricks: a step95% crossingjit 55 µs, ahead of time 2.9 µsMinesweeper: 16 by 30: the mines laid95% crossingjit 3.7 ms, ahead of time 0.20 msGo: a move played94% crossingjit 83 µs, ahead of time 4.6 µsI Ching: a cast by yarrow94% crossingjit 5.8 µs, ahead of time 337 nsSnake: a step94% crossingjit 62 µs, ahead of time 3.8 µsSolitaire: every legal move94% crossingjit 100 µs, ahead of time 6.4 µsMinesweeper: 9 by 9: the mines laid92% crossingjit 0.42 ms, ahead of time 34 µsSolitaire: a deal92% crossingjit 0.12 ms, ahead of time 9.9 µsLight trails: a step90% crossingjit 63 µs, ahead of time 6.5 µsMinesweeper: 16 by 30: the hint (a sure mo…89% crossingjit 2.0 ms, ahead of time 0.22 msTarot: a deal, CelticCross89% crossingjit 56 µs, ahead of time 6.2 µsPinball: a board89% crossingjit 0.31 ms, ahead of time 35 µsButterfly garden: a step88% crossingjit 0.23 ms, ahead of time 27 µsGnome dig: a step88% crossingjit 0.36 ms, ahead of time 43 µsMinesweeper: 9 by 9: the hint (a sure move)88% crossingjit 0.35 ms, ahead of time 43 µsDownhill: a step87% crossingjit 38 µs, ahead of time 4.9 µsTarot: a deal, YearAhead87% crossingjit 80 µs, ahead of time 10 µsI Ching: a reading85% crossingjit 14 µs, ahead of time 2.2 µsMahjong: your options84% crossingjit 34 µs, ahead of time 5.6 µsDownhill: the robot's choice83% crossingjit 11 µs, ahead of time 1.9 µsStar wave: a step82% crossingjit 81 µs, ahead of time 14 µsAstrology: a chart (interpreted: the whole…82% crossingjit 0.97 ms, ahead of time 0.18 msMahjong: a hand dealt61% crossingjit 0.10 ms, ahead of time 40 µsCheckers: the moves57% crossingjit 15 µs, ahead of time 6.3 µsCheckers: Hazel's move, looking 1 ahead53% crossingjit 27 µs, ahead of time 13 µsButterfly garden: the robot's choice52% crossingjit 0.14 ms, ahead of time 69 µsStar wave: the robot's choice42% crossingjit 37 µs, ahead of time 21 µsPinball: a step41% crossingjit 59 µs, ahead of time 35 µsGo: Hazel's choice of move32% crossingjit 0.17 ms, ahead of time 0.12 msCheckers: Hazel's move, looking 2 ahead26% crossingjit 31 µs, ahead of time 23 µsMahjong solitaire: a deal19% crossingjit 15 ms, ahead of time 12 msGo: the score16% crossingjit 0.20 ms, ahead of time 0.17 msGnome dig: the robot's choice13% crossingjit 1.0 ms, ahead of time 0.88 msCheckers: Hazel's move, looking 3 ahead12% crossingjit 74 µs, ahead of time 65 µsCheckers: Hazel's move, looking 4 ahead3% crossingjit 0.18 ms, ahead of time 0.17 msLight trails: the robot's choice, looking …3% crossingjit 1.0 ms, ahead of time 1.0 msSnake: the robot's choice (42 long)0% crossingjit 0.14 ms, ahead of time 0.16 msthe compiled code’s own workcrossing: the values in and out
Builds 2 and 3 (not a build of the app). Each operation’s time through the jit, split into the compiled code’s own work (its time ahead of time, blue) and the rest, the crossing (orange), sorted by the crossing’s share. Where the jit came out a little faster than ahead of time (the robots’ searches, within the clock’s noise), the bar is all blue.

Expert Minesweeper shows the extremes. Opening one cell of a 16 by 30 board takes 32 ms interpreted, 7.5 ms through the jit, 50 µs ahead of time and 531 ns in plain JavaScript: the call does almost nothing, and the board of 480 cells crosses twice. A search, on the other hand, works for milliseconds on a state that crosses once: there the jit and code compiled ahead of time are the same speed.

The converters, a day later#

With #2857’s converters by declared type (build 5), the 45 operations ran 9.0× as fast through the jit as with build 2 (the geometric mean), and the jit came out about even with code compiled ahead of time: 0.8× its time, against 7.5× the day before. On 35 of the 45 operations the jit is now within half again of the ahead-of-time code, or faster. Part of that is the driver’s: see the first caution below, before reading these as a game’s speed. Build 5 is build 2’s package with one file changed: its hazel-lib.js, taken from #2857 at a97b6f7465, whose jit writes a page’s values straight into compiled code’s shapes and back by each function’s declared type, with no Hazel values in between, and remembers each value it has converted. Every answer was the same.

0.01×0.1×1×10×100×1000×Mahjong solitaire: Hazel's choice of pairthe day before: jit 3.5 ms, ahead of time 1.4 µswith the converters: jit 1.4 µs, ahead of time 1.4 µs3.5 ms → 1.4 µsMinesweeper: 16 by 30: a cell openedthe day before: jit 7.5 ms, ahead of time 50 µswith the converters: jit 2.7 µs, ahead of time 49 µs7.5 ms → 2.7 µsI Ching: a cast by coinsthe day before: jit 4.1 µs, ahead of time 78 nswith the converters: jit 894 ns, ahead of time 97 ns4.1 µs → 894 nsSolitaire: a move madethe day before: jit 0.16 ms, ahead of time 3.0 µswith the converters: jit 883 ns, ahead of time 5.2 µs0.16 ms → 883 nsMinesweeper: 9 by 9: a cell openedthe day before: jit 0.60 ms, ahead of time 12 µswith the converters: jit 12 µs, ahead of time 12 µs0.60 ms → 12 µsTarot: a deal, Onethe day before: jit 10 µs, ahead of time 290 nswith the converters: jit 616 ns, ahead of time 271 ns10 µs → 616 nsMahjong: a stepthe day before: jit 75 µs, ahead of time 2.2 µswith the converters: jit 155 ns, ahead of time 3.1 µs75 µs → 155 nsBricks: the robot's choicethe day before: jit 17 µs, ahead of time 648 nswith the converters: jit 361 ns, ahead of time 768 ns17 µs → 361 nsBricks: a stepthe day before: jit 55 µs, ahead of time 2.9 µswith the converters: jit 993 ns, ahead of time 4.1 µs55 µs → 993 nsMinesweeper: 16 by 30: the mines laidthe day before: jit 3.7 ms, ahead of time 0.20 mswith the converters: jit 0.37 ms, ahead of time 0.21 ms3.7 ms → 0.37 msGo: a move playedthe day before: jit 83 µs, ahead of time 4.6 µswith the converters: jit 3.9 µs, ahead of time 3.4 µs83 µs → 3.9 µsI Ching: a cast by yarrowthe day before: jit 5.8 µs, ahead of time 337 nswith the converters: jit 1.5 µs, ahead of time 331 ns5.8 µs → 1.5 µsSnake: a stepthe day before: jit 62 µs, ahead of time 3.8 µswith the converters: jit 2.8 µs, ahead of time 3.3 µs62 µs → 2.8 µsSolitaire: every legal movethe day before: jit 100 µs, ahead of time 6.4 µswith the converters: jit 12 µs, ahead of time 12 µs100 µs → 12 µsMinesweeper: 9 by 9: the mines laidthe day before: jit 0.42 ms, ahead of time 34 µswith the converters: jit 55 µs, ahead of time 36 µs0.42 ms → 55 µsSolitaire: a dealthe day before: jit 0.12 ms, ahead of time 9.9 µswith the converters: jit 30 µs, ahead of time 17 µs0.12 ms → 30 µsLight trails: a stepthe day before: jit 63 µs, ahead of time 6.5 µswith the converters: jit 4.4 µs, ahead of time 5.2 µs63 µs → 4.4 µsMinesweeper: 16 by 30: the hint (a sure mo…the day before: jit 2.0 ms, ahead of time 0.22 mswith the converters: jit 0.20 ms, ahead of time 0.23 ms2.0 ms → 0.20 msTarot: a deal, CelticCrossthe day before: jit 56 µs, ahead of time 6.2 µswith the converters: jit 12 µs, ahead of time 5.3 µs56 µs → 12 µsPinball: a boardthe day before: jit 0.31 ms, ahead of time 35 µswith the converters: jit 44 ns, ahead of time 31 µs0.31 ms → 44 nsButterfly garden: a stepthe day before: jit 0.23 ms, ahead of time 27 µswith the converters: jit 2.5 µs, ahead of time 26 µs0.23 ms → 2.5 µsGnome dig: a stepthe day before: jit 0.36 ms, ahead of time 43 µswith the converters: jit 30 µs, ahead of time 36 µs0.36 ms → 30 µsMinesweeper: 9 by 9: the hint (a sure move)the day before: jit 0.35 ms, ahead of time 43 µswith the converters: jit 35 µs, ahead of time 43 µs0.35 ms → 35 µsDownhill: a stepthe day before: jit 38 µs, ahead of time 4.9 µswith the converters: jit 1.7 µs, ahead of time 5.6 µs38 µs → 1.7 µsTarot: a deal, YearAheadthe day before: jit 80 µs, ahead of time 10 µswith the converters: jit 14 µs, ahead of time 9.2 µs80 µs → 14 µsI Ching: a readingthe day before: jit 14 µs, ahead of time 2.2 µswith the converters: jit 3.6 µs, ahead of time 2.0 µs14 µs → 3.6 µsMahjong: your optionsthe day before: jit 34 µs, ahead of time 5.6 µswith the converters: jit 7.6 µs, ahead of time 7.8 µs34 µs → 7.6 µsDownhill: the robot's choicethe day before: jit 11 µs, ahead of time 1.9 µswith the converters: jit 1.2 µs, ahead of time 2.5 µs11 µs → 1.2 µsStar wave: a stepthe day before: jit 81 µs, ahead of time 14 µswith the converters: jit 13 µs, ahead of time 18 µs81 µs → 13 µsAstrology: a chart (interpreted: the whole…the day before: jit 0.97 ms, ahead of time 0.18 mswith the converters: jit 0.57 ms, ahead of time 0.30 ms0.97 ms → 0.57 msMahjong: a hand dealtthe day before: jit 0.10 ms, ahead of time 40 µswith the converters: jit 56 µs, ahead of time 48 µs0.10 ms → 56 µsCheckers: the movesthe day before: jit 15 µs, ahead of time 6.3 µswith the converters: jit 12 µs, ahead of time 10 µs15 µs → 12 µsCheckers: Hazel's move, looking 1 aheadthe day before: jit 27 µs, ahead of time 13 µswith the converters: jit 21 µs, ahead of time 22 µs27 µs → 21 µsButterfly garden: the robot's choicethe day before: jit 0.14 ms, ahead of time 69 µswith the converters: jit 64 µs, ahead of time 75 µs0.14 ms → 64 µsStar wave: the robot's choicethe day before: jit 37 µs, ahead of time 21 µswith the converters: jit 28 µs, ahead of time 29 µs37 µs → 28 µsPinball: a stepthe day before: jit 59 µs, ahead of time 35 µswith the converters: jit 29 µs, ahead of time 27 µs59 µs → 29 µsGo: Hazel's choice of movethe day before: jit 0.17 ms, ahead of time 0.12 mswith the converters: jit 72 µs, ahead of time 78 µs0.17 ms → 72 µsCheckers: Hazel's move, looking 2 aheadthe day before: jit 31 µs, ahead of time 23 µswith the converters: jit 38 µs, ahead of time 39 µs31 µs → 38 µsMahjong solitaire: a dealthe day before: jit 15 ms, ahead of time 12 mswith the converters: jit 12 ms, ahead of time 11 ms15 ms → 12 msGo: the scorethe day before: jit 0.20 ms, ahead of time 0.17 mswith the converters: jit 0.13 ms, ahead of time 0.13 ms0.20 ms → 0.13 msGnome dig: the robot's choicethe day before: jit 1.0 ms, ahead of time 0.88 mswith the converters: jit 0.90 ms, ahead of time 0.86 ms1.0 ms → 0.90 msCheckers: Hazel's move, looking 3 aheadthe day before: jit 74 µs, ahead of time 65 µswith the converters: jit 0.10 ms, ahead of time 0.11 ms74 µs → 0.10 msCheckers: Hazel's move, looking 4 aheadthe day before: jit 0.18 ms, ahead of time 0.17 mswith the converters: jit 0.29 ms, ahead of time 0.30 ms0.18 ms → 0.29 msLight trails: the robot's choice, looking …the day before: jit 1.0 ms, ahead of time 1.0 mswith the converters: jit 0.93 ms, ahead of time 0.83 ms1.0 ms → 0.93 msSnake: the robot's choice (42 long)the day before: jit 0.14 ms, ahead of time 0.16 mswith the converters: jit 0.18 ms, ahead of time 0.16 ms0.14 ms → 0.18 msthe day before (build 2)with the converters (build 5)
Builds 2 and 5, each against build 3 (not a build of the app: Node 22 on GitHub’s runners, one runner per game; runs 45247119864 and 45424324608). Each operation’s time through the jit as a multiple of its time ahead of time, on a log scale: orange the day before, green with the converters; left of the dark line, the jit was the faster. Beside each, its jit time before and after.

This was the first step of What next; the second follows.

A game’s own run of states#

Played the way a page plays them, each call given the state the last call answered, the jit took 0.91× the time of code compiled ahead of time (the geometric mean over 14 games), and every answer was the same. Given a fresh copy of every state, so that the converters remember nothing, the jit took 2.2× its own time. The operations above are timed on one position again and again, which flatters the jit’s memory. Here each game plays one run instead: up to 600 frames of an arcade game, or a whole game, hand or deal of a board or card game, 4,601 steps in all. Each run is recorded once by the compiled rules (the robot’s choices with a press of chance now and then, or random legal moves), then replayed by every way from the same start with the same inputs. A page gives back what the jit answered, so each state is the jit’s own: what did not change is the very object converted before, and only the new parts convert.

0.1 µs1 µs10 µs0.1 ms1 ms10 ms0.1 s1 sBoard and card games, each with a plain JavaScript versionCheckers: a game of 46 movesCheckers, a game of 46 moves (each: the moves, then one made): interpreted 15 ms a call, 92 callsCheckers, a game of 46 moves (each: the moves, then one made): jit 15 µs a call, 92 callsCheckers, a game of 46 moves (each: the moves, then one made): jit, every argument copied 14 µs a call, 92 callsCheckers, a game of 46 moves (each: the moves, then one made): compiled ahead of time 16 µs a call, 92 callsCheckers, a game of 46 moves (each: the moves, then one made): plain JS 2.1 µs a call, 92 callsGo: a game on 9 by 9, 89 turnsGo, a game on 9 by 9, 89 turns (each: a move played, or a pass): interpreted 54 ms a call, 89 callsGo, a game on 9 by 9, 89 turns (each: a move played, or a pass): jit 19 µs a call, 89 callsGo, a game on 9 by 9, 89 turns (each: a move played, or a pass): jit, every argument copied 27 µs a call, 89 callsGo, a game on 9 by 9, 89 turns (each: a move played, or a pass): compiled ahead of time 16 µs a call, 89 callsGo, a game on 9 by 9, 89 turns (each: a move played, or a pass): plain JS 2.3 µs a call, 89 callsMinesweeper: 16 by 30, 319 tapsMinesweeper, 16 by 30, 319 taps (each: a cell opened or flagged): interpreted 51 ms a call, 319 callsMinesweeper, 16 by 30, 319 taps (each: a cell opened or flagged): jit 19 µs a call, 319 callsMinesweeper, 16 by 30, 319 taps (each: a cell opened or flagged): jit, every argument copied 0.31 ms a call, 319 callsMinesweeper, 16 by 30, 319 taps (each: a cell opened or flagged): compiled ahead of time 44 µs a call, 319 callsMinesweeper, 16 by 30, 319 taps (each: a cell opened or flagged): plain JS 511 ns a call, 319 callsMahjong: a hand of 137 stepsMahjong, a hand of 137 steps (each: your options, then a step): interpreted 23 ms a call, 274 callsMahjong, a hand of 137 steps (each: your options, then a step): jit 17 µs a call, 274 callsMahjong, a hand of 137 steps (each: your options, then a step): jit, every argument copied 62 µs a call, 274 callsMahjong, a hand of 137 steps (each: your options, then a step): compiled ahead of time 19 µs a call, 274 callsMahjong, a hand of 137 steps (each: your options, then a step): plain JS 7.9 µs a call, 274 callsSolitaire: a deal, 200 movesSolitaire, a deal, 200 moves (each: the moves, whether it is won, then one made): interpreted 46 ms a call, 600 callsSolitaire, a deal, 200 moves (each: the moves, whether it is won, then one made): jit 11 µs a call, 600 callsSolitaire, a deal, 200 moves (each: the moves, whether it is won, then one made): jit, every argument copied 0.15 ms a call, 600 callsSolitaire, a deal, 200 moves (each: the moves, whether it is won, then one made): compiled ahead of time 8.1 µs a call, 600 callsSolitaire, a deal, 200 moves (each: the moves, whether it is won, then one made): plain JS 1.1 µs a call, 600 callsA puzzleMahjong solitaire: a deal, 72 pairs takenMahjong solitaire, a deal, 72 pairs taken (each: the free places, then a pair taken): interpreted 78 ms a call, 144 callsMahjong solitaire, a deal, 72 pairs taken (each: the free places, then a pair taken): jit 100 µs a call, 144 callsMahjong solitaire, a deal, 72 pairs taken (each: the free places, then a pair taken): jit, every argument copied 0.80 ms a call, 144 callsMahjong solitaire, a deal, 72 pairs taken (each: the free places, then a pair taken): compiled ahead of time 65 µs a call, 144 callsArcade games, a step at a timePinball: 600 framesPinball, 600 frames (the flippers at random, the plunger when the ball waits): interpreted 289 ms a call, 600 callsPinball, 600 frames (the flippers at random, the plunger when the ball waits): jit 54 µs a call, 600 callsPinball, 600 frames (the flippers at random, the plunger when the ball waits): jit, every argument copied 57 µs a call, 600 callsPinball, 600 frames (the flippers at random, the plunger when the ball waits): compiled ahead of time 50 µs a call, 600 callsDownhill: 600 framesDownhill, 600 frames (the robot's turns and crouches, a press of chance now and then): interpreted 14 ms a call, 600 callsDownhill, 600 frames (the robot's turns and crouches, a press of chance now and then): jit 9.3 µs a call, 600 callsDownhill, 600 frames (the robot's turns and crouches, a press of chance now and then): jit, every argument copied 11 µs a call, 600 callsDownhill, 600 frames (the robot's turns and crouches, a press of chance now and then): compiled ahead of time 13 µs a call, 600 callsBricks: 600 framesBricks, 600 frames (the robot's paddle, sent elsewhere now and then): interpreted 16 ms a call, 600 callsBricks, 600 frames (the robot's paddle, sent elsewhere now and then): jit 5.1 µs a call, 600 callsBricks, 600 frames (the robot's paddle, sent elsewhere now and then): jit, every argument copied 6.4 µs a call, 600 callsBricks, 600 frames (the robot's paddle, sent elsewhere now and then): compiled ahead of time 8.0 µs a call, 600 callsSnake: 116 framesSnake, 116 frames (the robot's turns, one of chance now and then): interpreted 1.4 ms a call, 116 callsSnake, 116 frames (the robot's turns, one of chance now and then): jit 2.8 µs a call, 116 callsSnake, 116 frames (the robot's turns, one of chance now and then): jit, every argument copied 2.6 µs a call, 116 callsSnake, 116 frames (the robot's turns, one of chance now and then): compiled ahead of time 2.4 µs a call, 116 callsLight trails: 22 framesLight trails, 22 frames (both robot riders): interpreted 7.1 ms a call, 22 callsLight trails, 22 frames (both robot riders): jit 6.9 µs a call, 22 callsLight trails, 22 frames (both robot riders): jit, every argument copied 4.7 µs a call, 22 callsLight trails, 22 frames (both robot riders): compiled ahead of time 4.8 µs a call, 22 callsStar wave: 600 framesStar wave, 600 frames (the robot's moves and shots, one of chance now and then): interpreted 35 ms a call, 600 callsStar wave, 600 frames (the robot's moves and shots, one of chance now and then): jit 13 µs a call, 600 callsStar wave, 600 frames (the robot's moves and shots, one of chance now and then): jit, every argument copied 18 µs a call, 600 callsStar wave, 600 frames (the robot's moves and shots, one of chance now and then): compiled ahead of time 15 µs a call, 600 callsButterfly garden: 600 framesButterfly garden, 600 frames (the robot's moves and shots, one of chance now and then): interpreted 4.4 ms a call, 600 callsButterfly garden, 600 frames (the robot's moves and shots, one of chance now and then): jit 4.9 µs a call, 600 callsButterfly garden, 600 frames (the robot's moves and shots, one of chance now and then): jit, every argument copied 18 µs a call, 600 callsButterfly garden, 600 frames (the robot's moves and shots, one of chance now and then): compiled ahead of time 17 µs a call, 600 callsGnome dig: 600 framesGnome dig, 600 frames (the robot's ways, flashes and blasts, one of chance now and then): interpreted 111 ms a call, 600 callsGnome dig, 600 frames (the robot's ways, flashes and blasts, one of chance now and then): jit 51 µs a call, 600 callsGnome dig, 600 frames (the robot's ways, flashes and blasts, one of chance now and then): jit, every argument copied 64 µs a call, 600 callsGnome dig, 600 frames (the robot's ways, flashes and blasts, one of chance now and then): compiled ahead of time 39 µs a call, 600 callsinterpretedjitjit, every argument copiedcompiled ahead of timeplain JS
Build 5, with builds 1, 3 and 4 (not a build of the app: Node 22 on GitHub’s runners, one runner per game; run 45452623490). Each game’s run, the time a call each way: the run’s calls timed and summed, over the number of calls, on a log scale. The ring is build 5 again, given a fresh copy of every argument (copied off the clock), so its converters find nothing they have seen.
0.1×0.2×0.5×1×2×Mahjong solitaireMahjong solitaire, build 5 (memos in WeakMaps): the jit 1.53× the ahead-of-time codeMahjong solitaire, build 6 (memos as links): the jit 0.85× the ahead-of-time code1.53× → 0.85×Light trailsLight trails, build 5 (memos in WeakMaps): the jit 1.42× the ahead-of-time codeLight trails, build 6 (memos as links): the jit 1.78× the ahead-of-time code1.42× → 1.78×SolitaireSolitaire, build 5 (memos in WeakMaps): the jit 1.39× the ahead-of-time codeSolitaire, build 6 (memos as links): the jit 0.66× the ahead-of-time code1.39× → 0.66×Gnome digGnome dig, build 5 (memos in WeakMaps): the jit 1.31× the ahead-of-time codeGnome dig, build 6 (memos as links): the jit 0.83× the ahead-of-time code1.31× → 0.83×GoGo, build 5 (memos in WeakMaps): the jit 1.19× the ahead-of-time codeGo, build 6 (memos as links): the jit 1.00× the ahead-of-time code1.19× → 1.00×SnakeSnake, build 5 (memos in WeakMaps): the jit 1.17× the ahead-of-time codeSnake, build 6 (memos as links): the jit 1.09× the ahead-of-time code1.17× → 1.09×PinballPinball, build 5 (memos in WeakMaps): the jit 1.09× the ahead-of-time codePinball, build 6 (memos as links): the jit 1.06× the ahead-of-time code1.09× → 1.06×CheckersCheckers, build 5 (memos in WeakMaps): the jit 0.97× the ahead-of-time codeCheckers, build 6 (memos as links): the jit 1.20× the ahead-of-time code0.97× → 1.20×MahjongMahjong, build 5 (memos in WeakMaps): the jit 0.90× the ahead-of-time codeMahjong, build 6 (memos as links): the jit 0.79× the ahead-of-time code0.90× → 0.79×Star waveStar wave, build 5 (memos in WeakMaps): the jit 0.87× the ahead-of-time codeStar wave, build 6 (memos as links): the jit 0.80× the ahead-of-time code0.87× → 0.80×DownhillDownhill, build 5 (memos in WeakMaps): the jit 0.72× the ahead-of-time codeDownhill, build 6 (memos as links): the jit 0.69× the ahead-of-time code0.72× → 0.69×BricksBricks, build 5 (memos in WeakMaps): the jit 0.64× the ahead-of-time codeBricks, build 6 (memos as links): the jit 0.63× the ahead-of-time code0.64× → 0.63×MinesweeperMinesweeper, build 5 (memos in WeakMaps): the jit 0.44× the ahead-of-time codeMinesweeper, build 6 (memos as links): the jit 0.34× the ahead-of-time code0.44× → 0.34×Butterfly gardenButterfly garden, build 5 (memos in WeakMaps): the jit 0.29× the ahead-of-time codeButterfly garden, build 6 (memos as links): the jit 0.21× the ahead-of-time code0.29× → 0.21×build 5: memos in WeakMapsbuild 6: memos as links
Builds 5 and 6, each against build 3 on the same runner (not a build of the app: Node 22 on GitHub’s runners; runs 45452623490 and 45459402709). Each game’s run: the jit’s time a call over the ahead-of-time code’s, on a log scale; left of the dark line the jit was the faster.

Building the rules#

Building a game’s rules with the jit took less time than with the interpreter on every game: 117 ms to 3.7 s, against 511 ms to 6.1 s (23% to 65% of the interpreter’s time). Compiled ahead of time, nothing is built in the page. Both builds read and check the program first; then the interpreter evaluates every module’s definitions, while the jit compiles them and runs the compiled code. For Gnome dig that is 5.6 s interpreted and 3.7 s through the jit, each time a page loads it. An ahead-of-time file is 32 kB to 124 kB (the compiled program, 18 kB of runtime and the converters), against 2.9 MB of hazel-lib.js, and it starts at once.

Minesweeper6.1 s2.1 s98 kBGnome dig5.6 s3.7 s124 kBMahjong solitaire2.4 s1.3 s66 kBGo2.1 s492 ms53 kBMahjong2.0 s1.1 s82 kBAstrology1.7 s722 ms101 kBPinball1.7 s730 ms77 kBCheckers1.2 s338 ms43 kBButterfly garden1.2 s589 ms49 kBStar wave982 ms414 ms45 kBSolitaire950 ms367 ms54 kBDownhill907 ms417 ms41 kBLight trails778 ms250 ms32 kBSnake742 ms247 ms32 kBBricks653 ms230 ms38 kBTarot512 ms119 ms34 kBI Ching511 ms117 ms35 kBahead of time: the fileinterpreted: buildjit: build and compile
Builds 1 and 2 (not a build of the app). Milliseconds to build each game’s rules once, interpreted (runProgram, grey) and with the jit (jitProgram: reading, checking, compiling and running the compiled modules, orange); on the right, the size of its ahead-of-time file (build 3). One run each, each game on its own runner.
The one-time costs, game by game, and each runner’s processor
gameinterpreted: buildjit: build and compilecompileProgramcompiled programahead-of-time filethe runner’s processor
I Ching511 ms117 ms111 ms15 kB35 kBAMD EPYC 9V45 96-Core Processor
Tarot512 ms119 ms105 ms14 kB34 kBIntel(R) Xeon(R) 6975P-C
Astrology1.7 s722 ms663 ms80 kB101 kBIntel(R) Xeon(R) 6975P-C
Checkers1.2 s338 ms370 ms22 kB43 kBAMD EPYC 9V45 96-Core Processor
Go2.1 s492 ms419 ms32 kB53 kBAMD EPYC 7763 64-Core Processor
Minesweeper6.1 s2.1 s1.7 s77 kB98 kBAMD EPYC 7763 64-Core Processor
Mahjong2.0 s1.1 s1.1 s60 kB82 kBIntel(R) Xeon(R) 6975P-C
Solitaire950 ms367 ms267 ms32 kB54 kBAMD EPYC 9V45 96-Core Processor
Mahjong solitaire2.4 s1.3 s1.2 s45 kB66 kBAMD EPYC 7763 64-Core Processor
Pinball1.7 s730 ms799 ms55 kB77 kBIntel(R) Xeon(R) 6975P-C
Downhill907 ms417 ms390 ms20 kB41 kBAMD EPYC 9V74 80-Core Processor
Bricks653 ms230 ms229 ms18 kB38 kBIntel(R) Xeon(R) 6975P-C
Snake742 ms247 ms216 ms12 kB32 kBAMD EPYC 7763 64-Core Processor
Light trails778 ms250 ms265 ms11 kB32 kBAMD EPYC 7763 64-Core Processor
Star wave982 ms414 ms428 ms24 kB45 kBINTEL(R) XEON(R) PLATINUM 8573C
Butterfly garden1.2 s589 ms510 ms28 kB49 kBAMD EPYC 7763 64-Core Processor
Gnome dig5.6 s3.7 s3.4 s102 kB124 kBAMD EPYC 7763 64-Core Processor

Against plain JavaScript#

Plain JavaScript runs the 5 games that have it about 16× as fast as hazel-jit’s code compiled ahead of time (geometric mean over 20 operations), from 2.3× to 283×; that is 36,200× as fast as the interpreter. These are the games of ^report(60), which measured the same gap with an earlier hazel-jit (about 13 times) and listed what hazel-jit’s code pays for, one cost at a time: a step budget checked in every function, exact integers through runtime calls, lists as chains of cells, and tuples as arrays carrying their labels. This build’s compiler writes code the same way; this report adds no new measurement of those costs.

What to decide before dev#

Ten questions, roughly in the order they block. Each says what is at stake, the options we see, and what the measurements or the code say.

  1. One PR, or its parts? #2857 is 8,577 lines in one review. Its parts’ own PRs are still written for OCaml’s standard library, so landing them one by one means porting each to Core again; #2857 has the port done. Options: review and merge #2857, closing #2711, #2719 and #2814; land #2837 (the font) and #2842 (the icons) first, on their own, since they are small and independent and change what every user sees; or split #2857 by part, from its ported files.
  2. Does hazel-jit belong on dev, and where? It is 2,215 lines in src/language/dynamics: a second way to evaluate a program, which has to agree with the interpreter. Every new kind of term needs either a rule in the compiler or an explicit refusal (the program then runs interpreted). The livelits line (#2817) has a larger hazel-jit, with livelit views and the mixed mode of ^report(54). Options: on dev in the language library, with agreement tests in CI; on dev as a library of its own that only the package and the web app link; or on the package’s branch only, until the livelits line settles where hazel-jit lives.
  3. How is agreement kept? The answers agree on every operation measured here, but #2857’s CI checks the jit only through 7 library cases. The per-game agreement tests (the games’ rules, compiled and interpreted, the same answers) live on trial/794-games-aot. Options: bring them into #2857 (their programs into test/); or keep agreement as hazeljs-examples’ job, outside Hazel’s CI.
  4. What is the jit for: finished programs, or the editor too? It compiles a program only when every form in it compiles: a hole, a test, a stepper filter or a livelit makes the whole program run interpreted. That suits the games, which are finished programs; it does not help a program being edited. ^report(54)’s mixed mode, the interpreter calling compiled functions, is the path to the editor. Options: say the package’s jit is for finished programs, for now; or plan mixed mode into the package.
  5. The values a page sees are an API: which shapes? A constructor is {tag, value}, a list an array, a labeled tuple an object, a number read by its declared type, a function a function. Every call converts whole values (the crossing). Options: keep plain values only (simple, and the crossing stays); add handles, values kept in hazel-jit’s own shape between calls that a page reads through accessors; or both, plain by default.
  6. Numbers. On this branch an SInt is checked at 32 bits; #2828 gives SInt 64 bits everywhere and is on Core like this branch now, and the 64-bit compiled SInt of trial/694-view-jit needs it. An Int is exact in compiled code (a BigInt past 253), through runtime calls that ^report(60) found among the costs over plain JavaScript. Options: bring #2828 and the 64-bit compiled SInt into #2857 before merging; or merge first and follow with them. Either way, the number model is part of the API above.
  7. What dev’s users would see. The interface font changes on every page, and the icons work offline. The interpreter’s change is small and only reachable from JavaScript (fields put in their type’s order). Option: agree to the font as a team, since it is the one visible change.
  8. Licenses. The Source Code Pro, IBM Plex Sans and Material Icons licenses are now in licenses/ and ship with the editor. Five icons from The Noun Project, under its “Icon Pro” license, still ship, as Hazel’s web app always has. Options: confirm that license allows an npm package to carry them; replace them; or leave them out of the package’s editor.
  9. Who may embed Hazel? Embed mode takes messages only from matthewhammer.org, localhost and its own origin, a list in the code. On dev every hazel.org build would have it. Options: a list kept in the code; any page; or a setting at build time.
  10. Publishing. The package records the Hazel commit, branch and PR it was built from, and Verify checks its hazel-lib.js against that commit. 0.0.10 and 0.0.11 are not on npm. Options: who publishes, and from which branch (today hazeljs; after a merge, dev); whether every version must come from a commit on dev.

What next for speed#

  1. Convert by declared type, in one step. Done on 2026-11-24 (build 5, the converters, a day later): the jit 9.0× as fast, about even with ahead of time. Write the page’s values straight into hazel-jit’s shapes and back, guided by the function’s declared types, as the hand-written converters of the ahead-of-time files do, instead of through Hazel’s own values. On the 34 operations where the crossing is most of the jit’s time, this should bring the jit near the ahead-of-time column (7.5 ms to near 50 µs for an expert Minesweeper cell). The converters could be generated, and the ahead-of-time files could use them too.
  2. Time the converters on a game’s own run of states. Done on 2026-11-24 (a game’s own run of states): the jit 0.91× the ahead-of-time code’s time; with nothing remembered, 2.2× its own. Drive each game a few hundred steps from a seed and time the calls as a page makes them, each step’s state new, so that the converters’ memory helps only where a part of the state did not change; compare the jit with the ahead-of-time files the same way.
  3. Keep values compiled between calls. With handles (question 5 above), a game’s state stays in hazel-jit’s shape from one call to the next, and only what the page reads crosses. That removes the crossing’s cost for the state entirely, at the price of a second kind of value in the API.
  4. Load time. The jit builds a program each time a page loads it, 117 ms to 3.7 s for these games. Keep the compiled program in the browser’s storage, keyed by the program’s text, or compile it ahead of time when the program does not change, as the games do now.
  5. The costs over plain JavaScript. About 16× remains between hazel-jit’s code and a hand-written game. ^report(60) measured each cost on its own; the next step is to remove the largest where the declared types allow it, for example plain arithmetic where an Int provably stays small.
  6. Agreement in CI. Before any of these, bring the per-game agreement tests into Hazel’s CI (question 3), so a change to the compiler that changes an answer fails there, and not in a game.

How this was put together#

buildwhatwhere
Build 1 · the package’s interpreterHazel.runProgram on the game’s rules; its value’s functions calledhazeljs 0.0.11 (hazelgrove/hazeljs b149ed58f2, editor-in-package), whose hazel-lib.js is hazelgrove/hazel 98c482395d (branch hazeljs, #2857)
Build 2 · the package’s jitHazel.jitProgram on the same rules; its value’s functions calledthe same file
Build 3 · ahead of timeHazel.compileProgram’s program and runtime, put in place of those in the game’s own compiled.js, so its converters are the page’sthe same file, at the start of each game’s run
Build 5 · the jit with the convertersHazel.jitProgram on the same rules, with converters by declared typebuild 1’s package with its hazel-lib.js from hazelgrove/hazel a97b6f7465 (branch hazeljs, #2857), as build.hazel.org/hazeljs served it; hazeljs-examples 66cbf53 (branch bench/converters-a97b6f7)
Build 6 · the jit, memos as linksHazel.jitProgram on the same rules, its converters’ memos kept as links on the objects rather than in WeakMapsbuild 1’s package with its hazel-lib.js from hazelgrove/hazel ca851b422d (branch hazeljs, #2857), as build.hazel.org/hazeljs served it
Build 4 · plain JavaScriptthe game written by hand, <game>/plain.js: Checkers, Go, Minesweeper, Mahjong and Solitairematthewhammer/hazeljs-examples a4a0a88