5

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() ?

1
  • If no parrameter is provided the slice assumes it's 0. Commented Apr 4, 2017 at 13:16

3 Answers 3

10

There'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

Sign up to request clarification or add additional context in comments.

Comments

5

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 comparing it to the rest of the code!

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.