Hazel reports · ^report(64) · Gnome world · 2026-11-24 · work in progress · after ^report(63)

Gnome World: Which Parts Are Hazel

Matthew asked, on 2026-11-24: “what parts of gnome world logic are expressed in JS vs Hazel? event handling? physics? global game world state? procedural rendering of trees, and other elements?” This report answers for Gnome world 0.0.103 (matthewhammer/hazeljs-examples 02b73ab), its rules compiled in the page by hazel-jit from the hazeljs branch’s build (#2857).

Outline#

  1. The split: Hazel decides what the world is and how the gnome moves; JavaScript keeps the state, takes the input, moves everything else and draws.
  2. One step of the world: Hazel is called once for the gnome and, every sixth step, once a beaver; its share of the step’s time is under 1%.
  3. Event handling: every input is JavaScript’s; Hazel sees one record a step and answers with one.
  4. Physics: the gnome’s movement is Hazel; the water, the boulders, the animals and the rabbit’s rules around it are JavaScript.
  5. The world’s state: all of it in JavaScript; Hazel keeps nothing between calls.
  6. Procedural drawing: Hazel plans where things are and grows the roots; JavaScript draws everything.
  7. Why the line falls there: what Hazel in a page can and cannot do well today.
  8. What next, and how this was put together.

The split#

Hazel decides what the world is and how the gnome moves; JavaScript does everything that touches the tiles, the screen or the clock. By size, the Hazel is 40,213 characters of code in 32 functions (World.hz, comments left out), and the JavaScript 443,696 in 160 functions (app.js): 92% of the code, 70% of it drawing.

HazelWorld.hz, 32 functionsthe world's plan: 13,835 charactersthe villagers' words: 11,107 charactersthe gnome's step: 8,825 charactersthe beavers and their dams: 3,378 charactersroots and mycelium: 2,541 charactersarithmetic: 526 characters40,213 characters (8% of the code)the world's plan 13,835the villagers' words 11,107the gnome's step 8,825the beavers and their dams 3,378roots and mycelium 2,541arithmetic 526JavaScriptapp.js, 160 functionsdrawing: 311,422 charactersdrawingsimulation: 49,885 characterssimulationbuilding the world: 37,253 charactersrules, input, setup: 20,972 characterssave and load: 10,852 charactersthe speed run: 10,344 charactersdrawing 311,422 (70%)simulation 49,885 (11%)building the world 37,253 (8%)rules, input, setup 20,972 (5%)save and load 10,852 (2%)the speed run 10,344 (2%)
Not a build of the app: the code of 0.0.103 (02b73ab) by size, in characters. Hazel by what each function decides; JavaScript by what each function does, grouped by name (the grouping is in How this was put together). Hover a part for its size.

The Hazel is a set of pure functions the page calls. Each takes small records and returns one:

Hazel functionwhat it decideswhen the page calls itms a call
World.planthe world from its seed: the land, the layers of rock, the water table, the rivers, the beavers' lodges and dams, the villages, the great trees, the trees, bushes, flowers and mushrooms, and the places under the ground (500 places)once a world, and at Restart and Load2.06
World.step (with finish, slope_at, slide_vx, dig_ahead, tile)the gnome's step: running, jumping, falling, climbing, swimming, the slopes, diggingevery step: 60 a second0.016
World.beavera beaver's next job: rest, go to a tree, gnaw it, cut a log, drag it to the dam, lay itevery sixth step, for each of five beavers: 50 a second0.0017
World.water_rowhow high a dam holds its river, by its logswhen a dam's logs change0.0004
World.greeta villager's words: 768 greetings, from 166 lines, none said by two villagerswhen the gnome stops by a villager0.0068
World.roots, World.threadsa tree's roots and its mycelium, as linesthe first time a tree is in view1.26
World.ground, World.dig_stepseach kind of ground: its pores, how water flows through it, its color, the steps to dig it with each diggeronce a world (90 digging steps kept)—
World.village, World.furnish, World.limbs, World.stylesa village's rooms and their things; a great tree's limbs and what is on each; the sixteen styles of tree houseonce a world—

The times are each function compiled by hazel-jit, called in the page 2,000 times (the plan 5, the roots 20) and averaged, once, on the laptop.

One step of the world#

In a step (a sixtieth of a second), the page calls Hazel once, for the gnome, and every sixth step once for each of the five beavers; everything else in the step is JavaScript. World.step takes 0.016 ms of a step of about 2.4 ms before drawing; the water alone takes 2.2 ms. Then the frame is drawn, all of it in JavaScript.

Hazel (compiled by hazel-jit)JavaScriptkeys → PadS.keys packedinto a Pad recordWorld.stepthe gnome:0.016 mseventsdigOut, sounds,heartsboulderswater2.2 ms:every wet tileWorld.beaver ×5every 6thstepwind, squirrelssmoke, leavestoovillagersthey walkin JSwildlifebirds, frogs,insectsbunnies ·Clovermusicevery 15thstepdrawthe frame:canvasTo scale, the step itself (2.4 ms, before drawing): the water: 2.2 msthe rest of the JavaScript: about 0.2 msWorld.step: 0.016 mswater 2.2 ms · other JavaScript ≈ 0.2 ms · Hazel 0.016 ms (the thin purple line at the end)
Gnome world 0.0.103, one step in the order tick runs it (the yellow bar sweeps across it), then the drawing; Hazel’s parts on the top lane. Below, the step to scale: the water, the rest of the JavaScript, and World.step, a line too thin to see. The step’s times were measured during 0.0.102’s development, after its fixes, with temporary timing; the Hazel call’s, at 0.0.103.

Event handling#

Every input is JavaScript’s; Hazel sees one record a step and answers with one. Listeners for keys and for the pad’s buttons set S.keys. Each step the page packs the held keys into a Pad record (dx, up, down, jump, dig) and calls World.step(gnome, near, pad). The step answers with the gnome as it is now, its ev saying what happened, and the page makes the effects: the dug tile taken out of the arrays (digOut), a sound, a heart lost.

keydown ←→↑↓ XS.keysPad recordWorld.stepGnome record, eveffectsa listener sets a keyheld keys, in the pagedx, up, down, jump, digwith Gnome and Near: 3records inev: 0 nothing, 1 dug, 2jump, 3 splash, 4 nobreath, 5 divedigOut, a sound, aheartJavaScript only, never through Hazel: B (build a plank, a bridge, a ladder), R (ride Clover), M (the map), 💾 📂 🔄, and 🏃 the speed run,which steers the gnome by pressing the same keys a player would (it plans its way over the tiles in JavaScript).
Gnome world 0.0.103: a key press’s way into Hazel and back; JavaScript’s boxes teal, Hazel’s purple. The Gnome record has 16 fields; Near, the tiles round the gnome, 7; Pad 5.

Building (B), riding (R), the map, saving, loading and restarting never pass through Hazel. Nor does the speed run’s planning: it finds its way over the tiles in JavaScript and then presses the keys, so the gnome it drives still moves by World.step.

Physics#

The gnome’s movement is Hazel; every other thing that moves is JavaScript. World.step moves a box 0.66 tiles wide and 0.88 tall in thousandths of a tile: running at 92 a step, eased; falling 11 faster a step, to 267 at most; a jump of −208 cut short when up is let go; climbing at 70; floating, swimming and diving; walking on the diagonals of sloped banks and sliding down them; digging the tile pushed into. It sees the world only as the 3 by 5 tiles round the gnome, packed into five whole numbers.

On foot: what the page packs (Near: r0 to r4)000003200320🧑‍🌾0161116r0 = 0 + 512·0 + 262144·0 = 0r1 = 0 + 512·0 + 262144·32 = 8,388,608r2 = 0 + 512·0 + 262144·32 = 8,388,608r3 = 0 + 512·0 + 262144·16 = 4,194,304r4 = 1 + 512·1 + 262144·16 = 4,194,817On Clover, without the pack or the gear (pulsing: changed)0000000000🐇0101116r0 = 0 + 512·0 + 262144·0 = 0r1 = 0 + 512·0 + 262144·0 = 0r2 = 0 + 512·0 + 262144·0 = 0r3 = 0 + 512·0 + 262144·10 = 2,621,440r4 = 1 + 512·1 + 262144·16 = 4,194,817A tile: 0 to 11 what is there (1 to 7 ground, 10 a boulder, 11 a platform), +16 water, +32 climbable, +64·shape for a slope. On Clover the page clears +32 (no ladder, no tree, no tunnelto climb without the pack) and makes the water ahead a wall (10) outside a leap, so World.step, unchanged, stops her at the water’s edge.
Gnome world 0.0.103: the window World.step sees (Near), each row of three tiles one number, 512 apart. Right, the same window as the page gives it when Clover carries the gnome without the climbing pack or the diving gear; the pulsing tiles are the ones it changed: the ladder’s +32 gone, the water ahead a wall.

JavaScript moves the rest, each step: the water seeping through the ground (every wet tile, down and then across), the boulders falling, the pebbles, the wind and its gusts, the leaves, the birds’ flights, the squirrels along the branches, the frogs’ jumps, the bunnies’ hops, the villagers walking and a felled tree falling. The beavers are both: World.beaver decides each one’s job, and JavaScript eases its body toward where Hazel put it.

Clover the rabbit is Hazel’s gnome step with JavaScript rules around it. The page changes what the step sees (the window above), what it is asked (twice as fast: dx 2) and what comes back: a hop’s or a leap’s speed set after the jump, the digging’s progress tripled, a landing in the water undone. It went in JavaScript because World.hz is full: one more top-level definition overflowed the browser’s stack building the module on 2026-11-23.

The world’s state#

All of it lives in JavaScript; Hazel keeps nothing between calls. One object, S, holds the gnome, the rivers and dams, the trees, the beavers, the villagers, the animals, Clover, the weather, the camera and the speed run; nine arrays hold the 186,368 tiles (896 by 208): what is there, its water, what is behind and on it, gems, stones, slopes and roots, and the top of each column, about 2.05 MB.

JavaScript: all the stateS: the gnome, the hero, hearts, gems foundrivers and dams, beavers, fallen trees, piles of logstrees (alive, growing), villagers, the weather, windbirds, frogs, insects, squirrels, bunnies, Cloverthe camera, the speed run, the music's placetiles: 9 arrays × 186,368 (896 × 208) what is there, its water, behind, on it, gems, stones, slopes, roots; the top of each column≈ 2.05 MB; a save gzips them, with S's partsWorld.planseed → the plan (500 places)once a worldWorld.stepGnome, Near, Pad → Gnome60 a secondWorld.beaverBeaver, trees, piles → Beaver50 a secondWorld.water_rowthe river, its bank, the logs → a rowa dam changedWorld.greetwho, where, … → Talkstopping byWorld.rootsa tree → [Line]first in viewHazel keeps nothing between calls: every record comes in, and goes back out.
Gnome world 0.0.103: the state in JavaScript, and the Hazel functions it is passed to, each with the records it takes and gives back, and how often.

So saving is JavaScript’s: a save file is the seed and a snapshot of S and the tiles, and the GitHub check hashes its 36 parts before a save and after a load. The plan is never saved: Hazel makes it again from the seed.

Procedural drawing#

Hazel plans where things are and grows the roots; JavaScript draws everything, 311,422 characters of canvas code.

the ground’s surface; the oak’s foot at the middle617 lines of root, 245 of mycelium (grey), as Hazel returned them, grown outward from the foot; widths to scale. World.roots took 1.3 ms for a tree this size.
Gnome world 0.0.103: an oak 29 tiles tall, its roots (617 lines) and mycelium (245) as World.roots and World.threads returned them, grown outward from its foot when the figure comes into view.

Why the line falls there#

Hazel takes what is a function of small records; JavaScript keeps what is big, changing or drawn. What Hazel in a page cannot do well today decides most of the rest:

What next#

  1. Clover’s ride in Hazel. A Ride module of its own, calling nothing in World: the hop, the leap, and what she may climb or swim, from the same window of tiles.
  2. The animals’ minds in Hazel, as the beavers’. A bird’s or a bunny’s next move from a small record: where it is, how far the gnome is, the perches near.
  3. The water in Hazel, once Hazel has arrays. With ^ticket(846)’s sequences, a row at a time, timed against the JavaScript’s 2.2 ms.
  4. Tree shapes as Hazel geometry, like the roots. World.limbs already plans a great tree’s limbs; the drawn limbs could come back from Hazel as lines.
  5. Time the crossing. Gnome, Near and Pad go in and a Gnome comes out 60 times a second; with the converters by declared type (#2857), measure what that costs.

How this was put together#

Read World.hz, preamble.hz and app.js at matthewhammer/hazeljs-examples 02b73ab (0.0.103). Sizes: each Hazel top-level definition’s code with its comments taken out; the JavaScript’s top-level functions grouped by name (drawing: draw*, paint*, sprites, the gnome, birds, rabbits, trees, the HUD; simulation: tick and the steps it calls, the ride; building the world: build and its helpers; the speed run: bot*; save and load; the rest). Times: in the page with ?test=1, on the laptop, in the app’s browser pane (Chromium), the rules compiled by hazel-jit from build.hazel.org’s hazeljs build; each Hazel function called the stated number of times and averaged, once (one run is not a trend). The step’s 2.4 ms and the water’s 2.2 ms come from timing added for a while during 0.0.102’s development. The roots are World.roots’ and World.threads’ own output for the first oak of world 7.

The data#