Skip to content

Commit

Permalink
use native Array.prototype.forEach without fallbacks in simple cases
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 8, 2023
1 parent b9b426d commit 038e964
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 15 deletions.
3 changes: 1 addition & 2 deletions packages/core-js-pure/override/internals/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var isObject = require('../internals/is-object');
var isNullOrUndefined = require('../internals/is-null-or-undefined');
var setToStringTag = require('../internals/set-to-string-tag');
var defineProperty = require('../internals/object-define-property').f;
var forEach = require('../internals/array-iteration').forEach;
var InternalStateModule = require('../internals/internal-state');

var setInternalState = InternalStateModule.set;
Expand Down Expand Up @@ -45,7 +44,7 @@ module.exports = function (CONSTRUCTOR_NAME, wrapper, common) {

var getInternalState = internalStateGetterFor(CONSTRUCTOR_NAME);

forEach(['add', 'clear', 'delete', 'forEach', 'get', 'has', 'set', 'keys', 'values', 'entries'], function (KEY) {
['add', 'clear', 'delete', 'forEach', 'get', 'has', 'set', 'keys', 'values', 'entries'].forEach(function (KEY) {
var IS_ADDER = KEY === 'add' || KEY === 'set';
if (KEY in NativePrototype && !(IS_WEAK && KEY === 'clear')) {
createNonEnumerableProperty(Prototype, KEY, function (a, b) {
Expand Down
1 change: 0 additions & 1 deletion packages/core-js/internals/array-for-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ var STRICT_METHOD = arrayMethodIsStrict('forEach');
// https://tc39.es/ecma262/#sec-array.prototype.foreach
module.exports = !STRICT_METHOD ? function forEach(callbackfn /* , thisArg */) {
return $forEach(this, callbackfn, arguments.length > 1 ? arguments[1] : undefined);
// eslint-disable-next-line es/no-array-prototype-foreach -- safe
} : [].forEach;
3 changes: 1 addition & 2 deletions packages/core-js/internals/typed-array-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var isSymbol = require('../internals/is-symbol');
var isPrototypeOf = require('../internals/object-is-prototype-of');
var setPrototypeOf = require('../internals/object-set-prototype-of');
var typedArrayFrom = require('../internals/typed-array-from');
var forEach = require('../internals/array-iteration').forEach;
var setSpecies = require('../internals/set-species');
var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
var definePropertyModule = require('../internals/object-define-property');
Expand Down Expand Up @@ -206,7 +205,7 @@ module.exports = function (TYPE, wrapper, CLAMPED) {
});

if (setPrototypeOf) setPrototypeOf(TypedArrayConstructor, TypedArray);
forEach(getOwnPropertyNames(NativeTypedArrayConstructor), function (key) {
getOwnPropertyNames(NativeTypedArrayConstructor).forEach(function (key) {
if (!(key in TypedArrayConstructor)) {
createNonEnumerableProperty(TypedArrayConstructor, key, NativeTypedArrayConstructor[key]);
}
Expand Down
1 change: 0 additions & 1 deletion packages/core-js/modules/es.array.for-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var forEach = require('../internals/array-for-each');

// `Array.prototype.forEach` method
// https://tc39.es/ecma262/#sec-array.prototype.foreach
// eslint-disable-next-line es/no-array-prototype-foreach -- safe
$({ target: 'Array', proto: true, forced: [].forEach !== forEach }, {
forEach: forEach,
});
12 changes: 4 additions & 8 deletions packages/core-js/modules/es.symbol.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ var defineWellKnownSymbol = require('../internals/well-known-symbol-define');
var defineSymbolToPrimitive = require('../internals/symbol-define-to-primitive');
var setToStringTag = require('../internals/set-to-string-tag');
var InternalStateModule = require('../internals/internal-state');
var $forEach = require('../internals/array-iteration').forEach;

var HIDDEN = sharedKey('hidden');
var SYMBOL = 'Symbol';
Expand Down Expand Up @@ -107,8 +106,7 @@ var $defineProperty = function defineProperty(O, P, Attributes) {
var $defineProperties = function defineProperties(O, Properties) {
anObject(O);
var properties = toIndexedObject(Properties);
var keys = nativeObjectKeys(properties).concat($getOwnPropertySymbols(properties));
$forEach(keys, function (key) {
nativeObjectKeys(properties).concat($getOwnPropertySymbols(properties)).forEach(function (key) {
if (call($propertyIsEnumerable, properties, key)) $defineProperty(O, key, properties[key]);
});
return O;
Expand Down Expand Up @@ -138,19 +136,17 @@ var $getOwnPropertyDescriptor = function getOwnPropertyDescriptor(O, P) {
};

var $getOwnPropertyNames = function getOwnPropertyNames(O) {
var names = nativeGetOwnPropertyNames(toIndexedObject(O));
var result = [];
$forEach(names, function (key) {
nativeGetOwnPropertyNames(toIndexedObject(O)).forEach(function (key) {
if (!hasOwn(AllSymbols, key) && !hasOwn(hiddenKeys, key)) push(result, key);
});
return result;
};

var $getOwnPropertySymbols = function (O) {
var IS_OBJECT_PROTOTYPE = O === ObjectPrototype;
var names = nativeGetOwnPropertyNames(IS_OBJECT_PROTOTYPE ? ObjectPrototypeSymbols : toIndexedObject(O));
var result = [];
$forEach(names, function (key) {
nativeGetOwnPropertyNames(IS_OBJECT_PROTOTYPE ? ObjectPrototypeSymbols : toIndexedObject(O)).forEach(function (key) {
if (hasOwn(AllSymbols, key) && (!IS_OBJECT_PROTOTYPE || hasOwn(ObjectPrototype, key))) {
push(result, AllSymbols[key]);
}
Expand Down Expand Up @@ -217,7 +213,7 @@ $({ global: true, constructor: true, wrap: true, forced: !NATIVE_SYMBOL, sham: !
Symbol: $Symbol,
});

$forEach(nativeObjectKeys(WellKnownSymbolsStore), function (name) {
nativeObjectKeys(WellKnownSymbolsStore).forEach(function (name) {
defineWellKnownSymbol(name);
});

Expand Down
1 change: 0 additions & 1 deletion tests/eslint/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,6 @@ const forbidESAnnexBBuiltIns = {
const forbidES5BuiltIns = {
'es/no-array-prototype-every': ERROR,
'es/no-array-prototype-filter': ERROR,
'es/no-array-prototype-foreach': ERROR,
'es/no-array-prototype-indexof': ERROR,
'es/no-array-prototype-lastindexof': ERROR,
'es/no-array-prototype-map': ERROR,
Expand Down

0 comments on commit 038e964

Please sign in to comment.