Skip to content

Commit

Permalink
[optimisations] prevent some V8 deopt (exit from allowed arguments in…
Browse files Browse the repository at this point in the history
…dexes)
  • Loading branch information
zloirock committed Oct 18, 2015
1 parent 7165b9d commit 5bcabca
Show file tree
Hide file tree
Showing 51 changed files with 203 additions and 167 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"rules": {
"indent": [2, 2, {"VariableDeclarator": 2, "SwitchCase": 1}],
"max-len": [2, 100, 2],
"max-len": [2, 125, 2],
"eol-last": 0,
"camelcase": 2,
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
Expand Down
3 changes: 2 additions & 1 deletion library/modules/$.array-copy-within.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module.exports = [].copyWithin || function copyWithin(target/*= 0*/, start/*= 0,
, len = toLength(O.length)
, to = toIndex(target, len)
, from = toIndex(start, len)
, end = arguments[2]
, $$ = arguments
, end = $$.length > 2 ? $$[2] : undefined
, count = Math.min((end === undefined ? len : toIndex(end, len)) - from, len - to)
, inc = 1;
if(from < to && to < from + count){
Expand Down
6 changes: 4 additions & 2 deletions library/modules/$.array-fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ var toObject = require('./$.to-object')
module.exports = [].fill || function fill(value /*, start = 0, end = @length */){
var O = toObject(this, true)
, length = toLength(O.length)
, index = toIndex(arguments[1], length)
, end = arguments[2]
, $$ = arguments
, $$len = $$.length
, index = toIndex($$len > 1 ? $$[1] : undefined, length)
, end = $$len > 2 ? $$[2] : undefined
, endPos = end === undefined ? length : toIndex(end, length);
while(endPos > index)O[index++] = value;
return O;
Expand Down
11 changes: 6 additions & 5 deletions library/modules/$.assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ module.exports = require('./$.fails')(function(){
K.split('').forEach(function(k){ B[k] = k; });
return a({}, A)[S] != 7 || Object.keys(a({}, B)).join('') != K;
}) ? function assign(target, source){ // eslint-disable-line no-unused-vars
var T = toObject(target)
, l = arguments.length
, i = 1
var T = toObject(target)
, $$ = arguments
, $$len = $$.length
, index = 1
, getKeys = $.getKeys
, getSymbols = $.getSymbols
, isEnum = $.isEnum;
while(l > i){
var S = IObject(arguments[i++])
while($$len > index){
var S = IObject($$[index++])
, keys = getSymbols ? getKeys(S).concat(getSymbols(S)) : getKeys(S)
, length = keys.length
, j = 0
Expand Down
24 changes: 12 additions & 12 deletions library/modules/$.buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,22 +244,22 @@ if(require('./$.support-desc')){
return get(this, 1, byteOffset, unpackU8);
},
getInt16: function getInt16(byteOffset /*, littleEndian */){
return get(this, 2, byteOffset, unpackI16, arguments[1]);
return get(this, 2, byteOffset, unpackI16, arguments.length > 1 ? arguments[1] : undefined);
},
getUint16: function getUint16(byteOffset /*, littleEndian */){
return get(this, 2, byteOffset, unpackU16, arguments[1]);
return get(this, 2, byteOffset, unpackU16, arguments.length > 1 ? arguments[1] : undefined);
},
getInt32: function getInt32(byteOffset /*, littleEndian */){
return get(this, 4, byteOffset, unpackI32, arguments[1]);
return get(this, 4, byteOffset, unpackI32, arguments.length > 1 ? arguments[1] : undefined);
},
getUint32: function getUint32(byteOffset /*, littleEndian */){
return get(this, 4, byteOffset, unpackU32, arguments[1]);
return get(this, 4, byteOffset, unpackU32, arguments.length > 1 ? arguments[1] : undefined);
},
getFloat32: function getFloat32(byteOffset /*, littleEndian */){
return get(this, 4, byteOffset, unpackF32, arguments[1]);
return get(this, 4, byteOffset, unpackF32, arguments.length > 1 ? arguments[1] : undefined);
},
getFloat64: function getFloat64(byteOffset /*, littleEndian */){
return get(this, 8, byteOffset, unpackF64, arguments[1]);
return get(this, 8, byteOffset, unpackF64, arguments.length > 1 ? arguments[1] : undefined);
},
setInt8: function setInt8(byteOffset, value){
return set(this, 1, byteOffset, packI8, value);
Expand All @@ -268,22 +268,22 @@ if(require('./$.support-desc')){
return set(this, 1, byteOffset, packU8, value);
},
setInt16: function setInt16(byteOffset, value /*, littleEndian */){
return set(this, 2, byteOffset, packI16, value, arguments[2]);
return set(this, 2, byteOffset, packI16, value, arguments.length > 2 ? arguments[2] : undefined);
},
setUint16: function setUint16(byteOffset, value /*, littleEndian */){
return set(this, 2, byteOffset, packU16, value, arguments[2]);
return set(this, 2, byteOffset, packU16, value, arguments.length > 2 ? arguments[2] : undefined);
},
setInt32: function setInt32(byteOffset, value /*, littleEndian */){
return set(this, 4, byteOffset, packI32, value, arguments[2]);
return set(this, 4, byteOffset, packI32, value, arguments.length > 2 ? arguments[2] : undefined);
},
setUint32: function setUint32(byteOffset, value /*, littleEndian */){
return set(this, 4, byteOffset, packU32, value, arguments[2]);
return set(this, 4, byteOffset, packU32, value, arguments.length > 2 ? arguments[2] : undefined);
},
setFloat32: function setFloat32(byteOffset, value /*, littleEndian */){
return set(this, 4, byteOffset, packF32, value, arguments[2]);
return set(this, 4, byteOffset, packF32, value, arguments.length > 2 ? arguments[2] : undefined);
},
setFloat64: function setFloat64(byteOffset, value /*, littleEndian */){
return set(this, 8, byteOffset, packF64, value, arguments[2]);
return set(this, 8, byteOffset, packF64, value, arguments.length > 2 ? arguments[2] : undefined);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion library/modules/$.collection-strong.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module.exports = {
// 23.2.3.6 Set.prototype.forEach(callbackfn, thisArg = undefined)
// 23.1.3.5 Map.prototype.forEach(callbackfn, thisArg = undefined)
forEach: function forEach(callbackfn /*, that = undefined */){
var f = ctx(callbackfn, arguments[1], 3)
var f = ctx(callbackfn, arguments.length > 1 ? arguments[1] : undefined, 3)
, entry;
while(entry = entry ? entry.n : this._f){
f(entry.v, entry.k, this);
Expand Down
11 changes: 6 additions & 5 deletions library/modules/$.partial.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ module.exports = function(/* ...pargs */){
, holder = false;
while(length > i)if((pargs[i] = arguments[i++]) === _)holder = true;
return function(/* ...args */){
var that = this
, _length = arguments.length
var that = this
, $$ = arguments
, $$len = $$.length
, j = 0, k = 0, args;
if(!holder && !_length)return invoke(fn, pargs, that);
if(!holder && !$$len)return invoke(fn, pargs, that);
args = pargs.slice();
if(holder)for(;length > j; j++)if(args[j] === _)args[j] = arguments[k++];
while(_length > k)args.push(arguments[k++]);
if(holder)for(;length > j; j++)if(args[j] === _)args[j] = $$[k++];
while($$len > k)args.push($$[k++]);
return invoke(fn, args, that);
};
};
44 changes: 24 additions & 20 deletions library/modules/$.typed-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ var allocate = function(C, length){

var $from = function from(source /*, mapfn, thisArg */){
var O = toObject(source)
, mapfn = arguments[1]
, $$ = arguments
, $$len = $$.length
, mapfn = $$len > 1 ? $$[1] : undefined
, mapping = mapfn !== undefined
, iterFn = getIterFn(O)
, i, length, values, result, step, iterator;
Expand All @@ -79,7 +81,7 @@ var $from = function from(source /*, mapfn, thisArg */){
values.push(step.value);
} O = values;
}
if(mapping)mapfn = ctx(mapfn, arguments[2], 2);
if(mapping && $$len > 2)mapfn = ctx(mapfn, $$[2], 2);
for(i = 0, length = toLength(O.length), result = allocate(this, length); length > i; i++){
result[i] = mapping ? mapfn(O[i], i) : O[i];
}
Expand All @@ -106,53 +108,53 @@ var proto = {
// get length
// constructor
copyWithin: function copyWithin(target, start /*, end */){
return $copyWithin.call(validate(this), target, start, arguments[2]);
return $copyWithin.call(validate(this), target, start, arguments.length > 2 ? arguments[2] : undefined);
},
every: function every(callbackfn /*, thisArg */){
return $every(validate(this), callbackfn, arguments[1]);
return $every(validate(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
},
fill: function fill(value /*, start, end */){
return $fill.call(validate(this), value, arguments[1], arguments[2]);
fill: function fill(value /*, start, end */){ // eslint-disable-line no-unused-vars
return $fill.apply(validate(this), arguments);
},
filter: function filter(callbackfn /*, thisArg */){
return fromList(this.constructor, $filter(validate(this), callbackfn, arguments[1])); // TMP
return fromList(this.constructor, $filter(validate(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined));
},
find: function find(predicate /*, thisArg */){
return $find(validate(this), predicate, arguments[1]);
return $find(validate(this), predicate, arguments.length > 1 ? arguments[1] : undefined);
},
findIndex: function findIndex(predicate /*, thisArg */){
return $findIndex(validate(this), predicate, arguments[1]);
return $findIndex(validate(this), predicate, arguments.length > 1 ? arguments[1] : undefined);
},
forEach: function forEach(callbackfn /*, thisArg */){
$forEach(validate(this), callbackfn, arguments[1]);
$forEach(validate(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
},
indexOf: function indexOf(searchElement /*, fromIndex */){
return $indexOf(validate(this), searchElement, arguments[1]);
return $indexOf(validate(this), searchElement, arguments.length > 1 ? arguments[1] : undefined);
},
includes: function includes(searchElement /*, fromIndex */){
return $includes(validate(this), searchElement, arguments[1]);
return $includes(validate(this), searchElement, arguments.length > 1 ? arguments[1] : undefined);
},
join: function join(separator){ // eslint-disable-line no-unused-vars
return $join.apply(validate(this), arguments);
},
lastIndexOf: function lastIndexOf(searchElement /*, fromIndex */){ // eslint-disable-line
lastIndexOf: function lastIndexOf(searchElement /*, fromIndex */){ // eslint-disable-line no-unused-vars
return $lastIndexOf.apply(validate(this), arguments);
},
map: function map(mapfn /*, thisArg */){
return fromList(this.constructor, $map(validate(this), mapfn, arguments[1])); // TMP
return fromList(this.constructor, $map(validate(this), mapfn, arguments.length > 1 ? arguments[1] : undefined));
},
reduce: function reduce(callbackfn /*, initialValue */){ // eslint-disable-line
reduce: function reduce(callbackfn /*, initialValue */){ // eslint-disable-line no-unused-vars
return $reduce.apply(validate(this), arguments);
},
reduceRight: function reduceRight(callbackfn /*, initialValue */){ // eslint-disable-line
reduceRight: function reduceRight(callbackfn /*, initialValue */){ // eslint-disable-line no-unused-vars
return $reduceRight.apply(validate(this), arguments);
},
reverse: function reverse(){
return $reverse.call(validate(this));
},
set: function set(arrayLike /*, offset */){
validate(this);
var offset = toInteger(arguments[1]);
var offset = toInteger(arguments.length > 1 ? arguments[1] : undefined);
if(offset < 0)throw RangeError();
var length = this.length;
var src = toObject(arrayLike);
Expand All @@ -165,16 +167,18 @@ var proto = {
return fromList(this.constructor, $slice.call(validate(this), start, end)); // TODO
},
some: function some(callbackfn /*, thisArg */){
return $some(validate(this), callbackfn, arguments[1]);
return $some(validate(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
},
sort: function sort(comparefn){
return $sort.call(validate(this), comparefn);
},
subarray: function subarray(/* begin, end */){
var O = validate(this)
, length = O.length
, begin = toIndex(arguments[0], length)
, end = arguments[1];
, $$ = arguments
, $$len = $$.length
, begin = toIndex($$len > 0 ? $$[0] : undefined, length)
, end = $$len > 1 ? $$[1] : undefined;
return new O.constructor( // <- TODO SpeciesConstructor
O.buffer,
O.byteOffset + begin * O.BYTES_PER_ELEMENT,
Expand Down
2 changes: 1 addition & 1 deletion library/modules/es6.array.find-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var KEY = 'findIndex'
if(KEY in [])Array(1)[KEY](function(){ forced = false; });
$def($def.P + $def.F * forced, 'Array', {
findIndex: function findIndex(callbackfn/*, that = undefined */){
return $find(this, callbackfn, arguments[1]);
return $find(this, callbackfn, arguments.length > 1 ? arguments[1] : undefined);
}
});
require('./$.unscope')(KEY);
2 changes: 1 addition & 1 deletion library/modules/es6.array.find.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var KEY = 'find'
if(KEY in [])Array(1)[KEY](function(){ forced = false; });
$def($def.P + $def.F * forced, 'Array', {
find: function find(callbackfn/*, that = undefined */){
return $find(this, callbackfn, arguments[1]);
return $find(this, callbackfn, arguments.length > 1 ? arguments[1] : undefined);
}
});
require('./$.unscope')(KEY);
6 changes: 4 additions & 2 deletions library/modules/es6.array.from.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ $def($def.S + $def.F * !require('./$.iter-detect')(function(iter){ Array.from(it
from: function from(arrayLike/*, mapfn = undefined, thisArg = undefined*/){
var O = toObject(arrayLike)
, C = typeof this == 'function' ? this : Array
, mapfn = arguments[1]
, $$ = arguments
, $$len = $$.length
, mapfn = $$len > 1 ? $$[1] : undefined
, mapping = mapfn !== undefined
, index = 0
, iterFn = getIterFn(O)
, length, result, step, iterator;
if(mapping)mapfn = ctx(mapfn, arguments[2], 2);
if(mapping)mapfn = ctx(mapfn, $$len > 2 ? $$[2] : undefined, 2);
// if object isn't iterable or it's array with default iterator - use simple case
if(iterFn != undefined && !(C == Array && isArrayIter(iterFn))){
for(iterator = iterFn.call(O), result = new C; !(step = iterator.next()).done; index++){
Expand Down
9 changes: 5 additions & 4 deletions library/modules/es6.array.of.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ $def($def.S + $def.F * require('./$.fails')(function(){
// 22.1.2.3 Array.of( ...items)
of: function of(/* ...args */){
var index = 0
, length = arguments.length
, result = new (typeof this == 'function' ? this : Array)(length);
while(length > index)result[index] = arguments[index++];
result.length = length;
, $$ = arguments
, $$len = $$.length
, result = new (typeof this == 'function' ? this : Array)($$len);
while($$len > index)result[index] = $$[index++];
result.length = $$len;
return result;
}
});
2 changes: 1 addition & 1 deletion library/modules/es6.map.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var strong = require('./$.collection-strong');

// 23.1 Map Objects
require('./$.collection')('Map', function(get){
return function Map(){ return get(this, arguments[0]); };
return function Map(){ return get(this, arguments.length > 0 ? arguments[0] : undefined); };
}, {
// 23.1.3.6 Map.prototype.get(key)
get: function get(key){
Expand Down
13 changes: 7 additions & 6 deletions library/modules/es6.math.hypot.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ var $def = require('./$.def')

$def($def.S, 'Math', {
hypot: function hypot(value1, value2){ // eslint-disable-line no-unused-vars
var sum = 0
, i = 0
, len = arguments.length
, larg = 0
var sum = 0
, i = 0
, $$ = arguments
, $$len = $$.length
, larg = 0
, arg, div;
while(i < len){
arg = abs(arguments[i++]);
while(i < $$len){
arg = abs($$[i++]);
if(larg < arg){
div = larg / arg;
sum = sum * div * div + 1;
Expand Down
2 changes: 1 addition & 1 deletion library/modules/es6.set.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var strong = require('./$.collection-strong');

// 23.2 Set Objects
require('./$.collection')('Set', function(get){
return function Set(){ return get(this, arguments[0]); };
return function Set(){ return get(this, arguments.length > 0 ? arguments[0] : undefined); };
}, {
// 23.2.3.1 Set.prototype.add(value)
add: function add(value){
Expand Down
3 changes: 2 additions & 1 deletion library/modules/es6.string.ends-with.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ var $def = require('./$.def')
$def($def.P + $def.F * require('./$.fails-is-regexp')(ENDS_WITH), 'String', {
endsWith: function endsWith(searchString /*, endPosition = @length */){
var that = context(this, searchString, ENDS_WITH)
, endPosition = arguments[1]
, $$ = arguments
, endPosition = $$.length > 1 ? $$[1] : undefined
, len = toLength(that.length)
, end = endPosition === undefined ? len : Math.min(toLength(endPosition), len)
, search = String(searchString);
Expand Down
11 changes: 6 additions & 5 deletions library/modules/es6.string.from-code-point.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ var $def = require('./$.def')
$def($def.S + $def.F * (!!$fromCodePoint && $fromCodePoint.length != 1), 'String', {
// 21.1.2.2 String.fromCodePoint(...codePoints)
fromCodePoint: function fromCodePoint(x){ // eslint-disable-line no-unused-vars
var res = []
, len = arguments.length
, i = 0
var res = []
, $$ = arguments
, $$len = $$.length
, i = 0
, code;
while(len > i){
code = +arguments[i++];
while($$len > i){
code = +$$[i++];
if(toIndex(code, 0x10ffff) !== code)throw RangeError(code + ' is not a valid code point');
res.push(code < 0x10000
? fromCharCode(code)
Expand Down
2 changes: 1 addition & 1 deletion library/modules/es6.string.includes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ var $def = require('./$.def')

$def($def.P + $def.F * require('./$.fails-is-regexp')(INCLUDES), 'String', {
includes: function includes(searchString /*, position = 0 */){
return !!~context(this, searchString, INCLUDES).indexOf(searchString, arguments[1]);
return !!~context(this, searchString, INCLUDES).indexOf(searchString, arguments.length > 1 ? arguments[1] : undefined);
}
});
Loading

0 comments on commit 5bcabca

Please sign in to comment.