Skip to content

Commit d059437

Browse files
authored
fix(perf): hoist error creation (#4664)
for small performance win
1 parent 6e87945 commit d059437

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/execution/Executor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ export interface FormattedExecutionResult<
222222
extensions?: TExtensions;
223223
}
224224

225+
const defaultAbortReason = new Error('This operation was aborted');
226+
225227
/** @internal */
226228
export class Executor<
227229
TPositionContext = undefined, // No position context by default
@@ -246,7 +248,7 @@ export class Executor<
246248
) {
247249
this.validatedExecutionArgs = validatedExecutionArgs;
248250
this.aborted = false;
249-
this.abortReason = new Error('This operation was aborted');
251+
this.abortReason = defaultAbortReason;
250252
this.collectedErrors = new CollectedErrors();
251253

252254
if (sharedExecutionContext === undefined) {

src/validation/validate.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ const QueryDocumentKeysToValidate = mapValue(
3131
(keys: ReadonlyArray<string>) => keys.filter((key) => key !== 'description'),
3232
);
3333

34+
const tooManyValidationErrorsError = new GraphQLError(
35+
'Too many validation errors, error limit reached. Validation aborted.',
36+
);
37+
3438
/**
3539
* Implements the "Validation" section of the spec.
3640
*
@@ -63,9 +67,6 @@ export function validate(
6367
// If the schema used for validation is invalid, throw an error.
6468
assertValidSchema(schema);
6569

66-
const abortError = new GraphQLError(
67-
'Too many validation errors, error limit reached. Validation aborted.',
68-
);
6970
const errors: Array<GraphQLError> = [];
7071
const typeInfo = new TypeInfo(schema);
7172
const context = new ValidationContext(
@@ -74,7 +75,7 @@ export function validate(
7475
typeInfo,
7576
(error) => {
7677
if (errors.length >= maxErrors) {
77-
throw abortError;
78+
throw tooManyValidationErrorsError;
7879
}
7980
errors.push(error);
8081
},
@@ -93,8 +94,8 @@ export function validate(
9394
QueryDocumentKeysToValidate,
9495
);
9596
} catch (e: unknown) {
96-
if (e === abortError) {
97-
errors.push(abortError);
97+
if (e === tooManyValidationErrorsError) {
98+
errors.push(tooManyValidationErrorsError);
9899
} else {
99100
throw e;
100101
}

0 commit comments

Comments
 (0)