Alternative approach to spreadable arguments
See !35 (merged). This version initialises a new variable, argsArray
, in functions where it's needed for [...arguments]
. It's more bytes (unless you reference ...arguments
more than once), but faster, and the expressions themselves are much easier to read than the ( arguments.length === 1 ? [ arguments[0] ] : Array.apply( null, arguments ) )
.
Results:
// input
function x () {
[ ...foo ];
[ ...foo, ...bar ];
[ ...foo, ...bar, ...baz ];
[ ...foo, bar ];
[ foo, ...bar ];
[ ...arguments ];
[ ...foo, ...arguments ];
[ foo, ...arguments ];
[ ...arguments, ...foo ];
[ ...arguments, foo ];
Math.max( ...values );
Math.max( ...theseValues, ...thoseValues );
Math.max( ...arguments );
Math.max( ...arguments, ...values );
Math.max( ...values, ...arguments );
}
// output
function x () {
var i = arguments.length, argsArray = Array(i);
while ( i-- ) argsArray[i] = arguments[i];
[].concat( foo );
foo.concat( bar );
foo.concat( bar, baz );
foo.concat( [bar] );
[ foo ].concat( bar );
[].concat( argsArray );
foo.concat( argsArray );
[ foo ].concat( argsArray );
argsArray.concat( foo );
argsArray.concat( [foo] );
Math.max.apply( Math, values );
Math.max.apply( Math, theseValues.concat( thoseValues ) );
Math.max.apply( Math, arguments );
Math.max.apply( Math, argsArray.concat( values ) );
Math.max.apply( Math, values.concat( argsArray ) );
}
Any thoughts on which is preferable?
Note: in the case where var args = [...arguments]
is the only time we spread arguments inside a function, we don't need to do var args = [].concat(argsArray)
– we can just do var args = argsArray
. That isn't yet implemented.