Skip to content

Commit 6f8f3d6

Browse files
genuclaude
andcommitted
perf(orm): hoist static Set and per-row Map in ext result helpers
Move EXT_RESULT_OPERATIONS to a module-level constant to avoid reallocating on every Proxy get trap, and compute collectExtResultFieldDefs once per query instead of once per row. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 755b565 commit 6f8f3d6

1 file changed

Lines changed: 17 additions & 17 deletions

File tree

packages/orm/src/client/client-impl.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -553,19 +553,6 @@ function createModelCrudHandler(
553553
const schema = client.$schema;
554554
const hasAnyExtResult = hasExtResultFieldDefs(plugins);
555555

556-
// operations that return model rows and should have ext result fields applied
557-
const extResultOperations = new Set<CoreCrudOperations>([
558-
'findMany',
559-
'findUnique',
560-
'findFirst',
561-
'create',
562-
'createManyAndReturn',
563-
'update',
564-
'updateManyAndReturn',
565-
'upsert',
566-
'delete',
567-
]);
568-
569556
const createPromise = (
570557
operation: CoreCrudOperations,
571558
nominalOperation: AllCrudOperations,
@@ -578,7 +565,7 @@ function createModelCrudHandler(
578565
let proceed = async (_args: unknown) => {
579566
// prepare args for ext result: strip ext result field names from select/omit,
580567
// inject needs fields into select (recursively handles nested relations)
581-
const shouldApplyExtResult = hasAnyExtResult && extResultOperations.has(operation);
568+
const shouldApplyExtResult = hasAnyExtResult && EXT_RESULT_OPERATIONS.has(operation);
582569
const processedArgs = shouldApplyExtResult
583570
? prepareArgsForExtResult(_args, model, schema, plugins)
584571
: _args;
@@ -858,6 +845,18 @@ function createModelCrudHandler(
858845

859846
// #region Extended result field helpers
860847

848+
// operations that return model rows and should have ext result fields applied
849+
const EXT_RESULT_OPERATIONS = new Set<CoreCrudOperations>([
850+
'findMany',
851+
'findUnique',
852+
'findFirst',
853+
'create',
854+
'createManyAndReturn',
855+
'update',
856+
'updateManyAndReturn',
857+
'upsert',
858+
'delete',
859+
]);
861860

862861
/**
863862
* Returns true if any plugin defines ext result fields for any model.
@@ -1011,13 +1010,14 @@ function applyExtResult(
10111010
schema: SchemaDef,
10121011
plugins: AnyPlugin[],
10131012
): unknown {
1013+
const extResultDefs = collectExtResultFieldDefs(model, plugins);
10141014
if (Array.isArray(result)) {
10151015
for (let i = 0; i < result.length; i++) {
1016-
result[i] = applyExtResultToRow(result[i], model, originalArgs, schema, plugins);
1016+
result[i] = applyExtResultToRow(result[i], model, originalArgs, schema, plugins, extResultDefs);
10171017
}
10181018
return result;
10191019
} else {
1020-
return applyExtResultToRow(result, model, originalArgs, schema, plugins);
1020+
return applyExtResultToRow(result, model, originalArgs, schema, plugins, extResultDefs);
10211021
}
10221022
}
10231023

@@ -1027,13 +1027,13 @@ function applyExtResultToRow(
10271027
originalArgs: unknown,
10281028
schema: SchemaDef,
10291029
plugins: AnyPlugin[],
1030+
extResultDefs: Map<string, ExtResultFieldDef>,
10301031
): unknown {
10311032
if (!row || typeof row !== 'object') {
10321033
return row;
10331034
}
10341035

10351036
const data = row as Record<string, unknown>;
1036-
const extResultDefs = collectExtResultFieldDefs(model, plugins);
10371037
const typedArgs = (originalArgs && typeof originalArgs === 'object' ? originalArgs : {}) as Record<string, unknown>;
10381038
const select = typedArgs['select'] as Record<string, unknown> | undefined;
10391039
const omit = typedArgs['omit'] as Record<string, unknown> | undefined;

0 commit comments

Comments
 (0)