array-copy

JavaScript performance comparison

Test case created

Preparation code

<script>
var lengths = [
   32768, 16384, 4096,
   4096, 2048, 1024,  123,
    512,  256,  128,   64,
     32,   16,    8,    4,
                  2,    1
];

var sources = new Array(lengths.length);
var dests = new Array(lengths.length);

for(var i = 0; i < lengths.length; ++i) {
  var n = lengths[i];
  sources[i] = new Int32Array(n);
  dests[i] = new Int32Array(n);
  while(--n > -1) {
    sources[n] = n;
    dests[n] = 0;
  }
}

window.lengths = lengths;
window.sources = sources;
window.dests = dests;
</script>
<script>
Benchmark.prototype.setup = function() {
    var lengths = window.lengths;
    var sources = window.sources;
    var dests = window.dests;
   
    function copy0(source, sourceOff, dest, destOff, len) {
      var i = len;
      var j = sourceOff + len;
      var k = destOff + len;
      while(--i > -1) {
        dest[--k] = source[--j];
      }
    }
   
    function copy1(source, sourceOff, dest, destOff, len) {
      len = len|0;
      sourceOff = sourceOff|0;
      destOff = destOff|0;
      var i = (len - 1)|0;
      var j = (sourceOff + i)|0;
      var k = (destOff + i)|0;
      while(i > -1) {
        dest[k] = source[j];
        k=(k-1)|0;
        j=(j-1)|0;
        i = (i-1)|0;
      }
    }
    function c0() {
      for(var i = 0; i < lengths.length; ++i) {
        copy0(sources[i], 0, dests[i], 0, lengths[i]);
      }
    }
   
    function cN() {
      for(var i = 0; i < lengths.length; ++i) {
        copy1(sources[i], 0, dests[i], 0, lengths[i]);
      }
    }
};
</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
copy0
c0();
pending…
copyN
cN();
pending…

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

Compare results of other browsers

0 comments

Add a comment