最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Array.slice() vs Array.slice(0) - Stack Overflow

programmeradmin4浏览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() ?

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's 0. – ibrahim mahrir Commented Apr 4, 2017 at 13:16
Add a ment  | 

3 Answers 3

Reset to default 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

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.

发布评论

评论列表(0)

  1. 暂无评论