Array.prototype.move

JavaScript performance comparison

Revision 19 of this test case created by Shane Brinkman-Davis Delamore

Preparation code

<script>
  function newArray(size) {
    var array = new Array(size);
    for (i=0; i < array.length; i++) array[i] = i;
    return array;
  }
  var arraySmall = newArray(200);
  var arrayMed = newArray(300);
  var arrayLarge = newArray(400);
  
  Array.prototype.move1 = function(from, to) {
    this.splice(to, 0, this.splice(from, 1)[0]);
  };
  
  Array.prototype.move2 = function(pos1, pos2) {
    // local variables
    var i, tmp;
    // cast input parameters to integers
    pos1 = parseInt(pos1, 10);
    pos2 = parseInt(pos2, 10);
    // if positions are different and inside array
    if (pos1 !== pos2 && 0 <= pos1 && pos1 <= this.length && 0 <= pos2 && pos2 <= this.length) {
      // save element from position 1
      tmp = this[pos1];
      // move element down and shift other elements up
      if (pos1 < pos2) {
        for (i = pos1; i < pos2; i++) {
          this[i] = this[i + 1];
        }
      }
      // move element up and shift other elements down
      else {
        for (i = pos1; i > pos2; i--) {
          this[i] = this[i - 1];
        }
      }
      // put element from position 1 to destination
      this[pos2] = tmp;
    }
  }
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
move1 - large size, big move
var array = arrayLarge;
var size = array.length;
array.move1(0, size / 2);
array.move1(size - 1, size / 2);
pending…
move2 - large size, big move
var array = arrayLarge;
var size = array.length;
array.move2(0, size / 2);
array.move2(size - 1, size / 2);
pending…
move1 - small size, small move
var array = arraySmall;
var size = array.length;
array.move1(0, 5);
array.move1(size - 1, size - 6);
pending…
move2 - small size, small move
var array = arraySmall;
var size = array.length;
array.move2(0, 5);
array.move2(size - 1, size - 6);
pending…
move1 - small size, big move
var array = arraySmall;
var size = array.length;
array.move1(0, size / 2);
array.move1(size - 1, size / 2);
pending…
move2 - small size, big move
var array = arraySmall;
var size = array.length;
array.move2(0, size / 2);
array.move2(size - 1, size / 2);
pending…
move1 - medium size, small move
var array = arrayMed;
var size = array.length;
array.move1(0, 5);
array.move1(size - 1, size - 6);
pending…
move2 - medium size, small move
var array = arrayMed;
var size = array.length;
array.move2(0, 5);
array.move2(size - 1, size - 6);
pending…
move1 - medium size, big move
var array = arrayMed;
var size = array.length;
array.move1(0, size / 2);
array.move1(size - 1, size / 2);
pending…
move2 - medium size, big move
var array = arrayMed;
var size = array.length;
array.move2(0, size / 2);
array.move2(size - 1, size / 2);
pending…
move1 - large size, small move
var array = arrayLarge;
var size = array.length;
array.move1(0, 5);
array.move1(size - 1, size - 6);
pending…
move2 - large size, small move
var array = arrayLarge;
var size = array.length;
array.move2(0, 5);
array.move2(size - 1, size - 6);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments