icharts
icharts

Reputation: 81

Javascript Array.slice() vs Array.slice(0)

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

Upvotes: 5

Views: 3812

Answers (3)

Alex Romanov
Alex Romanov

Reputation: 11963

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

Upvotes: 10

ibrahim mahrir
ibrahim mahrir

Reputation: 31692

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!

Upvotes: 5

kind user
kind user

Reputation: 41893

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.

Upvotes: 3

Related Questions