Skip to content

Commit e53c6ce

Browse files
JoviDeCroockbenjie
andcommitted
Increase print/visit performance
Co-Authored-By: Benjie <code@benjiegillam.com>
1 parent 48afd37 commit e53c6ce

File tree

4 files changed

+103
-6
lines changed

4 files changed

+103
-6
lines changed

benchmark/fixtures.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ exports.bigSchemaSDL = fs.readFileSync(
88
'utf8',
99
);
1010

11+
exports.bigDocumentSDL = fs.readFileSync(
12+
path.join(__dirname, 'kitchen-sink.graphql'),
13+
'utf8',
14+
);
15+
1116
exports.bigSchemaIntrospectionResult = JSON.parse(
1217
fs.readFileSync(path.join(__dirname, 'github-schema.json'), 'utf8'),
1318
);

benchmark/kitchen-sink.graphql

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
query queryName($foo: ComplexType, $site: Site = MOBILE) @onQuery {
2+
whoever123is: node(id: [123, 456]) {
3+
id
4+
... on User @onInlineFragment {
5+
field2 {
6+
id
7+
alias: field1(first: 10, after: $foo) @include(if: $foo) {
8+
id
9+
...frag @onFragmentSpread
10+
}
11+
}
12+
}
13+
... @skip(unless: $foo) {
14+
id
15+
}
16+
... {
17+
id
18+
}
19+
}
20+
}
21+
22+
mutation likeStory @onMutation {
23+
like(story: 123) @onField {
24+
story {
25+
id @onField
26+
}
27+
}
28+
}
29+
30+
subscription StoryLikeSubscription(
31+
$input: StoryLikeSubscribeInput @onVariableDefinition
32+
) @onSubscription {
33+
storyLikeSubscribe(input: $input) {
34+
story {
35+
likers {
36+
count
37+
}
38+
likeSentence {
39+
text
40+
}
41+
}
42+
}
43+
}
44+
45+
fragment frag on Friend @onFragmentDefinition {
46+
foo(
47+
size: $size
48+
bar: $b
49+
obj: {
50+
key: "value"
51+
block: """
52+
block string uses \"""
53+
"""
54+
}
55+
)
56+
}
57+
58+
{
59+
unnamed(truthy: true, falsy: false, nullish: null)
60+
query
61+
}
62+
63+
query {
64+
__typename
65+
}

benchmark/printer-benchmark.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const { parse } = require('graphql/language/parser.js');
4+
const { print } = require('graphql/language/printer.js');
5+
6+
const { bigDocumentSDL } = require('./fixtures.js');
7+
8+
const document = parse(bigDocumentSDL);
9+
10+
module.exports = {
11+
name: 'Print kitchen sink document',
12+
count: 1000,
13+
measure() {
14+
print(document);
15+
},
16+
};

src/language/visitor.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,23 @@ export function visit(
222222
}
223223
}
224224
} else {
225-
node = Object.defineProperties(
226-
{},
227-
Object.getOwnPropertyDescriptors(node),
228-
);
229-
for (const [editKey, editValue] of edits) {
230-
node[editKey] = editValue;
225+
const descriptors = Object.getOwnPropertyDescriptors(node);
226+
node = { ...node };
227+
for (const nodeKey of Object.keys(descriptors)) {
228+
if (!(nodeKey in node)) {
229+
const descriptor = descriptors[nodeKey];
230+
if (
231+
descriptor.enumerable &&
232+
descriptor.configurable &&
233+
descriptor.writable &&
234+
!descriptor.get &&
235+
!descriptor.set
236+
) {
237+
// We already own this by means of the spread
238+
} else {
239+
Object.defineProperty(node, nodeKey, descriptor);
240+
}
241+
}
231242
}
232243
}
233244
}

0 commit comments

Comments
 (0)