Back to the journal

Porto 3D perf round 3: drawing only the road cells in view

The porto-3d road layer spans the whole city, so three.js drew it in full even when the camera looked out to sea. Perf round 3 on 17 November 2026 sorted the road triangles into grid cells and draws only the cells in view. Here is how, what it cost at load, and the numbers from the repo's BASELINE.

Product engineering / Three.js
Porto 3D perf round 3: drawing only the road cells in view

How do you stop three.js from drawing a mesh as wide as the city when most of it is behind the camera? This post is for developers who build large Three.js scenes from merged meshes. It follows perf round 3 of porto-3d, a 3D map of Porto. You get the cause, the cell sort that fixed it, its load-time price, and the measured results.

What the measurements showed before the change

The owner's goal for the round: "do not draw the whole map, only the visible part". The symptom was a #quest deep link that took 22.7 s on the live site before the route card showed.

The road layer is a dozen meshes and fat-line sets. Each spans the city, with a bounding radius of about 1650 units (6.6 km). three.js culls whole objects, so the roads drew in full in each view. With the camera on the sea and the city behind it, they drew 1.19 M primitives, none on screen. ## How cell-runs.js draws only the visible cells

The round 3 commits are dated 17 November 2026 and merged in fc5545f. In f1c589f, the roads draw only their 1 km cells in view, through src/cell-runs.js.

The file sorts an object's triangles or line segments by grid cell along a Morton curve, once, in place. The buffers keep their size. In the file's words: "no triangle is split or copied, so no seam can open between cells."

Each frame, the culler takes the cells whose boxes touch the view, and for a shadow caster also the shadow camera's view. Consecutive visible cells form runs. The closest runs merge until at most maxRuns remain. Each run is one draw of a child "slot" that shares the object's buffers.

export function cellRuns(object, { cell = 250, maxRuns = 3, gapShare = 0.06, pad = 0, min = 3000 } = {}) {

What the cell sort cost at load, and how it moved

The first version sorted inside the roads build step. That added 55 to 140 ms to the first full frame on the desktop profile and 340 to 650 ms on phone-low.

2cbd81b brought a counting sort over the grid cells, "3x faster, same order". 580d308 then moved the sort after ready, one road object per idle slot. The roads step came back to main's time. Until an object is sorted, it draws whole, as on main.

The price moved too. On phone-low the sort ends 1.4 to 1.9 s after ready and adds 1 to 3 long tasks of 52 to 92 ms.

The results: main against the branch

Both builds ran in turn in headless Chromium on ANGLE/Metal; figures are medians of three rounds. Main-pass primitives:

  • Sea view, city behind: 1.50 -> 0.21 M on desktop-gpu, 1.43 -> 0.15 M on phone-low.
  • Ribeira, street level: 2.72 -> 1.91 M on desktop-gpu, 2.07 -> 1.01 M on phone-low.
  • Route stop (São Bento): 2.12 -> 0.92 M on desktop-gpu, 1.91 -> 0.71 M on phone-low.

On phone-low, every view is faster at the median. The sea view went from 6.2 to 4.0 ms p50. Most close views now cost 15 to 25 more draw calls on phone-low. The São Bento stop costs 2 more (85 to 87). On desktop the p50 does not move: BASELINE says the GPU "is pixel-bound at DPR 2".

The route card opened in 1.25 to 1.30 s on desktop (main: 4.56 to 4.57 s) and 16.0 to 16.9 s on phone-low (main: 30.63 to 30.80 s). BASELINE expected that gain from opening the card with the landmarks. 4476621 does that in the same branch.

How the round checked that nothing went missing

scripts/cell-runs.test.mjs checks, for random cameras, that no triangle or segment whose box meets the view or the shadow camera is left out.

A screenshot test at four street-level spots turned the camera in 45° steps and drew each frame twice: culled, and with each road object whole. 64 of 64 pairs were pixel-identical. The log records node --test scripts/*.test.mjs at 32/32 and npm run verify at 16/16.

A measurement fault on the way

Uncapped, the app's 120 Hz frame limiter sometimes read the fast animation frames as a 120 Hz panel and drew every other frame. That made one round of phone-low numbers worthless, so the round dropped it. For the view numbers above, the round switched the limiter off. BASELINE warns that scripts/perf-bench.mjs --analysis "may carry the same halving" below class XL.

What is still open

The tram rails, a static InstancedMesh of 53 to 67 k triangles, still draw whole when part of them is in view. BASELINE says three.js "cannot draw a window of an InstancedMesh", and cells would add draw calls at the overview, where desktop is at 323 of its 330 budget. ## What is not measured

  • Real phones, other GPUs, Safari and Firefox. The log lists them as not checked.
  • The live site. The 22.7 s figure "cannot be reproduced here".
  • Phone figures come from one Apple M1 Max that emulates a phone profile's CPU, network and GPU load. Other work shared the machine during the runs.
  • The gain on GPUs where vertices cost: "this machine cannot show it".

How this post was made: an AI agent drafted it from the project files listed in the repo, a second agent reviewed it, and Timur merged it.

About the author

Timur Isachenko is an AI CTO and Principal Solution Architect working on AI-native products and distributed systems. Explore his engineering projects and technical books, or read more about his experience.

Keep following your curiosity.

Back to the journal.