Preludes define data properties in one instruction
Date: 2026-11-29
Area: engine, compiler, preludes
Related: #2218, #2369, Adding built-in types
Context
ECMA-262 builds a built-in's results with CreateDataProperty and CreateDataPropertyOrThrow (ES2026 §7.3.5 and
§7.3.6): the object's [[DefineOwnProperty]] with a writable, enumerable, configurable data descriptor, which never
consults the prototype chain. The JavaScript preludes had two ways to get that. Object.defineProperty with a
descriptor object is exact but slow: a native call and a descriptor allocation per property, about 130 ns against
29 ns for an assignment in a micro-benchmark of five-field records on Linux x86-64. An object or list without a
prototype, filled by assignment and given its prototype afterwards (setPrototypeOf), is cheaper per field but
rebuilds the shape once per object. CSV and TSV parsing makes one record or row per line, so it kept plain
assignment and index stores, which a setter or index accessor a script adds to Object.prototype or
Array.prototype intercepts (#2218): the setPrototypeOf version made it 7-21% slower.
Other engines give their self-hosted code a direct define: JavaScriptCore's builtins write @putByValDirect, which
its bytecode generator turns into one put_by_val_direct instruction (ArrayConstructor.js); SpiderMonkey's
self-hosted code calls the DefineDataProperty intrinsic (builtin/Array.js, Object.js); V8's Torque calls
CreateDataProperty (object-fromentries.tq).
The engine already has the instruction: a class's computed field compiles to opDefFieldC,
CreateDataPropertyOrThrow(R[A], ToPropertyKey(R[C]), R[B]) (§7.3.32 DefineField), which every tier runs: Execute,
the baseline JIT through JH_DefField, and the optimizing tier by embedding the baseline's code. Its
CreateDataPropOrThrow stored an array's index with SetElemV, a [[Set]], and treated an array's length as a new
property (#2369).
The alternatives:
- A native function the preludes capture, as they capture
Reflect.apply. A call of a native function costs about 45 ns more than an assignment (Reflect.setagainsto[k] = vin the same micro-benchmark), so CSV parsing would still be slower than today's assignments. - Recognize the captured native at run time, as the call paths recognize
Function.prototype.call: no call frame, but a check inopCallin FastLoop, the JIT's call stub and the optimizing tier, and still an argument shuffle per property.
Decision
- In a prelude,
CreateDataPropertyOrThrow(O, P, V)compiles toopDefFieldC. It is a name, not a binding, as@putByValDirectis in JavaScriptCore: the compiler lowers a call of it when it compiles a prelude (CompilingPrelude) and no declaration shadows the name, evaluating O, P and V in order and reading each in place asO[P] = Vdoes. The call's value is undefined. Any other use of the name, and any use in a script, is an ordinary identifier. opDefFieldCstores an index straight into the elements when it can: an existing element, a hole of an extensible object, or an append to an extensible dense array within its capacity (DefineElemFast, inline in FastLoop). It checks that its receiver is an object (aTypeErrorotherwise) and converts an integer key without making its string. FastLoop runs it (FastOp); the optimizing tier embeds it (OEmbedSets).CreateDataPropOrThrowsends an index of an array or of an object with elements, and an array'slength, to the object's [[DefineOwnProperty]] (CreateDataHook) when the fast store does not apply, so a class field on an array never reachesArray.prototypeand a frozen or non-extensible array refuses it (#2369).- The preludes use it for their records and lists and wherever the specification says CreateDataProperty or
CreateDataPropertyOrThrow: CSV and TSV,
builtins5.js(Array.fromAsync,Object.groupBy,Object.fromEntries,Promise.allKeyed, the decorator helpers' lists, pattern matching's rest and slices), JSON5 parsing, the iterator helpers' arrays,structuredCloneand the Intl objects handed to scripts.
Consequences
- CSV and TSV records keep
Object.prototypeand their rows stay arrays, and no setter or index accessor a script adds to either prototype sees them being built. In seven alternating rounds of--prodbuilds ofmainand this change (benchmarks/csv.js,tsv.js, Linux x86-64), every benchmark's median was faster:CSV.parseby 1-13%,TSV.parseby 5-16%, parsing with a reviver by 11-14%,stringifyby 1-6%. - The other preludes' defines cost less: in the same kind of runs
Object.fromEntrieswas 97% faster,Array.fromAsync52%,Intl.NumberFormatformatToParts49%, an iterator helper'stoArray35%,Iterator.zipKeyed28% andJSON5.parse8%. Counted with callgrind,Object.groupByran 4% fewer instructions,JSON5.parse6% and a class definition with decorated fields andaddInitializer14%. - Defining a property in a prelude costs about what an assignment costs. A prelude that writes
Object.defineProperty(o, k, { value: v, writable: true, enumerable: true, configurable: true })should writeCreateDataPropertyOrThrow(o, k, v); other attributes still needdefineProperty. - The name works only in a prelude. A small callee that uses it is not inlined by the optimizing tier (an embedded
instruction of an inlined callee is limited to
OINL_EMBED), so a hot helper writes the call at each site rather than in a one-line wrapper. tests/built-ins/prelude-intrinsics/prototype-additions.jstraps CSV and TSV records, rows and lists, indices included;tests/language/classes/field-definition-array-receiver.jscovers class fields on arrays in the compiled tiers.