Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to move first element to the end of an Array

I'm wondering what is the fastest way in JavaScript to move an element from the beginning of an Array to the end. For example if we have

[8,1,2,3,4,5,6,7]

And we want: [1,2,3,4,5,6,7,8]

I want to move the first element to the end. I was thinking about switching element 0 with element 1, after that switching element 1 with element 2 and so on until the 8 is at the and (basically how bubblesort works). I was wondering if there is a faster way to bring the first element to the end.

I will be using small Arrays (around 10 elements), and I want to avoid shift() since it's pretty slow.

This is what I have now on chrome it's 45% faster than normal shift+push: http://jsperf.com/shift-myfunc

The arrays will have objects in them for a game.

like image 416
Bosiwow Avatar asked Dec 04 '13 20:12

Bosiwow


People also ask

How do you move the first item of an array to the end?

When you need to move the first element in an array to the last position, follow this tip. You can use the returned function array. shift() as input to the function array. push().

How do you move one element in an array?

Create a temp variable and assign the value of the original position to it. Now, assign the value in the new position to original position. Finally, assign the value in the temp to the new position.

How do you skip the first element of an array?

You can use array. slice(0,1) // First index is removed and array is returned. FIrst index is not removed, a copy is created without the first element. The original array is not modified.


2 Answers

Use the shift() and push() functions:

var ary = [8,1,2,3,4,5,6,7]; ary.push(ary.shift());  // results in [1, 2, 3, 4, 5, 6, 7, 8]  

Example:

var ary = [8,1,2,3,4,5,6,7];  console.log("Before: " + ary);  ary.push(ary.shift());  // results in [1, 2, 3, 4, 5, 6, 7, 8]   console.log("After: " + ary);
like image 76
j08691 Avatar answered Sep 21 '22 12:09

j08691


Use shift and push

var a = ["a","b","c"]; var b = a.shift(); a.push(b); 

or

var b = a.shift(); a[a.length] = b; 

Edit Based on updated question

What is going to be the fastest? Really depends on content of the array and what browser/version!

Now what are the ways to remove the first index?

  • shift()
  • splice()
  • slice()

Now what are the ways to add to the last index?

  • push()
  • array[array.length]
  • concat() -- not even going to try

Other ways

  • for loop - make new array [going to be horrible on large arrays]

JSPerf:

http://jsperf.com/test-swapping-of-first-to-last


What is really the fastest?

What is the fastest really depends on what you are doing with the array. If you are just using the first index, it will be fastest just to make your code read an index and not shift the values. If you are using all the indexes, than just loop and through and when you get to the end start back at zero. Basic counters.

like image 31
epascarello Avatar answered Sep 22 '22 12:09

epascarello