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

I saw a while ago that Array.slice (0) is faster than Array.slice (). Unfortunately, I cannot find this source right now. Is it possible? Is there a difference between Array.slice (0) and Array.slice ()?

+3


source to share


3 answers


There is no difference as it is begin

assigned 0

by default if you do not provide any parameter to the method Array.slice()

.

to begin Optional

Zero-based index to start extraction. you can use a negative index by specifying an offset from the end of the sequence.

If the start is undefined, the slice starts at index 0.



For more information: link

+5


source


slice

looks something like this:

function slice(start) {
    if( /* start is not valid */ ) {
        start = 0;
    }
    // ...
}

      



The only difference is whether the line start = 0

is being evaluated or not! So the only change to the evaluation time would be that the assignment, which is not very expensive, compares it to the rest of the code!

+3


source


If you don't pass any argument to the function Array.slice()

, the default state will be set to 0

.

If begin is undefined, the slice starts at index 0.

Array.prototype.slice () MDN.

+2


source







All Articles