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

authored by kitten.sh and committed by GitHub 9e950169 51bc0db1

Changed files
+37 -43
.changeset
src
+5
.changeset/nice-papayas-cover.md
··· 1 + --- 2 + '@0no-co/graphql.web': patch 3 + --- 4 + 5 + Remove redundant loc setter/getter in favour of value to improve pre-warmup times
+32 -43
src/parser.ts
··· 467 467 }; 468 468 } 469 469 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[] = []; 473 472 do { 474 473 if (input.charCodeAt(idx) === 123 /*'{'*/) { 475 - definitions.push({ 474 + idx++; 475 + ignored(); 476 + _definitions.push({ 476 477 kind: 'OperationDefinition' as Kind.OPERATION_DEFINITION, 477 478 operation: 'query' as OperationTypeNode.QUERY, 478 479 name: undefined, 479 480 variableDefinitions: undefined, 480 481 directives: undefined, 481 - selectionSet: selectionSetStart(), 482 + selectionSet: selectionSet(), 482 483 }); 483 484 } else { 484 485 const definition = name(); 485 486 switch (definition) { 486 487 case 'fragment': 487 - definitions.push(fragmentDefinition()); 488 + _definitions.push(fragmentDefinition()); 488 489 break; 489 490 case 'query': 490 491 case 'mutation': ··· 498 499 ) { 499 500 name = nameNode(); 500 501 } 501 - definitions.push({ 502 + _definitions.push({ 502 503 kind: 'OperationDefinition' as Kind.OPERATION_DEFINITION, 503 504 operation: definition as OperationTypeNode, 504 505 name, ··· 512 513 } 513 514 } 514 515 } 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; 550 517 } 551 518 552 519 type ParseOptions = { ··· 559 526 ): ast.DocumentNode { 560 527 input = string.body ? string.body : string; 561 528 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 + } 563 552 } 564 553 565 554 export function parseValue(