Browse documentation

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.set against o[k] = v in 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 in opCall in 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 to opDefFieldC. It is a name, not a binding, as @putByValDirect is 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 as O[P] = V does. The call's value is undefined. Any other use of the name, and any use in a script, is an ordinary identifier.
  • opDefFieldC stores 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 (a TypeError otherwise) and converts an integer key without making its string. FastLoop runs it (FastOp); the optimizing tier embeds it (OEmbedSets).
  • CreateDataPropOrThrow sends an index of an array or of an object with elements, and an array's length, to the object's [[DefineOwnProperty]] (CreateDataHook) when the fast store does not apply, so a class field on an array never reaches Array.prototype and 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, structuredClone and the Intl objects handed to scripts.

Consequences

  • CSV and TSV records keep Object.prototype and 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 --prod builds of main and this change (benchmarks/csv.js, tsv.js, Linux x86-64), every benchmark's median was faster: CSV.parse by 1-13%, TSV.parse by 5-16%, parsing with a reviver by 11-14%, stringify by 1-6%.
  • The other preludes' defines cost less: in the same kind of runs Object.fromEntries was 97% faster, Array.fromAsync 52%, Intl.NumberFormat formatToParts 49%, an iterator helper's toArray 35%, Iterator.zipKeyed 28% and JSON5.parse 8%. Counted with callgrind, Object.groupBy ran 4% fewer instructions, JSON5.parse 6% and a class definition with decorated fields and addInitializer 14%.
  • 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 write CreateDataPropertyOrThrow(o, k, v); other attributes still need defineProperty.
  • 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.js traps CSV and TSV records, rows and lists, indices included; tests/language/classes/field-definition-array-receiver.js covers class fields on arrays in the compiled tiers.