Hazel reports · ^report(40) · Hazel as a JS library · started 2026-11-03, updated 2026-11-04 10:16 MST · work in progress · after ^report(25) and ^report(34)
Converting JS Values Directly
^report(34)'s next steps include this one: convert JavaScript values directly into Hazel values at the call boundary, not through syntax that Hazel then evaluates again. This report measures what that saves, in time and in space, with the trial branches that already do it. Asked by Matthew, 2026-11-03 (^ticket(681)).
Short answers. All measured on GitHub's machines in Node, two rounds; the method is at the end.
A call takes 60 to 85% less time. Passing 4,096 cells from JavaScript to a Hazel function took 139.2 ms with the package's build and 51.3 ms with the trial that converts directly (-63%). Without a type annotation on the parameter: 107.5 ms against 18.3 (-83%).
A call allocates 75 to 85% less memory. At 1,024 cells, a typed call allocated 80.4 MB with the package's build and 20.2 MB with the trial; an untyped call 71.2 MB against 11.0 MB.
A function that holds a value stops paying for it on every call. A function Hazel returned, holding 4,096 numbers, took 5.31 ms and allocated 29.0 MB a call with the package's build, growing with what it holds. With the closures trial: 0.025 ms and 93 KB at every size, about 215 times faster.
A value converted once and passed as a handle costs almost nothing: 0.024 ms a call at every size, against 66.5 ms converted each time, at 4,096 cells.
What is left is the type annotation's check. With the trial, a typed call still takes about 2.8 times an untyped one: Hazel checks the converted value against the parameter's type again. That is #2681's work, not this step's.
Up as PRs since 2026-11-04, on Matthew's word, stacked on #2719: lib-floor is #2807 (
^ticket(539)); the closures with the complexity test are #2808 (^ticket(558),^ticket(552)); the handles are #2809 (^ticket(551)), moved onto the closures from their measuring branch.
The step#
When a JavaScript program calls a Hazel function through Hazel's JS library, the argument has to become a Hazel value. Today the library turns it into Hazel syntax, a term with a fresh id on every node, and the evaluator then steps that term back into the value it already was, and checks it against the parameter's type. ^report(25) measured that as the floor every call pays, whatever the function does. ^report(34)'s step 4 is to stop doing that.
Three trial branches each take part of the step. Each is a branch of #2719 with green CI, built by Hazel's own Deploy workflow:
- lib-floor (
^ticket(539)): the converted argument is bound in the environment, and the function is applied to its name, so the evaluator never steps it again. - lib-closures (
^ticket(547),^ticket(558)): a function the library returns is the evaluator's own closure, its environment kept apart. Before, the environment was written into the function's body, so a held list became a literal that every call evaluated again. - handles (
^ticket(551)):Hazel.value(x)converts a value once and returns a handle; a call given the handle binds the stored value, with no conversion at all.
Time#
A Hazel function given a list of cells, each a record of two numbers and a string, and returning 0 at once: so the time is the boundary's alone. "Typed" annotates the parameter, (cells : [Cell]); "untyped" does not.
| Cells | Call | The package's build | lib-floor | Change | lib-closures |
|---|---|---|---|---|---|
| 64 | typed | 1.49 ms | 0.62 ms | -59% | 0.59 ms |
| 64 | untyped | 1.08 ms | 0.33 ms | -70% | 0.31 ms |
| 256 | typed | 6.28 ms | 2.16 ms | -66% | 2.10 ms |
| 256 | untyped | 4.37 ms | 1.18 ms | -73% | 1.15 ms |
| 1,024 | typed | 34.5 ms | 10.2 ms | -71% | 10.3 ms |
| 1,024 | untyped | 21.4 ms | 4.61 ms | -78% | 4.52 ms |
| 4,096 | typed | 139.2 ms | 51.3 ms | -63% | 52.1 ms |
| 4,096 | untyped | 107.5 ms | 18.3 ms | -83% | 18.1 ms |
lib-closures includes lib-floor's change, and reads the same here: this call holds nothing, so the closures have nothing to save.
Space#
The bytes one call allocates: the growth of the heap over a single call, made just after a forced collection, with a young generation large enough that no collection runs during the call. The 4,096-cell calls are left out: there the package's build allocates more than that space holds, a collection runs during the call, and the reading is too low.
| Cells | Call | The package's build | lib-floor | Change |
|---|---|---|---|---|
| 64 | typed | 5.2 MB | 1.3 MB | -74% |
| 64 | untyped | 4.6 MB | 752 KB | -84% |
| 256 | typed | 20.5 MB | 5.1 MB | -75% |
| 256 | untyped | 18.2 MB | 2.8 MB | -85% |
| 1,024 | typed | 80.4 MB | 20.2 MB | -75% |
| 1,024 | untyped | 71.2 MB | 11.0 MB | -85% |
Holding a value#
A Hazel function returns a function that holds a list of numbers and reads it on every call (it asks whether the list is empty), as a rule over a held world would. JavaScript then calls the returned function.
| Numbers held | the package's build | lib-floor | lib-closures |
|---|---|---|---|
| 64 | 0.11 ms 551 KB | 0.11 ms 557 KB | 0.034 ms 93 KB |
| 256 | 0.35 ms 1.9 MB | 0.35 ms 1.9 MB | 0.025 ms 93 KB |
| 1,024 | 1.30 ms 7.4 MB | 1.34 ms 7.4 MB | 0.024 ms 93 KB |
| 4,096 | 5.31 ms 29.0 MB | 5.81 ms 29.1 MB | 0.025 ms 93 KB |
The package's build and lib-floor pay for the whole list on every call, in time and in memory, because the list was written into the function's body as a literal. lib-closures keeps the list in the closure's environment, so a call costs the same whatever it holds. On ^ticket(558)'s laptop measurements, Messed Up Dress Up's World rule, its map held as a tree, went from 90 to 0.75 ms a move at 64×64 the same way.
Handles#
The handles trial is built on a branch that counts the evaluator's steps, so its times are not comparable with the other builds'. Within itself, the same typed call, given the cells each time and given a handle made once:
| Cells | Typed, converted each call | Typed, given a handle |
|---|---|---|
| 64 | 0.75 ms | 0.026 ms |
| 256 | 2.51 ms | 0.025 ms |
| 1,024 | 11.3 ms | 0.030 ms |
| 4,096 | 66.5 ms | 0.024 ms |
What this does not show#
- The browser. These are Node's numbers on GitHub's machines.
^report(25)'s and the trials' own laptop numbers agree on the direction and roughly on the size. - The game's real rules, beyond what
^ticket(558)measured. The functions here do nothing but return, so the boundary is all there is to measure. - What a held value keeps. The driver also reads the heap kept by a held function, but its readings are noisy at these sizes; all three builds keep about the list itself, about 1.9 MB for 4,096 numbers.
- Correctness. That is CI's: each trial has Fast checks and Full tests green, with tests of its own (curried functions, functions a call returns, handles in a call and in an argument tuple).
How this was put together#
- The driver is
bench/boundary.mjsin hazelgrove/hazeljs, run by its Boundary benchmark workflow, which anyone can start. A call's time is the median of at least 15 calls, and of at least 300 ms of them, after 3 to warm up. Its allocation is the median of 7 single calls, each after a forced collection (node --expose-gc --max-semi-space-size=256). - The run: 2026-11-03, workflow run 41693524506, GitHub's ubuntu-latest runner, Node 22. Two rounds, each running the four builds one after another; the tables show the mean of the two. The rounds agree within about 10%, except the typed calls at 1,024 cells on the trials (8.8 to 11.8 ms).
- The builds: the package's is hazeljs 0.0.3's
hazel-lib.jsfrom jsDelivr (hazelgrove/hazel at 0d66c36618, #2719). The trials' are Hazel's own Deploy builds from build.hazel.org: lib-floor at 409e8f2116, lib-closures at 7563919f60, and the handles trial at 0220562cee. - The data: in this page (
script#report-data) and in040/data/boundary.json, each round's numbers kept. - 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 in040/data/.