Shapes
V8 assigns every object a hidden class ("shape") based on its keys and the order they are added. Property reads stay fast when an access site sees a single shape (monomorphic); past four shapes, V8 falls back to a slow dictionary lookup (Maglev, System Analyzer). Keeping objects of the same kind on one shape preserves fast property loads everywhere.
In short
- Initialize all keys upfront in the same order for every instance of a type.
- Set absent fields to
undefinedrather than adding keys conditionally later. - Avoid
delete: assignundefinedfor fixed records, or use aMapwhen keys are dynamic. - Automatic in SAFE-TS: enum constructors and
parseJson<T>always construct monomorphic objects in declared key order.
❌ Rejected: eight shapes, one for each combination of optional keys a reading got
✕ Rejectedstable-shape
ts
export function read(i: number) {
return {
value: i,
...(i % 2 === 1 ? { note: "estimated" } : {}),
...(i % 3 === 1 ? { unit: "C" } : {}),
...(i % 5 === 1 ? { sensor: "north" } : {}),
};
}
export type Reading = ReturnType<typeof read>;
export function total(readings: readonly Reading[]): number {
let sum = 0;
for (const r of readings) sum += r.value;
return sum;
}(A declared type that makes the keys optional, note?: string, makes up to two such spreads acceptable: four shapes at most, a choice the type states. Three keys, as here, are reported either way. See Leaving out keys.)
✅ Accepted: one shape; absent values are undefined
✓ Accepted0 diagnostics
ts
import type { Option } from "@evobug_com/safe-ts-std/result";
export type Reading = {
readonly value: number;
readonly note: Option<string>;
readonly unit: Option<string>;
readonly sensor: Option<string>;
};
export function read(i: number): Reading {
return {
value: i,
note: i % 2 === 1 ? "estimated" : undefined,
unit: i % 3 === 1 ? "C" : undefined,
sensor: i % 5 === 1 ? "north" : undefined,
};
}
export function total(readings: readonly Reading[]): number {
let sum = 0;
for (const r of readings) sum += r.value;
return sum;
}Benchmark
total(readings)Code that runs
Each version runs on its own, in a fresh copy of this code (TypeScript types are removed first). The setup runs once per version; the last line is what is timed.
Several shapes
export function read(i: number) {
return {
value: i,
...(i % 2 === 1 ? { note: "estimated" } : {}),
...(i % 3 === 1 ? { unit: "C" } : {}),
...(i % 5 === 1 ? { sensor: "north" } : {}),
};
}
export type Reading = ReturnType<typeof read>;
export function total(readings: readonly Reading[]): number {
let sum = 0;
for (const r of readings) sum += r.value;
return sum;
}One shape
import type { Option } from "@evobug_com/safe-ts-std/result";
export type Reading = {
readonly value: number;
readonly note: Option<string>;
readonly unit: Option<string>;
readonly sensor: Option<string>;
};
export function read(i: number): Reading {
return {
value: i,
note: i % 2 === 1 ? "estimated" : undefined,
unit: i % 3 === 1 ? "C" : undefined,
sensor: i % 5 === 1 ? "north" : undefined,
};
}
export function total(readings: readonly Reading[]): number {
let sum = 0;
for (const r of readings) sum += r.value;
return sum;
}Setup and timed expression
const readings = Array.from({ length: 10_000 }, (_, i) => read(i));
// timed:
total(readings); Warm: median time per run once V8 has optimized the code (p10–p90: the spread of the samples). First run: the very first call, before any optimization, which is closer to code that runs rarely. Measured in a Web Worker in your browser; results depend on the engine and the machine, which is the point of running it here.
Size: Several shapes 395 B (209 B gzip) · One shape 568 B (214 B gzip).
Measured on Node 26: 3.1–4.4 ns per read with eight shapes against 0.64–0.97 ns with one, about 5x. With two optional keys (four shapes) the read stays within V8's limit and the gap is small (about 1.4x): the cost comes from passing four.
What SAFE-TS does for you
- Enum constructors put fields in the order the
Enumdeclares them, soShape.Rect({ h, w })andShape.Rect({ w, h })build the same shape. parseJson<T>builds every result in the declared key order, whatever order the JSON had, and assigns optional fields instead of spreading them.stable-shapereports conditional spreads (also of a value or a function result with different keys), unless the object's type declares those keys optional and there are at most two of them, and properties added after an object or instance was created. It also reports a function that reads a parameter in a loop and gets object literals with more than four different key lists for it from the calls in its file.no-mutable-globalkeeps module-level values from being reassigned, which V8 would otherwise have to undo optimizations for (v8.dev).
Habits that keep shapes stable
- Initialize every field in the constructor or the class body, even to
undefined. - Build objects with all their keys in one literal, in the same order everywhere.
- Don't
deleteproperties: it moves the object to slow dictionary mode (v8.dev). Set them toundefined, or use aMapwhen keys come and go (no-object-dictionary). - Arrays of same-shaped plain objects also make
JSON.stringifytake its fast path (v8.dev).
Rules in this guide: stable-shape