Benchmarks
Compare how fast Golem's generated code parses, stringifies and validates, and what each feature costs.
WARNING
Benchmarks are for reference purposes only.
The page has two tabs.
Speed per payload shows the speed of Golem's three setups on each payload.
What each feature costs compares Golem with itself: validation on and off, IModel and IObject, many converted fields and none, and invalid input and valid input.
A column is one setup of Golem. Each setup turns JSON text into a value and back.
- Golem (IObject) uses an
IObjectdeclaration. It gives plain objects, checks every rule, rejects unknown properties and convertsTSerdefields. - Golem (IModel) uses an
IModeldeclaration. It gives instances of the model class, with validation off. - Golem (IModel, validated) uses the same declaration with Golem's own generated validators on.
| Payload and operation | Golem (IObject) | Golem (IModel) | Golem (IModel, validated) |
|---|---|---|---|
| Simple object 123 B | |||
| Parse | 1,552,734 | 899,310 | 845,847 |
| Stringify | 5,826,805 | 8,158,360 | 2,701,598 |
| Validate | 16,690,583 | does not apply | 3,785,311 |
| Nested object 817 B | |||
| Parse | 427,420 | 282,303 | 262,790 |
| Stringify | 892,727 | 1,044,795 | 549,429 |
| Validate | 5,672,010 | does not apply | 1,233,447 |
| Large array 100 KB | |||
| Parse | 3,958 | 2,585 | 2,474 |
| Stringify | 7,559 | 8,430 | 4,567 |
| Validate | 62,282 | does not apply | 9,830 |
| Checkout 347 B | |||
| Parse | 349,612 | 383,155 | 294,268 |
| Stringify | 214,137 | 566,853 | 206,733 |
| Validate | 1,363,907 | does not apply | 769,390 |
| Checkout, no conversion 347 B | |||
| Parse | 836,021 | 702,034 | 602,919 |
| Stringify | 1,283,463 | 1,944,584 | 988,859 |
| Validate | 4,101,862 | does not apply | 2,092,408 |
- Each number is operations per second. The bar under it shows the number against the fastest setup in its row.
- A dash (—) means the setup has no such operation.
Code
Declarations for the Simple object payload
export type TPlainPoint3D = IObject<{
x: number;
y: number;
z: number;
}>;
export type TPlainObjectSimple = IObject<{
scale: TPlainPoint3D;
position: TPlainPoint3D;
rotate: TPlainPoint3D;
pivot: TPlainPoint3D;
}>;export type TPoint3D = IModel<{
x: number;
y: number;
z: number;
}>;
export class Point3D extends model<TPoint3D>() {}
export type TObjectSimple = IModel<{
scale: Point3D;
position: Point3D;
rotate: Point3D;
pivot: Point3D;
}>;
export class ObjectSimple extends model<TObjectSimple>() {}Timed calls
const model = ops.parse(input.text);
const timed = {
'text-to-model': () => ops.parse(input.text),
'model-to-text': () => ops.stringify(model),
validate: () => {
if (!ops.validate(model)) throw new Error('IObject validate rejected a valid fixture');
return true;
},
};// Public v1.2 model class methods (ModelClass.parse, stringify, validate, fromWireFormat, toWireFormat).
fixtures.push({
name: input.name,
payload: input.payload,
operations: {
'object-to-model': () => ops.fromWireFormat(input.payload, validation),
'model-to-object': () => ops.toWireFormat(model, validation),
'text-to-model': () => ops.parse(input.text, validation),
'model-to-text': () => ops.stringify(model, validation),
...(validation ? {
validate: () => {
const ok = ops.validate(model);
if (!ok) throw new Error('Class validate rejected a valid fixture');
return true;
},
} : {}),
},- Simple object (123 B): Four 3D points. Measures per-call overhead. Source
- Nested object (817 B): A nested API record with integer, length, pattern and array-size constraints. Source
- Large array (100 KB): One company, 10 departments, 1,000 employees. Source
- Checkout (347 B): Money from a generated SDK type, hex code, dates, nested line items, flags, a union and optional fields, with value conversion. Source
- Checkout, no conversion (347 B): The checkout fields with dates as date-time strings, no value conversion and no custom rule. It keeps the union, the tuple, the map and the optional and nullable fields. Source
Against hand-written code
Hand-written code copies each field and converts each date, and checks nothing. The table shows Golem's speed with validation on as a fraction of that code's speed, so 1.00 means equal. Golem checks the value before and after each conversion. Parse is JSON.parse then the hand-written decode; stringify is the hand-written encode then JSON.stringify.
| Model | Decode | Encode | Parse | Stringify |
|---|---|---|---|---|
| Flat | 0.46 | 0.89 | 0.73 | 1.09 |
| Nested | 0.36 | 0.80 | 0.81 | 0.95 |
| Union | 0.28 | 0.77 | 0.55 | 1.02 |
| Array of 100 items | 0.46 | 0.92 | 0.73 | 0.90 |
| Tuple | 0.40 | 0.91 | 0.64 | 0.95 |
| Optional fields | 0.53 | 0.93 | 0.74 | 1.04 |
They are point-in-time v1.2.0 snapshots measured with npm run bench:handwritten. Golem uses a generated JSON string writer for stringify, while the hand-written baseline calls JSON.stringify without validation. Run npm run bench:handwritten to reproduce them on your machine.
Split SDK bundle size and build speed
These figures are point-in-time v1.2.0 snapshots reproducible via npm run bench:extras, which measures three extra dimensions on your machine:
- Split SDK validator bundle size (minified and gzipped with
esbuild): one split standalone operation validator vs the full 5-operation SDK, Zod, and Ajv. On Golem 1.2.0, one split validator bundles to 5.09 KB gzip (vs 20.1 KB for the full 5-operation SDK, 38.6 KB for Ajv, and 92.7 KB for Zod). - Full error collection (
failFast: false): Golem's single-pass full validator vs Zod, Ajv (allErrors: true), andclass-validator(stopAtFirstError: false). - Direct TypeScript-to-IR generation speed: wall time and emitted size across 10, 50, and 100 models (100 models generate in ~354 ms).
What is measured
A case is one payload, one operation and one setup. The tables show the median speed of a case in operations per second. The payloads differ in what they convert:
| Payload | What it converts |
|---|---|
| Simple object | Nothing |
| Nested object | Nothing |
| Large array | Nothing |
| Checkout | A few fields: money, a hex code and dates |
| Checkout, no conversion | Nothing |
| Serde none | Nothing, in 21 fields |
| Serde many | Every one of the same 21 fields |
| Union, tagged | One date in each of 100 events |
| Union, class-first | One date in each of 100 events |
| Temporal fields | All 20 fields, with the temporal-polyfill package as Temporal |
| Recursive model | Each of 106 nodes to its class |
| Unique strings | Nothing. Validate compares 1,000 strings |
| Unique objects | Nothing. Validate compares 100 objects |
| Unique small list | Nothing. Validate compares 3 objects |
| Unique nested objects | Nothing. Validate compares 20 objects that differ only in a nested field |
| Nest query | Six query strings to their declared types |
| Client call | The request body and the response of one generated client call |
The speed tab shows the first five payloads. The cost tab shows every payload. Nest query shows the Decode row only. Client call shows the Call row only: it runs the generated client against a stub fetch that builds a new Response, so its time includes that Response and no network.
The run is short, so read the ratios between setups in one run and not the absolute numbers. Do not compare numbers from two machines or from two runs.
- The same JSON text. Every setup gets the same JSON text for a case and parses it in its own way.
- Proof before timing. Each setup must turn its parsed value back into the original JSON. A setup that validates must reject each invalid variant of the payload. A setup that fails gets no number. These checks run in a separate process.
- One process for each setup, valid input only. Each setup is timed in its own process, on valid input only. A validator runs slower after it has rejected differently shaped input, because V8 optimizes its generated function again for the new object shapes. The cost tab times invalid input in a separate process.
- A rotated order. The run has four interleaved rounds with balanced rotation across setups. In each round a case is warmed up for 200 ms and then timed in two batches of 100 ms, so the table shows the median of eight batches.
- Every result is consumed. A checksum reads each result, so the engine cannot remove the timed call.
- The spread is recorded. Each case stores its minimum, median and maximum.
What each feature costs
Each ratio on the second tab divides one speed by another speed, for the same operation on the same payload. A ratio below 1.00x means that the first speed is lower.
- on/off divides the speed with validation on by the speed with validation off. It is shown for
IModeland forIObject. Off is a build with validation off. Validate has no off mode. - IModel/IObject divides the
IModelspeed by theIObjectspeed, with validation on in both. - serde many/none divides the speed on Serde many by the speed on Serde none, with validation on. It is shown on the Serde many rows only.
- invalid/valid divides the speed on one invalid value by the speed on a valid value, with validation on. It is shown for parse, decode and validate. Golem makes the path of a valid value fast, and the path of an invalid value builds the error and can be slower.
A dash means that the pair does not apply. "Not run" means that the run did not time the pair. The tab has no release column.
Run the benchmarks
Reproduce one number on your machine. Name one setup and one payload:
npm run bench -- --adapters golem-object --payloads simple- The payloads are
simple,nested,array,checkout,checkout-plain,serde-none,serde-many,union-tagged,union-class,temporal,recursive,unique-strings,unique-objects,unique-small,unique-nested,nest-queryandclient-call. - The run prints its tables to the console and writes
scripts/benchmark/results.jsonfor full runs (filtered runs write.cache/benchmark/results.json). - A name that is not valid stops the run before it starts, with exit code 2. The message lists the valid names.
- The run stores the machine load with the numbers, so stop other work on the machine first.
