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.

  1. 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%).

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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:

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.

0.1 ms 1 ms 10 ms 100 ms 64 256 1,024 4,096 cells passed from JavaScript the package's build, typed the package's build, untyped lib-floor, typed lib-floor, untyped
Figure 1. Milliseconds a call against the number of cells passed, both on log scales; the mean of two rounds. Solid lines are typed calls, dashed lines untyped. The trial (lib-floor) is below the package's build at every size, and further below without an annotation.
CellsCallThe package's buildlib-floorChangelib-closures
64typed1.49 ms0.62 ms-59%0.59 ms
64untyped1.08 ms0.33 ms-70%0.31 ms
256typed6.28 ms2.16 ms-66%2.10 ms
256untyped4.37 ms1.18 ms-73%1.15 ms
1,024typed34.5 ms10.2 ms-71%10.3 ms
1,024untyped21.4 ms4.61 ms-78%4.52 ms
4,096typed139.2 ms51.3 ms-63%52.1 ms
4,096untyped107.5 ms18.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.

CellsCallThe package's buildlib-floorChange
64typed5.2 MB1.3 MB-74%
64untyped4.6 MB752 KB-84%
256typed20.5 MB5.1 MB-75%
256untyped18.2 MB2.8 MB-85%
1,024typed80.4 MB20.2 MB-75%
1,024untyped71.2 MB11.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 heldthe package's buildlib-floorlib-closures
640.11 ms
551 KB
0.11 ms
557 KB
0.034 ms
93 KB
2560.35 ms
1.9 MB
0.35 ms
1.9 MB
0.025 ms
93 KB
1,0241.30 ms
7.4 MB
1.34 ms
7.4 MB
0.024 ms
93 KB
4,0965.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:

CellsTyped, converted each callTyped, given a handle
640.75 ms0.026 ms
2562.51 ms0.025 ms
1,02411.3 ms0.030 ms
4,09666.5 ms0.024 ms

What this does not show#

How this was put together#