Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using slice() with or without [end] argument

Is there any performance difference using the slice() method with or without the last argument [end]?

Example:

var m = ['But', 'Will', 'It', 'Blend', 'Question'];
var r = m.slice(1,3);

OR

var r = m.slice(2);

PS: not the result as it is, but the performance issue only.

like image 497
lockedz Avatar asked Aug 14 '15 11:08

lockedz


1 Answers

If you take a look at the implementation https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice you will see that if the second argument is not sent then it uses the array length so no, I think they are the same.

Array.prototype.slice = function(begin, end) {
  // IE < 9 gets unhappy with an undefined end argument
  end = (typeof end !== 'undefined') ? end : this.length;
  .....................................
});
like image 78
biancamihai Avatar answered Oct 12 '22 19:10

biancamihai