Skip to main content
deleted 15 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I had a situation when I needed to move all array elements in circular fashion. When I say this, I mean:

0 1 2 3
1 2 3 0
2 3 0 1
3 0 1 2

So theThe array:

var players = ["hash1","hash2","hash3","hash4"];

Players is the array that contains user hashes and their place on the table. Table is round, so array must rotate in circular fashion.

Step dictates by how much it should move.

  • Players is the array that contains user hashes and their place on the table. Table is round, so array must rotate in circular fashion.
  • Step dictates by how much it should move.

I came up with following algorithm (if you can call that, probably not) that works fine. I was just wondering if there was a more efficient way. Or cleaner way to do same?

offsetThe offset variable is probably wrongly named,named; I couldn't come up with a better name.

var step = 0 // 3,2,1,0
    var offset = 0;
    var players_new = [];
    for (var i = 0; i <= players.length - 1; i++) {
        if (i + step <= players.length - 1) {
            players_new[i + step] = players[i];
            offset++;
        } else {
            players_new[i - offset] = players[i];
        }
    };

I tested many different versions, and looks like we have a winner :) vazha's version is the fastest all around, except in firefoxFirefox.

http://jsperf.com/rotate-array-javascript

Since, I'm using this code in node.js, which uses chromethe Chrome engine, for me chrome results are important.

I had a situation when I needed to move all array elements in circular fashion. When I say this, I mean:

0 1 2 3
1 2 3 0
2 3 0 1
3 0 1 2

So the array:

var players = ["hash1","hash2","hash3","hash4"];

Players is the array that contains user hashes and their place on the table. Table is round, so array must rotate in circular fashion.

Step dictates by how much it should move.

I came up with following algorithm (if you can call that, probably not) that works fine. I was just wondering if there was a more efficient way. Or cleaner way to do same?

offset variable is probably wrongly named, I couldn't come up with a better name.

var step = 0 // 3,2,1,0
    var offset = 0;
    var players_new = [];
    for (var i = 0; i <= players.length - 1; i++) {
        if (i + step <= players.length - 1) {
            players_new[i + step] = players[i];
            offset++;
        } else {
            players_new[i - offset] = players[i];
        }
    };

I tested many different versions, and looks like we have a winner :) vazha's version is the fastest all around, except in firefox.

http://jsperf.com/rotate-array-javascript

Since, I'm using this code in node.js which uses chrome engine, for me chrome results are important.

I had a situation when I needed to move all array elements in circular fashion. When I say this, I mean:

0 1 2 3
1 2 3 0
2 3 0 1
3 0 1 2

The array:

var players = ["hash1","hash2","hash3","hash4"];
  • Players is the array that contains user hashes and their place on the table. Table is round, so array must rotate in circular fashion.
  • Step dictates by how much it should move.

I came up with following algorithm (if you can call that, probably not) that works fine. I was just wondering if there was a more efficient way. Or cleaner way to do same?

The offset variable is probably wrongly named; I couldn't come up with a better name.

var step = 0 // 3,2,1,0
    var offset = 0;
    var players_new = [];
    for (var i = 0; i <= players.length - 1; i++) {
        if (i + step <= players.length - 1) {
            players_new[i + step] = players[i];
            offset++;
        } else {
            players_new[i - offset] = players[i];
        }
    };

I tested many different versions, and looks like vazha's version is the fastest all around, except in Firefox.

jsperf

Since I'm using this code in node.js, which uses the Chrome engine, results are important.

deleted 34 characters in body
Source Link
user36275
user36275

I had a situation when I needed to move all array elements in circular fashion. When I say this, I mean:

0 1 2 3
1 2 3 0
2 3 0 1
3 0 1 2

So the array:

var players = ["hash1","hash2","hash3","hash4"];

Players is the array that contains user hashes and their place on the table. Table is round, so array must rotate in circular fashion.

Step dictates by how much it should move.

I came up with following algorithm (if you can call that, probably not) that works fine. I was just wondering if there was a more efficient way. Or cleaner way to do same?

offset variable is probably wrongly named, I couldn't come up with a better name.

var step = 0 // 3,2,1,0
    var offset = 0;
    var players_new = [];
    for (var i = 0; i <= players.length - 1; i++) {
        if (i + step <= players.length - 1) {
            players_new[i + step] = players[i];
            offset++;
        } else {
            players_new[i - offset] = players[i];
        }
    };

I tested my version against proposed pushmany different versions, and shift. Mine appears to be loads faster:looks like we have a winner http://jsperf.com/rotate-array:) vazha's version is the fastest all around, except in firefox.

Any other clever solutions?http://jsperf.com/rotate-array-javascript

I also tested rolfs solutionSince, and it was the slowest. Trailing 80% slower in the back: http://jsperf.com/rotate-array/3

So I'm going to stick with my solutionusing this code in node.js which uses chrome engine, for me chrome results are important.

I had a situation when I needed to move all array elements in circular fashion. When I say this, I mean:

0 1 2 3
1 2 3 0
2 3 0 1
3 0 1 2

So the array:

var players = ["hash1","hash2","hash3","hash4"];

Players is the array that contains user hashes and their place on the table. Table is round, so array must rotate in circular fashion.

Step dictates by how much it should move.

I came up with following algorithm (if you can call that, probably not) that works fine. I was just wondering if there was a more efficient way. Or cleaner way to do same?

offset variable is probably wrongly named, I couldn't come up with a better name.

var step = 0 // 3,2,1,0
    var offset = 0;
    var players_new = [];
    for (var i = 0; i <= players.length - 1; i++) {
        if (i + step <= players.length - 1) {
            players_new[i + step] = players[i];
            offset++;
        } else {
            players_new[i - offset] = players[i];
        }
    };

I tested my version against proposed push and shift. Mine appears to be loads faster: http://jsperf.com/rotate-array

Any other clever solutions?

I also tested rolfs solution, and it was the slowest. Trailing 80% slower in the back: http://jsperf.com/rotate-array/3

So I'm going to stick with my solution.

I had a situation when I needed to move all array elements in circular fashion. When I say this, I mean:

0 1 2 3
1 2 3 0
2 3 0 1
3 0 1 2

So the array:

var players = ["hash1","hash2","hash3","hash4"];

Players is the array that contains user hashes and their place on the table. Table is round, so array must rotate in circular fashion.

Step dictates by how much it should move.

I came up with following algorithm (if you can call that, probably not) that works fine. I was just wondering if there was a more efficient way. Or cleaner way to do same?

offset variable is probably wrongly named, I couldn't come up with a better name.

var step = 0 // 3,2,1,0
    var offset = 0;
    var players_new = [];
    for (var i = 0; i <= players.length - 1; i++) {
        if (i + step <= players.length - 1) {
            players_new[i + step] = players[i];
            offset++;
        } else {
            players_new[i - offset] = players[i];
        }
    };

I tested many different versions, and looks like we have a winner :) vazha's version is the fastest all around, except in firefox.

http://jsperf.com/rotate-array-javascript

Since, I'm using this code in node.js which uses chrome engine, for me chrome results are important.

added 38 characters in body
Source Link
user36275
user36275

I had a situation when I needed to move all array elements in circular fashion. When I say this, I mean:

0 1 2 3
1 2 3 0
2 3 0 1
3 0 1 2

So the array:

var players = ["hash1","hash2","hash3","hash4"];

PlayersPlayers is the array that contains user hashes and their place on the table. Table is round, so array must rotate in circular fashion.

StepStep dictates by how much it should move.

I came up with following algorithm (if you can call that, probably not) that works fine. I was just wondering if there was a more efficient way. Or cleaner way to do same?

offset variable is probably wrongly named, I couldn't come up with a better name.

var step = 0 // 3,2,1,0
    var offset = 0;
    var players_new = {};[];
    for (var i = 0; i <= 3;players.length - 1; i++) {
        if (i + step <= 3players.length - 1) {
            players_new[i + step] = players[i];
            offset++;
        } else {
            players_new[i - offset] = players[i];
        }
    };

I tested my version against proposed push and shift. Mine appears to be loads faster: http://jsperf.com/rotate-array

Any other clever solutions?

I also tested rolfs solution, and it was the slowest. Trailing 80% slower in the back: http://jsperf.com/rotate-array/3

So I'm going to stick with my solution.

I had a situation when I needed to move all array elements in circular fashion. When I say this, I mean:

0 1 2 3
1 2 3 0
2 3 0 1
3 0 1 2

So the array:

var players = ["hash1","hash2","hash3","hash4"];

Players is the array that contains user hashes and their place on the table. Table is round, so array must rotate in circular fashion.

Step dictates by how much it should move.

I came up with following algorithm (if you can call that, probably not) that works fine. I was just wondering if there was a more efficient way. Or cleaner way to do same?

offset variable is probably wrongly named, I couldn't come up with a better name.

var step = 0 // 3,2,1,0
    var offset = 0;
    var players_new = {};
    for (var i = 0; i <= 3; i++) {
        if (i + step <= 3) {
            players_new[i + step] = players[i];
            offset++;
        } else {
            players_new[i - offset] = players[i];
        }
    };

I tested my version against proposed push and shift. Mine appears to be loads faster: http://jsperf.com/rotate-array

Any other clever solutions?

I also tested rolfs solution, and it was the slowest. Trailing 80% slower in the back: http://jsperf.com/rotate-array/3

So I'm going to stick with my solution.

I had a situation when I needed to move all array elements in circular fashion. When I say this, I mean:

0 1 2 3
1 2 3 0
2 3 0 1
3 0 1 2

So the array:

var players = ["hash1","hash2","hash3","hash4"];

Players is the array that contains user hashes and their place on the table. Table is round, so array must rotate in circular fashion.

Step dictates by how much it should move.

I came up with following algorithm (if you can call that, probably not) that works fine. I was just wondering if there was a more efficient way. Or cleaner way to do same?

offset variable is probably wrongly named, I couldn't come up with a better name.

var step = 0 // 3,2,1,0
    var offset = 0;
    var players_new = [];
    for (var i = 0; i <= players.length - 1; i++) {
        if (i + step <= players.length - 1) {
            players_new[i + step] = players[i];
            offset++;
        } else {
            players_new[i - offset] = players[i];
        }
    };

I tested my version against proposed push and shift. Mine appears to be loads faster: http://jsperf.com/rotate-array

Any other clever solutions?

I also tested rolfs solution, and it was the slowest. Trailing 80% slower in the back: http://jsperf.com/rotate-array/3

So I'm going to stick with my solution.

added 166 characters in body
Source Link
user36275
user36275
Loading
added 151 characters in body
Source Link
user36275
user36275
Loading
Use standard terminology in title, add array tag.
Link
rolfl
  • 97.5k
  • 17
  • 218
  • 418
Loading
Post Migrated Here from stackoverflow.com (revisions)
Source Link
user36275
user36275
Loading