applyFluent rebuilds the model field map with an O(n²) reduce + spread on every findUnique/findFirst/create/update call

Open
#30,825 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
node.js, postgresql, typescript

Research direction

Start with packages/client/src/runtime/core/model/applyFluent.ts, especially the field map construction at the line linked in the issue. Trace how applyFluent receives the model and whether its metadata is reused, then check nearby tests for the single-record operations listed in the report. Done means the field map is built in linear time or reused per model, with those operations still passing their tests.

Written by the indexing model from the issue text.

Description

Bug description

applyFluent rebuilds a name → field map of the model on every single-record model call (findUnique, findUniqueOrThrow, findFirst, findFirstOrThrow, create, update, upsert, delete). It uses a reduce with an object spread, which is O(n²) in the number of fields:

https://github.com/prisma/prisma/blob/6.19.0/packages/client/src/runtime/core/model/applyFluent.ts#L88-L91

const dmmfModelFieldMap = dmmfModel.fields.reduce(
  (acc, field) => ({ ...acc, [field.name]: field }),
  {} as { [dmmfModelFieldName: string]: DMMF.Field },
)

Each step copies the whole accumulator, so a model with n fields (relation fields included) costs about n²/2 property copies per call, before any query is sent. The map only depends on the model, so it could be built once.

Impact we measured

Our app has two wide models: one with 571 fields (most of them relation fields) and one with 273. A CPU profile of a production Next.js server (node --cpu-prof, Prisma 6.19.3, library engine, Node 24) put about 45 % of all server CPU in this single arrow function. That was roughly 670 ms per rendered page, because a page makes dozens of findUnique/findFirst calls on those models.

Patching only this line, with no other change, made the same pages 2–2.5× faster single-user. Under load (k6, 200 virtual users) throughput went up about 2.9×.

Micro-benchmark (Node 24.19.0, per call)
fields reduce + spread Object.fromEntries
30 0.013 ms 0.003 ms
100 0.112 ms 0.008 ms
273 1.40 ms 0.028 ms
571 12.6 ms 0.075 ms
const mk = (n) => Array.from({ length: n }, (_, i) => ({ name: `f${i}`, kind: "scalar", type: "String" }));
for (const n of [30, 100, 273, 571]) {
  const fields = mk(n); const N = 200;
  let t = performance.now();
  for (let i = 0; i < N; i++) fields.reduce((acc, f) => ({ ...acc, [f.name]: f }), {});
  const spread = (performance.now() - t) / N;
  t = performance.now();
  for (let i = 0; i < N; i++) Object.fromEntries(fields.map((f) => [f.name, f]));
  console.log(n, spread.toFixed(3), ((performance.now() - t) / N).toFixed(4));
}
How to reproduce
  1. Define a model with a few hundred fields (relation fields count, since fields includes them).
  2. Call prisma.<model>.findUnique({ where: { id } }) in a loop and CPU-profile it.
  3. The anonymous (acc, field) => ({ ...acc, [field.name]: field }) inside applyFluent dominates self time, and the cost grows quadratically with the field count.
Expected behavior

The field map is computed once per model (or linearly), so per-call overhead does not depend on the square of the model's field count.

Suggested fix

Build the map in O(n) and memoize it per model, e.g.:

const fieldMapCache = new WeakMap<object, Record<string, DMMF.Field>>()

const dmmfModelFieldMap =
  fieldMapCache.get(dmmfModel) ??
  (fieldMapCache.set(dmmfModel, Object.fromEntries(dmmfModel.fields.map((f) => [f.name, f]))), fieldMapCache.get(dmmfModel)!)

Even Object.fromEntries alone, without the cache, removes the quadratic term. We ship exactly this as a build-time patch to runtime/library.js, and it has passed our full integration suite.

Environment & setup
  • OS: Linux (Docker, node:24 image)
  • Database: PostgreSQL 16
  • Node.js version: 24.19.0
Prisma Version
prisma                  : 6.19.3
@prisma/client          : 6.19.3
Engine                  : library (default)

We have not checked Prisma 7. The same reduce is in applyFluent.ts at tag 6.19.0.

Dominant language
TypeScript
Stars
47.8k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
227

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from prisma/orm

All issues in prisma/orm

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.