For javascript which one is faster?
a.unshift("new first value");
vs
for(i=a.length; i>0; i--) a[i] = a[i-1];
a[0] = "new first value";
vs
a[5] = a[4];
a[4] = a[3];
a[3] = a[2];
a[2] = a[1];
a[1] = a[0];
a[0] = "new first value";
Well, of course, assigning values by hand is unpractical. But there would be cases that your array has very few items that you can assign it with your hand and it's in a loop that would repeat it millions of times. So I just included it.
Then the main question is:
- Is there any performance difference between first two cases?
- And can third one be faster than both of them
Seems the manual assignment is good with a small number of items only, then
unshifttakes the place.In Firefox
spliceis the fastest: