Skip to content

Commit 859f415

Browse files
committed
Avoid accessing arguments array without length check.
Accessing elements after the end of the arguments array triggers deoptimization of the entire function on v8, so avoid doing so. Also tweak all the checks into the same standard form for consistency.
1 parent 8dc13d7 commit 859f415

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

es6-shim.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,9 @@
10961096

10971097
var ArrayPrototypeShims = {
10981098
copyWithin: function copyWithin(target, start) {
1099-
var end = arguments[2]; // copyWithin.length must be 2
1099+
// copyWithin.length must be 2, so we can't add `end` to the arguments
1100+
// directly.
1101+
var end = arguments.length > 2 ? arguments[2] : void 0;
11001102
var o = ES.ToObject(this);
11011103
var len = ES.ToLength(o.length);
11021104
var relativeTarget = ES.ToInteger(target);
@@ -1243,7 +1245,7 @@
12431245
if (!arrayFromHandlesUndefinedMapFunction) {
12441246
var origArrayFrom = Array.from;
12451247
overrideNative(Array, 'from', function from(items) {
1246-
if (arguments.length > 0 && typeof arguments[1] !== 'undefined') {
1248+
if (arguments.length > 1 && typeof arguments[1] !== 'undefined') {
12471249
return ES.Call(origArrayFrom, this, arguments);
12481250
} else {
12491251
return _call(origArrayFrom, this, items);
@@ -2442,7 +2444,7 @@
24422444
if (!ES.IsPromise(promise)) { throw new TypeError('not a promise'); }
24432445
var C = ES.SpeciesConstructor(promise, Promise);
24442446
var resultCapability;
2445-
var returnValueIsIgnored = (arguments[2] === PROMISE_FAKE_CAPABILITY);
2447+
var returnValueIsIgnored = (arguments.length > 2 && arguments[2] === PROMISE_FAKE_CAPABILITY);
24462448
if (returnValueIsIgnored && C === Promise) {
24472449
resultCapability = PROMISE_FAKE_CAPABILITY;
24482450
} else {
@@ -3302,7 +3304,7 @@
33023304
if (!ES.IsConstructor(constructor)) {
33033305
throw new TypeError('First argument must be a constructor.');
33043306
}
3305-
var newTarget = arguments.length < 3 ? constructor : arguments[2];
3307+
var newTarget = arguments.length > 2 ? arguments[2] : constructor;
33063308
if (!ES.IsConstructor(newTarget)) {
33073309
throw new TypeError('new.target must be a constructor.');
33083310
}

0 commit comments

Comments
 (0)