array iteration timing differences

JavaScript performance comparison

Test case created

Info

javascript test of http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
  var length = 32768;
  data = []; //global
  for (var i = 0; i < length; i++) {
    data[i] = Math.floor(Math.random() * 256);
  }

</script>
<script>
Benchmark.prototype.setup = function() {
    var test = function() {
        var start = +new Date();
   
        for (var i = 0; i < 1000; i++) {
          for (var c = 0; c < length; c++) {
            if (data[c] >= 128) {
              //sum += data[c];
            }
          }
        }
        console.log( +new Date() - start );
    }
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
First iteration
test();
console.log('iter');
pending…
Second iteration
test();
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