Skip to content

Commit

Permalink
refactor: Improve parse performance (#25)
Browse files Browse the repository at this point in the history
* Combine value matchers into single regex match

* Refactor object/list parser into leading loop

* Turn arguments parsing into leading loop

* Tweak type parsing implementation

* Reformat variableDefinitions parsing

* Refactor selectionSet parsing to merge branches

* Pull out start of selections in parsing branches

* Remove redundant length checks

* Merge variable name into value parser

* Merge float part into main regex

* Absorb object/list parsing into value parser

* Merge field parsing into selectionSet parser

* Squash away typeCondition parser

* Merge fragmentSpread into selectionSet parser

* Remove redundant arrays in operation definitions

* Extract OperationType matching to document parser

* Avoid allocating variableDefinitions array as possible

* Avoid allocating directives if possible

* Avoid allocating arguments array if possible

* Remove name() helper

* Remove redundant ignore() calls

* Add changeset

* Remove redundant import

* Add mising tests and remove redundant branches

* Update comments

* Add additional tests

* Remove recursion from type() parser
  • Loading branch information
kitten committed Apr 2, 2024
1 parent 3f3b440 commit 6e4df56
Show file tree
Hide file tree
Showing 5 changed files with 507 additions and 350 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-tables-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@0no-co/graphql.web': patch
---

Improve parser performance.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dist/
coverage/
package-lock.json
.DS_Store
tsconfig.vitest-temp.json
100 changes: 50 additions & 50 deletions src/__tests__/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
{
"directives": [
{
"arguments": [],
"arguments": undefined,
"kind": "Directive",
"name": {
"kind": "Name",
Expand Down Expand Up @@ -50,7 +50,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
},
},
],
"directives": [],
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -61,8 +61,8 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"selections": [
{
"alias": undefined,
"arguments": [],
"directives": [],
"arguments": undefined,
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -73,7 +73,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
{
"directives": [
{
"arguments": [],
"arguments": undefined,
"kind": "Directive",
"name": {
"kind": "Name",
Expand All @@ -87,8 +87,8 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"selections": [
{
"alias": undefined,
"arguments": [],
"directives": [],
"arguments": undefined,
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -99,8 +99,8 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"selections": [
{
"alias": undefined,
"arguments": [],
"directives": [],
"arguments": undefined,
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand Down Expand Up @@ -175,8 +175,8 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"selections": [
{
"alias": undefined,
"arguments": [],
"directives": [],
"arguments": undefined,
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -187,7 +187,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
{
"directives": [
{
"arguments": [],
"arguments": undefined,
"kind": "Directive",
"name": {
"kind": "Name",
Expand Down Expand Up @@ -249,8 +249,8 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"selections": [
{
"alias": undefined,
"arguments": [],
"directives": [],
"arguments": undefined,
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -263,15 +263,15 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"typeCondition": undefined,
},
{
"directives": [],
"directives": undefined,
"kind": "InlineFragment",
"selectionSet": {
"kind": "SelectionSet",
"selections": [
{
"alias": undefined,
"arguments": [],
"directives": [],
"arguments": undefined,
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -291,7 +291,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"variableDefinitions": [
{
"defaultValue": undefined,
"directives": [],
"directives": undefined,
"kind": "VariableDefinition",
"type": {
"kind": "NamedType",
Expand All @@ -313,7 +313,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"kind": "EnumValue",
"value": "MOBILE",
},
"directives": [],
"directives": undefined,
"kind": "VariableDefinition",
"type": {
"kind": "NamedType",
Expand All @@ -335,7 +335,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
{
"directives": [
{
"arguments": [],
"arguments": undefined,
"kind": "Directive",
"name": {
"kind": "Name",
Expand Down Expand Up @@ -369,7 +369,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
],
"directives": [
{
"arguments": [],
"arguments": undefined,
"kind": "Directive",
"name": {
"kind": "Name",
Expand All @@ -387,8 +387,8 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"selections": [
{
"alias": undefined,
"arguments": [],
"directives": [],
"arguments": undefined,
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -399,10 +399,10 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"selections": [
{
"alias": undefined,
"arguments": [],
"arguments": undefined,
"directives": [
{
"arguments": [],
"arguments": undefined,
"kind": "Directive",
"name": {
"kind": "Name",
Expand All @@ -425,12 +425,12 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
},
],
},
"variableDefinitions": [],
"variableDefinitions": undefined,
},
{
"directives": [
{
"arguments": [],
"arguments": undefined,
"kind": "Directive",
"name": {
"kind": "Name",
Expand Down Expand Up @@ -465,7 +465,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
},
},
],
"directives": [],
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -476,8 +476,8 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"selections": [
{
"alias": undefined,
"arguments": [],
"directives": [],
"arguments": undefined,
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -488,8 +488,8 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"selections": [
{
"alias": undefined,
"arguments": [],
"directives": [],
"arguments": undefined,
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -500,8 +500,8 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"selections": [
{
"alias": undefined,
"arguments": [],
"directives": [],
"arguments": undefined,
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -514,8 +514,8 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
},
{
"alias": undefined,
"arguments": [],
"directives": [],
"arguments": undefined,
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -526,8 +526,8 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"selections": [
{
"alias": undefined,
"arguments": [],
"directives": [],
"arguments": undefined,
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -549,7 +549,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"variableDefinitions": [
{
"defaultValue": undefined,
"directives": [],
"directives": undefined,
"kind": "VariableDefinition",
"type": {
"kind": "NamedType",
Expand All @@ -571,7 +571,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
{
"directives": [
{
"arguments": [],
"arguments": undefined,
"kind": "Directive",
"name": {
"kind": "Name",
Expand Down Expand Up @@ -652,7 +652,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
},
},
],
"directives": [],
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -671,7 +671,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
},
},
{
"directives": [],
"directives": undefined,
"kind": "OperationDefinition",
"name": {
"kind": "Name",
Expand Down Expand Up @@ -717,7 +717,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
},
},
],
"directives": [],
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -727,8 +727,8 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
},
{
"alias": undefined,
"arguments": [],
"directives": [],
"arguments": undefined,
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -738,10 +738,10 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
},
],
},
"variableDefinitions": [],
"variableDefinitions": undefined,
},
{
"directives": [],
"directives": undefined,
"kind": "OperationDefinition",
"name": {
"kind": "Name",
Expand All @@ -753,8 +753,8 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
"selections": [
{
"alias": undefined,
"arguments": [],
"directives": [],
"arguments": undefined,
"directives": undefined,
"kind": "Field",
"name": {
"kind": "Name",
Expand All @@ -764,7 +764,7 @@ exports[`parse > parses the kitchen sink document like graphql.js does 1`] = `
},
],
},
"variableDefinitions": [],
"variableDefinitions": undefined,
},
],
"kind": "Document",
Expand Down

0 comments on commit 6e4df56

Please sign in to comment.