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#
- What the package is: three ways for a page to run Hazel code, and Hazel’s own editor, offline.
- The PRs: one PR holds it all; the parts it came from are still open.
- How the branch differs from dev: 8,577 lines added, nearly all in new files, and the move to Jane Street Core.
- Every game, four ways: the 17 games’ 45 operations, interpreted, through the jit, compiled ahead of time, and in plain JavaScript.
- 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.
- The converters, a day later: the jit 9.0× as fast, and about even with code compiled ahead of time.
- 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.
- Building the rules: the jit builds faster than the interpreter; ahead of time, nothing is built in the page.
- Against plain JavaScript: what hazel-jit’s code still costs over a hand-written game.
- What to decide before dev: ten questions, each with its options.
- 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.
{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.
| PR | branch | state | lines | what it gives the package |
|---|---|---|---|---|
| #2857 | hazeljs into dev | draft | +8,577 / −59 | The 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. |
| #2711 | js-library into dev | draft | +510 / −3 | The library, first version: Hazel.analyze and Hazel.run from any page. |
| #2719 | js-library-embed into js-library | draft | +4,168 / −109 | Programs of named modules (analyzeProgram, runProgram), and Hazel in another page's frame. |
| #2814 | embed-editable into js-library-embed | draft | +948 / −72 | Embed mode: editable cells, one module at a time, go to definition across cells. |
| #2837 | ui-font-installed into dev | open | +110 / −5 | IBM Plex Sans as the interface font, in place of a Helvetica Neue that Hazel had no license to ship. |
| #2842 | ui-icons-shipped into dev | open | +222 / −6 | Material Icons shipped with Hazel, so nothing loads from Google. |
| #2817 | trial/694-view-jit into integration/livelits-modular-editors | draft | +15,915 / −1,174 | hazel-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. |
| #2828 | fix/758-sint-32-bits into dev | draft | +233 / −76 | SInt 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.
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).| part | files | lines added | lines removed |
|---|---|---|---|
| hazel-jit | 2 | 2,215 | 0 |
| the library | 5 | 1,088 | 1 |
| programs of modules | 1 | 683 | 0 |
| embed mode | 11 | 1,702 | 5 |
| fonts, icons, licenses | 12 | 434 | 11 |
| changes to dev’s own code | 4 | 179 | 32 |
| tests | 6 | 2,276 | 10 |
| all | 41 | 8,577 | 59 |
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.)
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:
- Ahead of time (blue): the game’s own converters, written by hand for its types, turn the page’s values straight into the compiled code’s shapes; the compiled code runs; its answer is turned straight back into the page’s values.
- Through the jit (orange), in build 2: each argument is first made into Hazel’s own value, the interpreter’s representation of a term (OCaml data, built by the function’s declared type), and then from that into the compiled code’s shape; the same compiled code runs; its answer is made into a Hazel value, and from that into the page’s. Every value crosses twice each way, through a representation made for the interpreter, and a call also has a fallback to the interpreter if the compiled code throws.
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
| operation | interpreted | jit | ahead of time | plain JS | jit over interpreted | ahead of time over jit |
|---|---|---|---|---|---|---|
| Oracles | ||||||
| I Ching: a cast by coins | 0.78 ms | 4.1 µs | 78 ns | – | 189× | 53× |
| I Ching: a cast by yarrow | 1.2 ms | 5.8 µs | 337 ns | – | 212× | 17× |
| I Ching: a reading | 11 ms | 14 µs | 2.2 µs | – | 773× | 6.6× |
| Tarot: a deal, One | 2.5 ms | 10 µs | 290 ns | – | 237× | 36× |
| Tarot: a deal, CelticCross | 30 ms | 56 µs | 6.2 µs | – | 533× | 9.0× |
| Tarot: a deal, YearAhead | 46 ms | 80 µs | 10 µs | – | 584× | 7.8× |
| Astrology: a chart (interpreted: the whole program built each time) | 990 ms | 0.97 ms | 0.18 ms | – | 1,022× | 5.5× |
| Board and card games, each with a plain JavaScript version | ||||||
| Checkers: the moves | 20 ms | 15 µs | 6.3 µs | 336 ns | 1,323× | 2.3× |
| Checkers: Hazel's move, looking 1 ahead | 34 ms | 27 µs | 13 µs | 528 ns | 1,230× | 2.1× |
| Checkers: Hazel's move, looking 2 ahead | 55 ms | 31 µs | 23 µs | 1.4 µs | 1,761× | 1.3× |
| Checkers: Hazel's move, looking 3 ahead | 149 ms | 74 µs | 65 µs | 3.5 µs | 2,002× | 1.1× |
| Checkers: Hazel's move, looking 4 ahead | 375 ms | 0.18 ms | 0.17 ms | 12 µs | 2,080× | 1.0× |
| Go: a move played | 14 ms | 83 µs | 4.6 µs | 1.7 µs | 165× | 18× |
| Go: Hazel's choice of move | 446 ms | 0.17 ms | 0.12 ms | 8.5 µs | 2,560× | 1.5× |
| Go: the score | 700 ms | 0.20 ms | 0.17 ms | 7.3 µs | 3,430× | 1.2× |
| Minesweeper: 9 by 9: the mines laid | 81 ms | 0.42 ms | 34 µs | 3.6 µs | 190× | 12× |
| Minesweeper: 9 by 9: a cell opened | 10 ms | 0.60 ms | 12 µs | 474 ns | 17× | 50× |
| Minesweeper: 9 by 9: the hint (a sure move) | 134 ms | 0.35 ms | 43 µs | 3.4 µs | 386× | 8.1× |
| Minesweeper: 16 by 30: the mines laid | 442 ms | 3.7 ms | 0.20 ms | 23 µs | 119× | 19× |
| Minesweeper: 16 by 30: a cell opened | 32 ms | 7.5 ms | 50 µs | 531 ns | 4.2× | 150× |
| Minesweeper: 16 by 30: the hint (a sure move) | 576 ms | 2.0 ms | 0.22 ms | 6.6 µs | 288× | 9.0× |
| Mahjong: a hand dealt | 108 ms | 0.10 ms | 40 µs | 17 µs | 1,056× | 2.5× |
| Mahjong: a step | 1.8 ms | 75 µs | 2.2 µs | 8 ns | 24× | 34× |
| Mahjong: your options | 18 ms | 34 µs | 5.6 µs | 1.3 µs | 534× | 6.1× |
| Solitaire: a deal | 41 ms | 0.12 ms | 9.9 µs | 1.8 µs | 348× | 12× |
| Solitaire: every legal move | 44 ms | 100 µs | 6.4 µs | 332 ns | 438× | 16× |
| Solitaire: a move made | 1.7 ms | 0.16 ms | 3.0 µs | 92 ns | 10× | 53× |
| A puzzle | ||||||
| Mahjong solitaire: a deal | 16.2 s | 15 ms | 12 ms | – | 1,063× | 1.2× |
| Mahjong solitaire: Hazel's choice of pair | 16 ms | 3.5 ms | 1.4 µs | – | 4.7× | 2,462× |
| Arcade games, a step at a time | ||||||
| Pinball: a step | 86 ms | 59 µs | 35 µs | – | 1,467× | 1.7× |
| Pinball: a board | 20 ms | 0.31 ms | 35 µs | – | 63× | 8.9× |
| Downhill: a step | 2.9 ms | 38 µs | 4.9 µs | – | 75× | 7.8× |
| Downhill: the robot's choice | 3.8 ms | 11 µs | 1.9 µs | – | 344× | 5.9× |
| Bricks: a step | 1.9 ms | 55 µs | 2.9 µs | – | 35× | 19× |
| Bricks: the robot's choice | 0.47 ms | 17 µs | 648 ns | – | 28× | 26× |
| Snake: a step | 6.1 ms | 62 µs | 3.8 µs | – | 100× | 16× |
| Snake: the robot's choice (42 long) | 677 ms | 0.14 ms | 0.16 ms | – | 4,955× | 0.9× |
| Light trails: a step | 17 ms | 63 µs | 6.5 µs | – | 264× | 9.7× |
| Light trails: the robot's choice, looking 150 | 4.1 s | 1.0 ms | 1.0 ms | – | 3,941× | 1.0× |
| Star wave: a step | 27 ms | 81 µs | 14 µs | – | 331× | 5.6× |
| Star wave: the robot's choice | 68 ms | 37 µs | 21 µs | – | 1,838× | 1.7× |
| Butterfly garden: a step | 9.2 ms | 0.23 ms | 27 µs | – | 40× | 8.4× |
| Butterfly garden: the robot's choice | 224 ms | 0.14 ms | 69 µs | – | 1,563× | 2.1× |
| Gnome dig: a step | 97 ms | 0.36 ms | 43 µs | – | 267× | 8.3× |
| Gnome dig: the robot's choice | 3.8 s | 1.0 ms | 0.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.
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.
- The same values every call: this flatters the jit. The driver times each operation on the same position again and again, and the converters remember what they converted, so an unchanged value crosses for nothing the second time, while the ahead-of-time files’ own converters still convert it every call. Pinball’s board went from 0.31 ms to 44 ns, and an expert Minesweeper cell now takes 2.7 µs through the jit against 50 µs ahead of time. In a game, each step’s new state crosses again, all but the parts that did not change, which are the same objects as before; how much of the 9.0× survives that is measured in a game’s own run of states, next.
- The runners differ from day to day. The interpreter and the ahead-of-time code were the same in both runs, yet came out at 1.17× and 1.12× of the day before’s times (geometric means), one operation from 0.66× to 1.83×. So read an operation’s change against its game’s own ahead-of-time time, as the figure does, not across the two days.
- Building took longer for some games: Mahjong 1.1 s → 1.9 s, Solitaire 367 ms → 572 ms, Bricks 230 ms → 356 ms. The converters need each function’s types from the statics; whether that or the runner is the cost needs both builds timed on one machine.
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.
- Against code compiled ahead of time. The jit did best against it in Butterfly garden 0.29×, Minesweeper 0.44×, Bricks 0.64×, and worst in Mahjong solitaire 1.53×, Light trails 1.42×, Solitaire 1.39× (its time over the ahead-of-time code’s). Even with fresh copies, remembering nothing, the jit was as fast as the ahead-of-time code or faster in Checkers, Downhill, Bricks, Light trails, Butterfly garden: there its converters, generated from the declared types, already match the hand-written ones. Elsewhere its memory is what keeps it even. Where it was slower, its memory cost more than it saved: see build 6, below. Light trails’ run is short (below).
- Remembering nothing. With every argument a fresh copy, the jit took 2.2× its own time (the geometric mean), and 2.0× the ahead-of-time code’s; most where the state is large and the call light: Minesweeper 16×, Solitaire 13×, Mahjong solitaire 8×, Mahjong 4×. An expert Minesweeper tap took 19 µs through the jit, 0.31 ms with fresh copies, and 44 µs ahead of time. A page that builds its state anew for each call, rather than giving back what Hazel answered, pays that.
- The memos as links (build 6). A profile of Mahjong solitaire’s run through the jit put a third of its time in the garbage collector and a quarter in the memos’ WeakMaps, their lookups and inserts: remembering cost more than converting. Build 6 keeps each memo as a link on the objects themselves: a hidden property on a page’s object, not enumerable, so
{...answer}does not copy it into a copy the page then changes (a plain one did, and the copy crossed as the answer it was made from); a WeakMap entry for a frozen object; a plain property on a compiled value, which no page sees. Played again on GitHub, the jit took 0.76× the ahead-of-time code’s time over the 14 games, against 0.91× with build 5: Mahjong solitaire 1.53× → 0.85×, Solitaire 1.39× → 0.66×, Gnome dig 1.31× → 0.83×. Every answer was the same. The ratios are within each run: the runners differed between the two runs, and the same ahead-of-time code took 0.76× to 2.24× its earlier time, game by game. Within that spread, Light trails 1.42× → 1.78×, Checkers 0.97× → 1.20× came out worse. - Interpreted, and plain JavaScript. The interpreter took 1,729× the jit’s time a call. Plain JavaScript, in the five games that have it, ran 8.7× as fast as the jit.
- Short runs: some games ended within 120 steps: Light trails, 22 frames; Checkers, 46 moves; Mahjong solitaire, 72 pairs taken; Go, 89 turns; Snake, 116 frames. Their times rest on fewer calls.
- The clock: each call is timed on its own and the times summed, so the clock adds about 71 ns to a call, small against the microseconds here. Every way but the interpreter plays its run once to warm up, then again until at least five runs and 0.3 s, and the median run is taken; the interpreter plays it once.
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.
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
| game | interpreted: build | jit: build and compile | compileProgram | compiled program | ahead-of-time file | the runner’s processor |
|---|---|---|---|---|---|---|
| I Ching | 511 ms | 117 ms | 111 ms | 15 kB | 35 kB | AMD EPYC 9V45 96-Core Processor |
| Tarot | 512 ms | 119 ms | 105 ms | 14 kB | 34 kB | Intel(R) Xeon(R) 6975P-C |
| Astrology | 1.7 s | 722 ms | 663 ms | 80 kB | 101 kB | Intel(R) Xeon(R) 6975P-C |
| Checkers | 1.2 s | 338 ms | 370 ms | 22 kB | 43 kB | AMD EPYC 9V45 96-Core Processor |
| Go | 2.1 s | 492 ms | 419 ms | 32 kB | 53 kB | AMD EPYC 7763 64-Core Processor |
| Minesweeper | 6.1 s | 2.1 s | 1.7 s | 77 kB | 98 kB | AMD EPYC 7763 64-Core Processor |
| Mahjong | 2.0 s | 1.1 s | 1.1 s | 60 kB | 82 kB | Intel(R) Xeon(R) 6975P-C |
| Solitaire | 950 ms | 367 ms | 267 ms | 32 kB | 54 kB | AMD EPYC 9V45 96-Core Processor |
| Mahjong solitaire | 2.4 s | 1.3 s | 1.2 s | 45 kB | 66 kB | AMD EPYC 7763 64-Core Processor |
| Pinball | 1.7 s | 730 ms | 799 ms | 55 kB | 77 kB | Intel(R) Xeon(R) 6975P-C |
| Downhill | 907 ms | 417 ms | 390 ms | 20 kB | 41 kB | AMD EPYC 9V74 80-Core Processor |
| Bricks | 653 ms | 230 ms | 229 ms | 18 kB | 38 kB | Intel(R) Xeon(R) 6975P-C |
| Snake | 742 ms | 247 ms | 216 ms | 12 kB | 32 kB | AMD EPYC 7763 64-Core Processor |
| Light trails | 778 ms | 250 ms | 265 ms | 11 kB | 32 kB | AMD EPYC 7763 64-Core Processor |
| Star wave | 982 ms | 414 ms | 428 ms | 24 kB | 45 kB | INTEL(R) XEON(R) PLATINUM 8573C |
| Butterfly garden | 1.2 s | 589 ms | 510 ms | 28 kB | 49 kB | AMD EPYC 7763 64-Core Processor |
| Gnome dig | 5.6 s | 3.7 s | 3.4 s | 102 kB | 124 kB | AMD 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.
- 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.
- Does hazel-jit belong on
dev, and where? It is 2,215 lines insrc/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: ondevin the language library, with agreement tests in CI; ondevas 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. - 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 intotest/); or keep agreement as hazeljs-examples’ job, outside Hazel’s CI. - 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. - 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. - 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-jitneeds 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. - 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. - 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. - Who may embed Hazel? Embed mode takes messages only from matthewhammer.org, localhost and its own origin, a list in the code. On
devevery hazel.org build would have it. Options: a list kept in the code; any page; or a setting at build time. - Publishing. The package records the Hazel commit, branch and PR it was built from, and Verify checks its
hazel-lib.jsagainst that commit. 0.0.10 and 0.0.11 are not on npm. Options: who publishes, and from which branch (todayhazeljs; after a merge,dev); whether every version must come from a commit ondev.
What next for speed#
- 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.
- 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.
- 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.
- 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.
- 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. - 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#
| build | what | where |
|---|---|---|
| Build 1 · the package’s interpreter | Hazel.runProgram on the game’s rules; its value’s functions called | hazeljs 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 jit | Hazel.jitProgram on the same rules; its value’s functions called | the same file |
| Build 3 · ahead of time | Hazel.compileProgram’s program and runtime, put in place of those in the game’s own compiled.js, so its converters are the page’s | the same file, at the start of each game’s run |
| Build 5 · the jit with the converters | Hazel.jitProgram on the same rules, with converters by declared type | build 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 links | Hazel.jitProgram on the same rules, its converters’ memos kept as links on the objects rather than in WeakMaps | build 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 JavaScript | the game written by hand, <game>/plain.js: Checkers, Go, Minesweeper, Mahjong and Solitaire | matthewhammer/hazeljs-examples a4a0a88 |
- The driver: hazeljs-examples’
node/package-bench.mjs, run by its workflowpackage bench(GitHub Actions run 45247119864, 2026-11-23), one runner per game, Node v22.23.5. For each game it builds the rules all four ways, then times each operation on positions made by the compiled rules from a seeded game: the operationsnode/compare-compiled.mjstimes. Both files are in063/drivers/. - Build 5 ran the same driver on 2026-11-24 (GitHub Actions run 45424324608), the four ways again on fresh runners; its results are in
063/data/conv/. - Build 6 is build 5’s package with its
hazel-lib.jsfrom #2857 at ca851b422d (the memos as links), played the same runs (hazeljs-examples 5a6fdb7, branchbench/runs-ca851b4; GitHub Actions run 45459402709); its results are in063/data/runs-links/. - The runs (a game’s own run of states) are the same driver with
--runs(hazeljs-examples 8866fbc, branchbench/runs-a97b6f7; GitHub Actions run 45452623490, 2026-11-24), build 5’s package, every game but the three oracles, whose calls stand alone; the results are in063/data/runs/. - The clock is
^report(60)’s: a call too fast for the clock is timed in batches (as many calls as take at least a millisecond), and the median of the batches is taken, per call; the interpreter at least 3 times and for at least 1.5 s, the others at least 30 times and for at least 0.3 s. Each operation was timed in one such run. A one-time cost (building, compiling) is one run. - The same answers: every way’s answer to every operation was compared with the ahead-of-time code’s (numbers to a relative 10−9, everything else exactly), and every one was the same. The run fails otherwise.
- The machines differ. Each game ran on its own runner, and GitHub gave them 5 different processors (listed in the one-time costs). A game’s four ways shared a machine, so the ratios within a game hold; times across games are not comparable to better than the machines’ difference.
- Astrology is interpreted as its page does it: the whole program built with one reading as its answer, each time, since building it with the reading function as its answer grows past gigabytes (#2832). The jit and the ahead-of-time code call the function.
- The diff and the PRs:
git diff --numstatofdev(21da4ae66b) against the branch (98c482395d), and the PRs as GitHub had them on 2026-11-23, in063/data/diff.jsonand063/data/prs.json. - The data: each game’s results as
063/data/<game>.json, its printed lines and its machine, in063/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 in063/data/.