From 7f5fe4da1a2f213db0a253e2a9b5398071931950 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Tue, 9 Aug 2022 20:53:28 +0300 Subject: [PATCH] Remove superficial `.values()` and `.entries()` calls (#3689) Motivation: Spotted during investigation of unrelated issue. Maps and sets are iteratable, so no need to call these methods. --- src/execution/execute.ts | 8 +++++--- src/jsutils/__tests__/AccumulatorMap-test.ts | 2 +- src/jsutils/promiseForObject.ts | 9 ++++++--- src/type/validate.ts | 2 +- src/validation/rules/SingleFieldSubscriptionsRule.ts | 3 +-- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/execution/execute.ts b/src/execution/execute.ts index cc3cbb9cbf..e7b7e4e177 100644 --- a/src/execution/execute.ts +++ b/src/execution/execute.ts @@ -391,7 +391,7 @@ function executeFieldsSerially( fields: Map>, ): PromiseOrValue> { return promiseReduce( - fields.entries(), + fields, (results, [responseName, fieldNodes]) => { const fieldPath = addPath(path, responseName, parentType.name); const result = executeField( @@ -431,7 +431,7 @@ function executeFields( const results = Object.create(null); let containsPromise = false; - for (const [responseName, fieldNodes] of fields.entries()) { + for (const [responseName, fieldNodes] of fields) { const fieldPath = addPath(path, responseName, parentType.name); const result = executeField( exeContext, @@ -1227,7 +1227,9 @@ function executeSubscription( rootType, operation.selectionSet, ); - const [responseName, fieldNodes] = [...rootFields.entries()][0]; + + const firstRootField = rootFields.entries().next().value; + const [responseName, fieldNodes] = firstRootField; const fieldName = fieldNodes[0].name.value; const fieldDef = schema.getField(rootType, fieldName); diff --git a/src/jsutils/__tests__/AccumulatorMap-test.ts b/src/jsutils/__tests__/AccumulatorMap-test.ts index 0fb34c6499..b4360a7951 100644 --- a/src/jsutils/__tests__/AccumulatorMap-test.ts +++ b/src/jsutils/__tests__/AccumulatorMap-test.ts @@ -4,7 +4,7 @@ import { describe, it } from 'mocha'; import { AccumulatorMap } from '../AccumulatorMap'; function expectMap(map: Map) { - return expect(Object.fromEntries(map.entries())); + return expect(Object.fromEntries(map)); } describe('AccumulatorMap', () => { diff --git a/src/jsutils/promiseForObject.ts b/src/jsutils/promiseForObject.ts index 1074676030..ede94dc803 100644 --- a/src/jsutils/promiseForObject.ts +++ b/src/jsutils/promiseForObject.ts @@ -10,10 +10,13 @@ import type { ObjMap } from './ObjMap'; export function promiseForObject( object: ObjMap>, ): Promise> { - return Promise.all(Object.values(object)).then((resolvedValues) => { + const keys = Object.keys(object); + const values = Object.values(object); + + return Promise.all(values).then((resolvedValues) => { const resolvedObject = Object.create(null); - for (const [i, key] of Object.keys(object).entries()) { - resolvedObject[key] = resolvedValues[i]; + for (let i = 0; i < keys.length; ++i) { + resolvedObject[keys[i]] = resolvedValues[i]; } return resolvedObject; }); diff --git a/src/type/validate.ts b/src/type/validate.ts index 469b00aaab..8c33114432 100644 --- a/src/type/validate.ts +++ b/src/type/validate.ts @@ -144,7 +144,7 @@ function validateRootTypes(context: SchemaValidationContext): void { } } - for (const [rootType, operationTypes] of rootTypesMap.entries()) { + for (const [rootType, operationTypes] of rootTypesMap) { if (operationTypes.length > 1) { const operationList = andList(operationTypes); context.reportError( diff --git a/src/validation/rules/SingleFieldSubscriptionsRule.ts b/src/validation/rules/SingleFieldSubscriptionsRule.ts index 21cb1abaf6..01fdc9f966 100644 --- a/src/validation/rules/SingleFieldSubscriptionsRule.ts +++ b/src/validation/rules/SingleFieldSubscriptionsRule.ts @@ -62,8 +62,7 @@ export function SingleFieldSubscriptionsRule( ); } for (const fieldNodes of fields.values()) { - const field = fieldNodes[0]; - const fieldName = field.name.value; + const fieldName = fieldNodes[0].name.value; if (fieldName.startsWith('__')) { context.reportError( new GraphQLError(