branch-prediction

JavaScript performance comparison

Revision 7 of this test case created by mcfedr

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arraySize = 32768;
  var array = [];
  var arraySorted = [];
  
  for (var i = 0; i < arraySize; ++i) {
    var value = Math.floor(Math.random() * 256);
    array[i] = value;
    arraySorted[i] = value;
  }
  
  var sum = 0;
  
  arraySorted.sort(function (a, b) {
    return a - b;
  })

};
</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
Not Sorted Array
for (var j = 0; j < arraySize; ++j) {
  if (array[j] < 128) {
    sum += array[j];
  }
}
pending…
Sorted Array
for (var j = 0; j < arraySize; ++j) {
  if (arraySorted[j] < 128) {
    sum += arraySorted[j];
  }
}
pending…
Ternary operator not sorted
for (var j = 0; j < arraySize; ++j) {
  sum += array[j] < 128? array[j]: 0;
}
pending…
Ternary operator sorted
for (var j = 0; j < arraySize; ++j) {
  sum += arraySorted[j] < 128? arraySorted[j]: 0;
}
pending…
Not sorted using 'hack'
var t;
for (var j = 0; j < arraySize; ++j) {
  t = (array[j] - 128) >> 32;
  sum += ~t & array[j];
}
pending…
Sorted using hack
var t;
for (var j = 0; j < arraySize; ++j) {
  t = (arraySorted[j] - 128) >> 32;
  sum += ~t & arraySorted[j];
}
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