Skip to content

chore(performance): optimize regressed for..of and array destructuring performance #4080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .babelrc-deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"plugins": [
"@babel/plugin-syntax-typescript",
["@babel/plugin-transform-for-of", { "assumeArray": true }],
"@babel/plugin-transform-destructuring",
["./resources/add-extension-to-import-paths", { "extension": "ts" }],
"./resources/inline-invariant"
]
Expand Down
2 changes: 2 additions & 0 deletions .babelrc-npm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"plugins": [
"@babel/plugin-transform-typescript",
["@babel/plugin-transform-for-of", { "assumeArray": true }],
"@babel/plugin-transform-destructuring",
"./resources/inline-invariant"
],
"env": {
Expand Down
6 changes: 5 additions & 1 deletion .babelrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"plugins": ["@babel/plugin-transform-typescript"],
"plugins": [
"@babel/plugin-transform-typescript",
["@babel/plugin-transform-for-of", { "assumeArray": true }],
"@babel/plugin-transform-destructuring"
],
"presets": [
[
"@babel/preset-env",
Expand Down
109 changes: 65 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"devDependencies": {
"@babel/core": "7.17.9",
"@babel/plugin-syntax-typescript": "7.16.7",
"@babel/plugin-transform-destructuring": "^7.24.5",
"@babel/plugin-transform-for-of": "^7.24.1",
"@babel/plugin-transform-typescript": "7.16.8",
"@babel/preset-env": "7.16.11",
"@babel/register": "7.17.7",
Expand Down
5 changes: 3 additions & 2 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ function executeFieldsSerially(
fields: Map<string, ReadonlyArray<FieldNode>>,
): PromiseOrValue<ObjMap<unknown>> {
return promiseReduce(
fields.entries(),
Array.from(fields.entries()),
(results, [responseName, fieldNodes]) => {
const fieldPath = addPath(path, responseName, parentType.name);
const result = executeField(
Expand Down Expand Up @@ -446,7 +446,8 @@ function executeFields(
let containsPromise = false;

try {
for (const [responseName, fieldNodes] of fields.entries()) {
const fieldsEntries = Array.from(fields.entries());
for (const [responseName, fieldNodes] of fieldsEntries) {
const fieldPath = addPath(path, responseName, parentType.name);
const result = executeField(
exeContext,
Expand Down
3 changes: 2 additions & 1 deletion src/jsutils/promiseForObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export function promiseForObject<T>(
): Promise<ObjMap<T>> {
return Promise.all(Object.values(object)).then((resolvedValues) => {
const resolvedObject = Object.create(null);
for (const [i, key] of Object.keys(object).entries()) {
const entries = Array.from(Object.keys(object).entries());
for (const [i, key] of entries) {
resolvedObject[key] = resolvedValues[i];
}
return resolvedObject;
Expand Down
2 changes: 1 addition & 1 deletion src/jsutils/promiseReduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { PromiseOrValue } from './PromiseOrValue';
* return a Promise.
*/
export function promiseReduce<T, U>(
values: Iterable<T>,
values: Array<T>,
callbackFn: (accumulator: U, currentValue: T) => PromiseOrValue<U>,
initialValue: PromiseOrValue<U>,
): PromiseOrValue<U> {
Expand Down
3 changes: 2 additions & 1 deletion src/language/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export function getLocation(source: Source, position: number): SourceLocation {
let lastLineStart = 0;
let line = 1;

for (const match of source.body.matchAll(LineRegExp)) {
const matches = Array.from(source.body.matchAll(LineRegExp))
for (const match of matches) {
invariant(typeof match.index === 'number');
if (match.index >= position) {
break;
Expand Down
2 changes: 1 addition & 1 deletion src/type/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class GraphQLSchema {
// Keep track of all implementations by interface name.
this._implementationsMap = Object.create(null);

for (const namedType of allReferencedTypes) {
for (const namedType of Array.from(allReferencedTypes)) {
if (namedType == null) {
continue;
}
Expand Down