Skip to content

Commit 2f876b6

Browse files
Replace 'Array.reduce' with 'for of' (#2988)
Results in measurable perfomance increase and significantly lower memory usage in a few benchmarks.
1 parent 494a3d2 commit 2f876b6

File tree

7 files changed

+38
-54
lines changed

7 files changed

+38
-54
lines changed

src/error/GraphQLError.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ export class GraphQLError extends Error {
102102

103103
let _positions = positions;
104104
if (!_positions && _nodes) {
105-
_positions = _nodes.reduce((list, node) => {
105+
_positions = [];
106+
for (const node of _nodes) {
106107
if (node.loc) {
107-
list.push(node.loc.start);
108+
_positions.push(node.loc.start);
108109
}
109-
return list;
110-
}, []);
110+
}
111111
}
112112
if (_positions && _positions.length === 0) {
113113
_positions = undefined;
@@ -117,12 +117,12 @@ export class GraphQLError extends Error {
117117
if (positions && source) {
118118
_locations = positions.map((pos) => getLocation(source, pos));
119119
} else if (_nodes) {
120-
_locations = _nodes.reduce((list, node) => {
120+
_locations = [];
121+
for (const node of _nodes) {
121122
if (node.loc) {
122-
list.push(getLocation(node.loc.source, node.loc.start));
123+
_locations.push(getLocation(node.loc.source, node.loc.start));
123124
}
124-
return list;
125-
}, []);
125+
}
126126
}
127127

128128
let _extensions = extensions;

src/jsutils/keyMap.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ export function keyMap<T>(
2727
list: $ReadOnlyArray<T>,
2828
keyFn: (item: T) => string,
2929
): ObjMap<T> {
30-
return list.reduce((map, item) => {
31-
map[keyFn(item)] = item;
32-
return map;
33-
}, Object.create(null));
30+
const result = Object.create(null);
31+
for (const item of list) {
32+
result[keyFn(item)] = item;
33+
}
34+
return result;
3435
}

src/jsutils/keyValMap.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ export function keyValMap<T, V>(
2222
keyFn: (item: T) => string,
2323
valFn: (item: T) => V,
2424
): ObjMap<V> {
25-
return list.reduce((map, item) => {
26-
map[keyFn(item)] = valFn(item);
27-
return map;
28-
}, Object.create(null));
25+
const result = Object.create(null);
26+
for (const item of list) {
27+
result[keyFn(item)] = valFn(item);
28+
}
29+
return result;
2930
}

src/jsutils/promiseForObject.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import type { ObjMap } from './ObjMap';
1010
export function promiseForObject<T>(
1111
object: ObjMap<Promise<T>>,
1212
): Promise<ObjMap<T>> {
13-
const keys = Object.keys(object);
14-
const valuesAndPromises = keys.map((name) => object[name]);
15-
return Promise.all(valuesAndPromises).then((values) =>
16-
values.reduce((resolvedObject, value, i) => {
17-
resolvedObject[keys[i]] = value;
18-
return resolvedObject;
19-
}, Object.create(null)),
20-
);
13+
return Promise.all(Object.values(object)).then((resolvedValues) => {
14+
const resolvedObject = Object.create(null);
15+
for (const [i, key] of Object.keys(object).entries()) {
16+
resolvedObject[key] = resolvedValues[i];
17+
}
18+
return resolvedObject;
19+
});
2120
}

src/language/experimentalOnlineParser/onlineParser.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -389,17 +389,9 @@ export class OnlineParser {
389389
): boolean {
390390
if (rule.butNot) {
391391
if (Array.isArray(rule.butNot)) {
392-
if (
393-
rule.butNot.reduce(
394-
(matched, constraint) =>
395-
matched || this._matchToken(token, constraint),
396-
false,
397-
)
398-
) {
399-
return false;
400-
}
401-
402-
return true;
392+
return !rule.butNot.some((constraint) =>
393+
this._matchToken(token, constraint),
394+
);
403395
}
404396

405397
return !this._matchToken(token, rule.butNot);

src/validation/ValidationContext.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,15 @@ export class ASTValidationContext {
6767
}
6868

6969
getFragment(name: string): ?FragmentDefinitionNode {
70-
let fragments = this._fragments;
71-
if (!fragments) {
72-
this._fragments = fragments = this.getDocument().definitions.reduce(
73-
(frags, statement) => {
74-
if (statement.kind === Kind.FRAGMENT_DEFINITION) {
75-
frags[statement.name.value] = statement;
76-
}
77-
return frags;
78-
},
79-
Object.create(null),
80-
);
70+
if (!this._fragments) {
71+
const fragments = (this._fragments = Object.create(null));
72+
for (const defNode of this.getDocument().definitions) {
73+
if (defNode.kind === Kind.FRAGMENT_DEFINITION) {
74+
fragments[defNode.name.value] = defNode;
75+
}
76+
}
8177
}
82-
return fragments[name];
78+
return this._fragments[name];
8379
}
8480

8581
getFragmentSpreads(

src/validation/rules/OverlappingFieldsCanBeMergedRule.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -775,13 +775,8 @@ function subfieldConflicts(
775775
if (conflicts.length > 0) {
776776
return [
777777
[responseName, conflicts.map(([reason]) => reason)],
778-
conflicts.reduce((allFields, [, fields1]) => allFields.concat(fields1), [
779-
node1,
780-
]),
781-
conflicts.reduce(
782-
(allFields, [, , fields2]) => allFields.concat(fields2),
783-
[node2],
784-
),
778+
[node1, ...conflicts.map(([, fields1]) => fields1).flat()],
779+
[node2, ...conflicts.map(([, , fields2]) => fields2).flat()],
785780
];
786781
}
787782
}

0 commit comments

Comments
 (0)