Array item swap

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var data = [];
    for (var i = 0; i < 100; i++) data.push(i);
    var index1 = 33;
    var index2 = 66;
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
temp
var temp = data[index1];
data[index1] = data[index2];
data[index2] = temp;
pending…
splice
data.splice(index2, 1, data.splice(index1, 1, data[index2]));
 
pending…
slice
if (index1 < index2) {
  data = data.slice(0, index1).concat(data[index2], data.slice(index1 + 1, index2 - index1 - 1), data[index1], data.slice(index2 + 1));
} else {
  data = data.slice(0, index2).concat(data[index1], data.slice(index2 + 1, index1 - index2 - 1), data[index2], data.slice(index1 + 1));
}
 
pending…
splice2
data[index1] = data.splice(index2, 1, data[index1])[0];
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. Here’s a list of current revisions for this page:

0 comments

Add a comment