Skip to content

Commit

Permalink
perf: Drop redundant loc setter/getter for simple value (#54)
Browse files Browse the repository at this point in the history
* Remove loc setter/getter and hoist into `document`

* Remove redundant selectionSetStart check in definitions

* Add changeset
  • Loading branch information
kitten authored Feb 13, 2025
1 parent 51bc0db commit 9e95016
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-papayas-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@0no-co/graphql.web': patch
---

Remove redundant loc setter/getter in favour of value to improve pre-warmup times
75 changes: 32 additions & 43 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,24 +467,25 @@ function fragmentDefinition(): ast.FragmentDefinitionNode {
};
}

function document(input: string, noLoc: boolean): ast.DocumentNode {
ignored();
const definitions: ast.ExecutableDefinitionNode[] = [];
function definitions(): ast.DefinitionNode[] {
const _definitions: ast.ExecutableDefinitionNode[] = [];
do {
if (input.charCodeAt(idx) === 123 /*'{'*/) {
definitions.push({
idx++;
ignored();
_definitions.push({
kind: 'OperationDefinition' as Kind.OPERATION_DEFINITION,
operation: 'query' as OperationTypeNode.QUERY,
name: undefined,
variableDefinitions: undefined,
directives: undefined,
selectionSet: selectionSetStart(),
selectionSet: selectionSet(),
});
} else {
const definition = name();
switch (definition) {
case 'fragment':
definitions.push(fragmentDefinition());
_definitions.push(fragmentDefinition());
break;
case 'query':
case 'mutation':
Expand All @@ -498,7 +499,7 @@ function document(input: string, noLoc: boolean): ast.DocumentNode {
) {
name = nameNode();
}
definitions.push({
_definitions.push({
kind: 'OperationDefinition' as Kind.OPERATION_DEFINITION,
operation: definition as OperationTypeNode,
name,
Expand All @@ -512,41 +513,7 @@ function document(input: string, noLoc: boolean): ast.DocumentNode {
}
}
} while (idx < input.length);

if (!noLoc) {
let loc: Location | undefined;
return {
kind: 'Document' as Kind.DOCUMENT,
definitions,
/* v8 ignore start */
set loc(_loc: Location) {
loc = _loc;
},
/* v8 ignore stop */
// @ts-ignore
get loc() {
if (!loc) {
loc = {
start: 0,
end: input.length,
startToken: undefined,
endToken: undefined,
source: {
body: input,
name: 'graphql.web',
locationOffset: { line: 1, column: 1 },
},
};
}
return loc;
},
};
}

return {
kind: 'Document' as Kind.DOCUMENT,
definitions,
};
return _definitions;
}

type ParseOptions = {
Expand All @@ -559,7 +526,29 @@ export function parse(
): ast.DocumentNode {
input = string.body ? string.body : string;
idx = 0;
return document(input, options && options.noLocation);
ignored();
if (options && options.noLocation) {
return {
kind: 'Document' as Kind.DOCUMENT,
definitions: definitions(),
};
} else {
return {
kind: 'Document' as Kind.DOCUMENT,
definitions: definitions(),
loc: {
start: 0,
end: input.length,
startToken: undefined,
endToken: undefined,
source: {
body: input,
name: 'graphql.web',
locationOffset: { line: 1, column: 1 },
},
},
} as Location;
}
}

export function parseValue(
Expand Down

0 comments on commit 9e95016

Please sign in to comment.