Skip to content

Commit 9e95016

Browse files
authored
perf: Drop redundant loc setter/getter for simple value (#54)
* Remove loc setter/getter and hoist into `document` * Remove redundant selectionSetStart check in definitions * Add changeset
1 parent 51bc0db commit 9e95016

File tree

2 files changed

+37
-43
lines changed

2 files changed

+37
-43
lines changed

.changeset/nice-papayas-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@0no-co/graphql.web': patch
3+
---
4+
5+
Remove redundant loc setter/getter in favour of value to improve pre-warmup times

src/parser.ts

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -467,24 +467,25 @@ function fragmentDefinition(): ast.FragmentDefinitionNode {
467467
};
468468
}
469469

470-
function document(input: string, noLoc: boolean): ast.DocumentNode {
471-
ignored();
472-
const definitions: ast.ExecutableDefinitionNode[] = [];
470+
function definitions(): ast.DefinitionNode[] {
471+
const _definitions: ast.ExecutableDefinitionNode[] = [];
473472
do {
474473
if (input.charCodeAt(idx) === 123 /*'{'*/) {
475-
definitions.push({
474+
idx++;
475+
ignored();
476+
_definitions.push({
476477
kind: 'OperationDefinition' as Kind.OPERATION_DEFINITION,
477478
operation: 'query' as OperationTypeNode.QUERY,
478479
name: undefined,
479480
variableDefinitions: undefined,
480481
directives: undefined,
481-
selectionSet: selectionSetStart(),
482+
selectionSet: selectionSet(),
482483
});
483484
} else {
484485
const definition = name();
485486
switch (definition) {
486487
case 'fragment':
487-
definitions.push(fragmentDefinition());
488+
_definitions.push(fragmentDefinition());
488489
break;
489490
case 'query':
490491
case 'mutation':
@@ -498,7 +499,7 @@ function document(input: string, noLoc: boolean): ast.DocumentNode {
498499
) {
499500
name = nameNode();
500501
}
501-
definitions.push({
502+
_definitions.push({
502503
kind: 'OperationDefinition' as Kind.OPERATION_DEFINITION,
503504
operation: definition as OperationTypeNode,
504505
name,
@@ -512,41 +513,7 @@ function document(input: string, noLoc: boolean): ast.DocumentNode {
512513
}
513514
}
514515
} while (idx < input.length);
515-
516-
if (!noLoc) {
517-
let loc: Location | undefined;
518-
return {
519-
kind: 'Document' as Kind.DOCUMENT,
520-
definitions,
521-
/* v8 ignore start */
522-
set loc(_loc: Location) {
523-
loc = _loc;
524-
},
525-
/* v8 ignore stop */
526-
// @ts-ignore
527-
get loc() {
528-
if (!loc) {
529-
loc = {
530-
start: 0,
531-
end: input.length,
532-
startToken: undefined,
533-
endToken: undefined,
534-
source: {
535-
body: input,
536-
name: 'graphql.web',
537-
locationOffset: { line: 1, column: 1 },
538-
},
539-
};
540-
}
541-
return loc;
542-
},
543-
};
544-
}
545-
546-
return {
547-
kind: 'Document' as Kind.DOCUMENT,
548-
definitions,
549-
};
516+
return _definitions;
550517
}
551518

552519
type ParseOptions = {
@@ -559,7 +526,29 @@ export function parse(
559526
): ast.DocumentNode {
560527
input = string.body ? string.body : string;
561528
idx = 0;
562-
return document(input, options && options.noLocation);
529+
ignored();
530+
if (options && options.noLocation) {
531+
return {
532+
kind: 'Document' as Kind.DOCUMENT,
533+
definitions: definitions(),
534+
};
535+
} else {
536+
return {
537+
kind: 'Document' as Kind.DOCUMENT,
538+
definitions: definitions(),
539+
loc: {
540+
start: 0,
541+
end: input.length,
542+
startToken: undefined,
543+
endToken: undefined,
544+
source: {
545+
body: input,
546+
name: 'graphql.web',
547+
locationOffset: { line: 1, column: 1 },
548+
},
549+
},
550+
} as Location;
551+
}
563552
}
564553

565554
export function parseValue(

0 commit comments

Comments
 (0)