Feature Library

Uniform spatial grid for 2D

0.41 CPU ms
A still of the Uniform spatial grid for 2D demo
Still
The live demo starts when it’s in view.Move the pointer over the field (or focus it and use the arrow keys) to move the probe · Cells shows what one query visits

The neighbour search under a 2D action game.

The neighbour search under a 2D action game: hundreds of bodies dropped into a uniform grid every tick in O(n) (a head per cell and a next per body, two typed arrays), then asked "who is near this circle?" by visiting only the cells it touches. The answer lands in a scratch array, so a tick of pushes, hit tests and nearest-enemy searches allocates nothing. A cell query (a superset) and an exact one; plain functions over typed-array columns, no dependencies, tested against brute force.

What it costs a frame

0.41 ms
GPU
not used
CPU, median
0.41 ms
CPU, 95th pct
0.69 ms
Draw calls
0
Weight
Light

Measured 21 November 2026 on RTX 4060 Laptop, 13th Gen Intel(R) Core(TM) i9-13900H, Chrome 157, 1920×1080 at 1×. 600 frames × 3 rounds, median kept (rounds ranged CPU 0.40–0.42 ms). What ran: Alone: 900 points drift, the grid is rebuilt every frame and the probe sweeps the field with one exact circle query; the frame includes the Canvas 2D drawing. Phones: tiers are being measured next.

How steadily it runs on RTX 4060 Laptop · 13th Gen Intel(R) Core(TM) i9-13900H · reference

Typical frame ?
16.77 ms
1% low ?
54 fps
Varies by ?
±0.45 ms
All metricsRTX 4060 Laptop · 13th Gen Intel(R) Core(TM) i9-13900H · reference
p50p95p9916.02 ms18.44 ms +
How long each frame took. Most frames sit in the tall bars; a long tail to the right is hitching.

Frames run on a 60 Hz timer (a page demo), so most frames take about one tick, 16.7 ms: the spread around it is what matters.

Frames

Median (p50) ?
16.77 ms
p95 ?
17.66 ms
p99 ?
18.07 ms
Slowest ?
18.96 ms
Mean ?
16.82 ms
Standard deviation ?
0.45 ms
1% low ?
54 fps
0.1% low ?
53 fps
Hitches ?
0 of 600
Over budget ?
0 of 600

Main thread

Long tasks ?
0
Long frames ?
0
Event-loop delay ?
0.1 / 0.9 / 1.1 ms

Input

No input during this run.

Memory

JavaScript heap ?
34.3 → 34.6 MB
Growth ?
+0.36 MB

Browser

JavaScript a frame ?
0.56 ms
All tasks a frame ?
1.04 ms
Layout / style ?
0.05 / 0.01 ms
Main thread busy ?
6%

Garbage collection

Collections ?
0 (0 minor, 0 major)
Pauses ?
0.0 ms in all, longest 0.0

What it needs

Renders with
Canvas 2DA 2D canvas: every current browser.
Your browser
Runs hereNo WebGPU needed
Depends on
NothingNo other packages
With an agent
install("spatial-grid-2d")Your coding agent reads its pitfalls and installs it through the library’s MCP server. Set it up

What it’s for

  1. A swarm that keeps apart, so hundreds read as many and not one blot
  2. Hit tests for shots, beams and blasts against a crowd
  3. The nearest enemy for auto-aim, or pickups within a magnet radius
  4. Particles or boids that react to their neighbours
  5. Hover and lasso picking over thousands of points on a 2D chart or map

Pitfalls, in brief

  1. g.hits is shared scratch: the next query overwrites it.
  2. queryCircle is a superset (everything in the touched cells): test the distance yourself, or call queryWithin.
  3. Rebuild once a tick, after everything has moved and before anything queries.
  4. Indices are positions in your columns.
  5. Pick the cell near your common query radius.
1 more in brief
  1. The capacity is fixed when you create it (nextIn and hits are that long); a query that finds more than that stops at capacity.
6 pitfalls in full, the hook-up guide, the module source and its tests come with the part.

Where it comes from

From another of the maker’s apps. Fully custom code.

spatial hashuniform gridbroad phaseneighbour searchcollisionseparationswarmtyped arraysno allocationgame2dcanvas 2dno dependencies

Near it in the frame