Skip to content

perf: compile field name transformation row mappers into object literals - #828

Merged
gajus merged 1 commit into
mainfrom
gajus/field-name-transformation-performance
Nov 5, 2026
Merged

gajus merged 1 commit into
mainfrom
gajus/field-name-transformation-performance

Conversation

@gajus

@gajus gajus commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Improves the performance of slonik-interceptor-field-name-transformation, and fixes two cases in which it reused the transformation of a different set of fields.

Performance

transformRow used to copy every row one property at a time into a null-prototype object (Object.create(null)). V8 keeps such objects in dictionary mode, which makes them slow to create, and slow to read for everything that consumes the rows afterwards.

Rows are now copied by a function that is compiled once per set of fields and returns an object literal, e.g. (row) => ({ "fooBar": row["foo_bar"], "id": row["id"] }). Rows whose field names do not need to be transformed are returned as they are.

200,000 rows of 12 fields, through transformRow, Node 26:

before after
transformRow 93.7 ms 3.7 ms
reading 2 fields from each transformed row 11.0 ms 0.9 ms
  • Field names are embedded as JSON string literals, so they cannot inject code.
  • A field that keeps the name __proto__ is defined using a computed key, because a literal "__proto__": value property sets the prototype.
  • Where code generation from strings is disallowed, rows are copied one property at a time instead (still into plain objects).

Rows are now plain objects (they used to have a null prototype).

Fixes

The transformation is cached per set of fields. The cache key was the field names joined with ,, so:

  • fields whose names contain commas could reuse the transformation of a different set of fields, e.g. ["foo_bar,baz_qux"] and ["foo_bar", "baz_qux"];
  • fields with the same names but different types shared a transformation, although test receives the whole field (including dataTypeId).

The key now consists of the field names and types, joined with a NUL character (which cannot appear in field names).

Tests

8 new tests: plain objects, passthrough, __proto__, special characters in field names, both cache key cases, and that test runs once per set of fields. They pass both with the compiled row mapper and with the fallback (--disallow-code-generation-from-strings).

Note: on macOS, this package's tests do not run locally (ava reports "No tests found"): the path of the tsimp daemon socket in this package is 106 characters long, which is longer than macOS allows for a Unix socket path. CI (Linux) is not affected.

@changeset-bot

changeset-bot Bot commented Nov 5, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 13acc2a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 14 packages
Name Type
slonik-interceptor-field-name-transformation Minor
@slonik/benchmark Minor
@slonik/driver Minor
@slonik/errors Minor
@slonik/pg-driver Minor
@slonik/dataloaders Minor
slonik-interceptor-query-cache Minor
slonik-interceptor-query-logging Minor
slonik-sql-tag-raw Minor
slonik Minor
@slonik/sql-tag Minor
@slonik/test-ssls Minor
@slonik/types Minor
@slonik/utilities Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gajus
gajus merged commit 370c0d3 into main Nov 5, 2026
4 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 5, 2026

This branch was previously deployed

1 inactive deployment
release — 13acc2a9 Deployed Nov 5, 2026 by gajus via Test node_version:24 test_only:utilities #446
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant