Skip to content

Commit 43b94b7

Browse files
committed
Remove remaining for of loops
1 parent 2f912be commit 43b94b7

File tree

6 files changed

+37
-21
lines changed

6 files changed

+37
-21
lines changed

assert.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,16 @@ function expectedException(actual, expected, msg, fn) {
381381
expected, 'may not be an empty object');
382382
}
383383
if (isDeepEqual === undefined) lazyLoadComparison();
384-
for (const key of keys) {
385-
if (typeof actual[key] === 'string' &&
386-
isRegExp(expected[key]) &&
387-
expected[key].test(actual[key])) {
388-
continue;
384+
keys.forEach(key => {
385+
if (
386+
typeof actual[key] === 'string' &&
387+
isRegExp(expected[key]) &&
388+
expected[key].test(actual[key])
389+
) {
390+
return;
389391
}
390392
compareExceptionKey(actual, expected, key, msg, keys, fn);
391-
}
393+
});
392394
return true;
393395
}
394396
// Guard instanceof against arrow functions as they don't have a prototype.

internal/assert/assertion_error.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ const kMaxShortLength = 10;
8181
function copyError(source) {
8282
const keys = Object.keys(source);
8383
const target = Object.create(Object.getPrototypeOf(source));
84-
for (const key of keys) {
84+
keys.forEach(key => {
8585
target[key] = source[key];
86-
}
86+
});
8787
Object.defineProperty(target, 'message', { value: source.message });
8888
return target;
8989
}

internal/util/comparisons.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
const regexFlagsSupported = /a/g.flags !== undefined;
77

8+
const arrayFrom = require('array-from');
9+
810
const objectIs = Object.is ? Object.is : require('object-is');
911
const objectGetOwnPropertySymbols = Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols : () => [];
1012

@@ -344,7 +346,9 @@ function keyCheck(val1, val2, strict, memos, iterationType, aKeys) {
344346

345347
function setHasEqualElement(set, val1, strict, memo) {
346348
// Go looking.
347-
for (const val2 of set) {
349+
const setValues = arrayFrom(set.values());
350+
for (let i = 0; i < setValues.length; i++) {
351+
const val2 = setValues[i];
348352
if (innerDeepEqual(val1, val2, strict, memo)) {
349353
// Remove the matching element to make sure we do not check that again.
350354
set.delete(val2);
@@ -405,7 +409,9 @@ function setEquiv(a, b, strict, memo) {
405409
// This is a lazily initiated Set of entries which have to be compared
406410
// pairwise.
407411
let set = null;
408-
for (const val of a) {
412+
const aValues = arrayFrom(a.values());
413+
for (let i = 0; i < aValues.length; i++) {
414+
const val = aValues[i];
409415
// Note: Checking for the objects first improves the performance for object
410416
// heavy sets but it is a minor slow down for primitives. As they are fast
411417
// to check this improves the worst case scenario instead.
@@ -435,7 +441,9 @@ function setEquiv(a, b, strict, memo) {
435441
}
436442

437443
if (set !== null) {
438-
for (const val of b) {
444+
const bValues = arrayFrom(b.values());
445+
for (let i = 0; i < bValues.length; i++) {
446+
const val = bValues[i];
439447
// We have to check if a primitive value is already
440448
// matching and only if it's not, go hunting for it.
441449
if (typeof val === 'object' && val !== null) {
@@ -457,7 +465,9 @@ function mapHasEqualEntry(set, map, key1, item1, strict, memo) {
457465
// To be able to handle cases like:
458466
// Map([[{}, 'a'], [{}, 'b']]) vs Map([[{}, 'b'], [{}, 'a']])
459467
// ... we need to consider *all* matching keys, not just the first we find.
460-
for (const key2 of set) {
468+
const setValues = arrayFrom(set.values());
469+
for (let i = 0; i < setValues.length; i++) {
470+
const key2 = setValues[i];
461471
if (innerDeepEqual(key1, key2, strict, memo) &&
462472
innerDeepEqual(item1, map.get(key2), strict, memo)) {
463473
set.delete(key2);
@@ -471,7 +481,9 @@ function mapHasEqualEntry(set, map, key1, item1, strict, memo) {
471481
function mapEquiv(a, b, strict, memo) {
472482
let set = null;
473483

474-
for (const [key, item1] of a) {
484+
const aEntries = arrayFrom(a.entries());
485+
for (let i = 0; i < aEntries.length; i++) {
486+
const [key, item1] = aEntries[i];
475487
if (typeof key === 'object' && key !== null) {
476488
if (set === null) {
477489
set = new Set();
@@ -498,7 +510,9 @@ function mapEquiv(a, b, strict, memo) {
498510
}
499511

500512
if (set !== null) {
501-
for (const [key, item] of b) {
513+
const bEntries = arrayFrom(b.entries());
514+
for (let i = 0; i < bEntries.length; i++) {
515+
const [key, item] = bEntries[i];
502516
if (typeof key === 'object' && key !== null) {
503517
if (!mapHasEqualEntry(set, a, key, item, strict, memo))
504518
return false;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
"@babel/core": "^7.4.4",
2525
"@babel/preset-env": "^7.4.4",
2626
"airtap": "^2.0.2",
27-
"array-from": "^2.1.1",
2827
"core-js": "^3.0.1",
2928
"cross-env": "^5.2.0",
3029
"object.getownpropertydescriptors": "^2.0.3",
3130
"tape": "^4.10.1"
3231
},
3332
"dependencies": {
33+
"array-from": "^2.1.1",
3434
"buffer": "^5.2.1",
3535
"es6-object-assign": "^1.1.0",
3636
"object-is": "^1.0.1",

test/common/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ function expectsError(fn, settings, exact) {
247247

248248
// Check all error properties.
249249
const keys = Object.keys(settings);
250-
for (const key of keys) {
250+
keys.forEach(key => {
251251
if (!isDeepStrictEqual(error[key], innerSettings[key])) {
252252
// Create placeholder objects to create a nice output.
253253
const a = new Comparison(error, keys);
@@ -271,7 +271,7 @@ function expectsError(fn, settings, exact) {
271271
});
272272
}
273273

274-
}
274+
});
275275
return true;
276276
}
277277
if (fn) {

test/parallel/test-assert-checktag.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ if (process.stdout && process.stdout.isTTY)
4545
if (!isBrowser) { // At the moment global has its own type tag
4646
const fakeGlobal = {};
4747
Object.setPrototypeOf(fakeGlobal, Object.getPrototypeOf(global));
48-
for (const prop of Object.keys(global)) {
48+
Object.keys(global).forEach(prop => {
4949
fakeGlobal[prop] = global[prop];
50-
}
50+
});
5151
assert.notDeepEqual(fakeGlobal, global);
5252
// Message will be truncated anyway, don't validate
5353
assert.throws(() => assert.deepStrictEqual(fakeGlobal, global),
@@ -58,9 +58,9 @@ if (!isBrowser) { // At the moment global has its own type tag
5858
if (!isBrowser) { // At the moment process has its own type tag
5959
const fakeProcess = {};
6060
Object.setPrototypeOf(fakeProcess, Object.getPrototypeOf(process));
61-
for (const prop of Object.keys(process)) {
61+
Object.keys(process).forEach(prop => {
6262
fakeProcess[prop] = process[prop];
63-
}
63+
});
6464
assert.notDeepEqual(fakeProcess, process);
6565
// Message will be truncated anyway, don't validate
6666
assert.throws(() => assert.deepStrictEqual(fakeProcess, process),

0 commit comments

Comments
 (0)