applyFluent rebuilds the model field map with an O(n²) reduce + spread on every findUnique/findFirst/create/update call
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
- Domain
- backend-api-design, performance
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:
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
- Define a model with a few hundred fields (relation fields count, since
fieldsincludes them). - Call
prisma.<model>.findUnique({ where: { id } })in a loop and CPU-profile it. - The anonymous
(acc, field) => ({ ...acc, [field.name]: field })insideapplyFluentdominates 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
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from prisma/orm
-
bug/1-unconfirmed kind/bug
Difficulty 1/5 Under an hour Newbie friendliness 90/100
Maintainers usually reply within 1 day
-
get-platform: distros with ID_LIKE=alpine are not detected as musl, so glibc engines are used on Alpine-based imagesPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
prisma/orm#31281 · 1 comment ·
Maintainers usually reply within 1 day
-
adapter-pg: connectToShadowDb connects to the main database when built from a connection stringMay be free again @cestercian claimed this 10 days ago, and no pull request is open. Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
prisma/orm#31196 · 1 comment ·
Maintainers usually reply within 1 day
-
[Prisma 8] Type-check time grows exponentially with chained .include() callsPossibly taken @SiKreuz claimed this 13 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
prisma/orm#31161 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
prisma/orm#31125 · 4 comments · 6 reactions ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
tenstorrent/ttnn-visualizer#2526 ·
Maintainers usually reply within 1 day
-
trySessionCache calls /get-session twice when minting session_data (cache miss or expired cookie)Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
neondatabase/neon-js#301 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
tashfeenahmed/freellmapi#2305 ·
Maintainers usually reply within 1 day