Trending

World technology news

→
JavaScript Float16Array: What a height map loses when packed
Software Engineering · 2025
Software Engineering2025

JavaScript Float16Array: What a height map loses when packed

Measure the bytes saved by packing a height tile into Float16Array, locate the rounding error, and test when storing offsets preserves neighboring samples.

Archive retrospective · Topic year 2025

Developer's JournalPublished: Dec 4, 20266 min read
Reading progress0%
In this article
  1. Treat a smaller tile as a change in resolution
  2. Inspect the available values around the tile
  3. Change coordinates before choosing the packing format
  4. Run the conversion and capture its result
  5. Specify the bytes on the wire
  6. Define acceptance with the people consuming the data

Treat a smaller tile as a change in resolution

Changing the type of a height array can lower the storage cost of a streamed terrain tile. It also changes which neighboring elevations the viewer can distinguish. Draw a hill with three nearby sample heights and an abrupt step may replace a smooth grade after conversion. Both the package size and the quality of the geometry belong in the release decision. The May 2025 Node.js 24.0.0 announcement lists Float16Array among the features enabled with V8 13.6. That release supplies the historical context for this experiment. The numbers below are intentionally chosen to isolate a rounding threshold, as a terrain survey would give too many variables for a first check. Think of the samples as meters along one row of a tile and ask where the comparison first stops preserving the original detail.

Inspect the available values around the tile

At an elevation of 1024, the next higher binary16 value is 1025. A measurement of 1024.125 therefore becomes 1024, as does 1024.375. The neighboring value 1024.625 ends up at 1025. If this part of a tile is passed to a renderer, two distinct samples have already been collapsed by the time it interpolates colors and lighting. Looking at the average value of the tile would hide that distinction. The half-step cases make the rule visible. 1024.5 becomes 1024 while 1025.5 becomes 1026. The rounded representation picks the even choice on a tie. Design a handful of checks for the extremes you care about, including representative negative values and missing data. Values a long way above or below this elevation have different spacing and deserve their own sample set.

Frontend data and event flowConceptual sketch · summary of the article's technical flow
Illustrative diagram by Yazılımcının Günlüğü

Change coordinates before choosing the packing format

Instead of passing the absolute elevations straight into the packed array, reserve one value in the header for a tile base. Store the offsets from that base. Around this particular tile, the first offset is 0.125 and the largest is 1.5. Those are small enough here for all five offsets to be preserved exactly. Reconstruct the samples by adding the header value. The same recipe with a poor choice of base, or a much steeper tile, can have worse results. Treat the header as real overhead and choose tile extent with encoding and decoding costs in view. Small tiles make bases more local. They also put more boundaries and more metadata in a fixed area. Use two adjacent tiles to check for seams. A correct reconstruction within each isolated tile is insufficient evidence for a smooth transition across the boundary.

Run the conversion and capture its result

The program ran on Windows in Node.js 24.19.0. The reference array used 20 bytes for samples and the packed array used 10. Direct conversion gave a worst absolute error of 0.5. Adding the base to each stored offset recovered all five original values. Little-endian DataView output for the first packed value was 00 64. The code checks the entire value list as well as the byte order so swapping those bytes does not look like a successful round trip. The runtime here is a later patch in the Node 24 line. This exercise establishes behavior on that installation. It leaves the original release build, browser support, GPU upload and decoding speed untested. Measure those separately if they affect the proposed viewer. The byte count excludes headers, framing and any compression that a real delivery format applies.

import assert from "node:assert/strict";

const base = 1024;
const input = Float32Array.of(1024.125, 1024.375, 1024.625, 1024.5, 1025.5);
const packed = Float16Array.from(input);
const offsets = Float16Array.from(input, height => height - base);
const restored = Float32Array.from(offsets, offset => offset + base);
const view = new DataView(new ArrayBuffer(2));
view.setFloat16(0, input[0], true);
const hex = Array.from(new Uint8Array(view.buffer), n => n.toString(16).padStart(2, "0")).join(" ");

assert.deepEqual([...packed], [1024, 1024, 1025, 1024, 1026]);
assert.deepEqual([...restored], [...input]);
assert.equal(hex, "00 64");
const worstError = Math.max(...input.map((height, i) => Math.abs(height - packed[i])));
console.log({float32Bytes: input.byteLength, float16Bytes: packed.byteLength, worstError, littleEndian: hex});

Specify the bytes on the wire

The typed array is convenient for local JavaScript. DataView gives a file writer a way of specifying byte order explicitly. A reader should use the same declared order. If a format is already in use, introduce a version or encoding identifier so an older reader can reject unfamiliar data cleanly. Validate the declared count against the length before allocating a decoded tile. A missing baseline matters as much as a truncated sample block. The cost model is equally explicit. With a four-byte base the five packed samples and their base would take fourteen bytes, compared with twenty bytes for the reference samples alone. For realistic tiles the base is amortized across many more values. Compute the final byte count on the files you actually serve. A format-level change can alter compression behavior even when the in-memory tile is smaller.

Define acceptance with the people consuming the data

A decorative map layer and a drainage model can assign very different importance to a half-meter error. Keep the resolution rule with the dataset. Ask who uses it, how close to a boundary the application makes decisions, and whether a tile can be reprocessed from a reference with more precision. Give the renderer a panel of challenging tiles and keep error summaries for the processing pipeline. Absolute error is useful, but there is more to check. Compare gradients across neighboring cells, track how often adjacent levels collapse and verify seams using the original coordinates. A set with sentinel values or unusually tall structures may need an encoding exception. The packed representation should be judged against measurable requirements for that layer. Once the requirements are written down, the same fixtures can guard a later change to a reader, a tile generator or a new transport.

890 reads ✎ Write a comment

Reader discussion

Discussion prompt

What would you change first for maintainability and testing in your own project?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.