Mirror: The spec-compliant minimum of client-side GraphQL.

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

Changed files
+37 -43
.changeset
src
+5
.changeset/nice-papayas-cover.md
···
+
---
+
'@0no-co/graphql.web': patch
+
---
+
+
Remove redundant loc setter/getter in favour of value to improve pre-warmup times
+32 -43
src/parser.ts
···
};
}
-
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':
···
) {
name = nameNode();
}
-
definitions.push({
+
_definitions.push({
kind: 'OperationDefinition' as Kind.OPERATION_DEFINITION,
operation: definition as OperationTypeNode,
name,
···
}
}
} 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 = {
···
): 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(