Some time ago I saw somewhere the thing that Array.slice(0) is faster than Array.slice(). Unfortunately now I can't find that source. So is that possible ? There is any difference between Array.slice(0) and Array.slice() ?
Some time ago I saw somewhere the thing that Array.slice(0) is faster than Array.slice(). Unfortunately now I can't find that source. So is that possible ? There is any difference between Array.slice(0) and Array.slice() ?
Share Improve this question edited Apr 4, 2017 at 13:16 Tarek 3,7983 gold badges41 silver badges62 bronze badges asked Apr 4, 2017 at 13:14 ichartsicharts 811 silver badge6 bronze badges 1-
If no parrameter is provided the
slice
assumes it's0
. – ibrahim mahrir Commented Apr 4, 2017 at 13:16
3 Answers
Reset to default 10There's no difference, because begin
is assigned to 0
by default if you don't provide any parameter to Array.slice()
method.
begin
Optional
Zero-based index at which to begin extraction. A negative index can be used, indicating an offset from the end of the sequence.
If begin is undefined, slice begins from index 0.
For more info: link
slice
is something like this:
function slice(start) {
if( /* start is not valid */ ) {
start = 0;
}
// ...
}
The only diffirence is wether the line start = 0
is evaluated or not! So the only change in evaluation time will be that of the assignment which is not very costly paring it to the rest of the code!
If you won't pass any argument to the Array.slice()
function, the default state will be set to 0
.
If begin is undefined, slice begins from index 0.
Array.prototype.slice() MDN.