Fast median of three

JavaScript performance comparison

Test case created by Jonas Wagner

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var a = ~~(Math.random()*1024),
      b = ~~(Math.random()*1024),
      c = ~~(Math.random()*1024);

};

Benchmark.prototype.teardown = function() {
  // break any possible escape analysis
  window.a = a;

};
</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
Xor
var max = Math.max(Math.max(a,b),c),
    min = Math.min(Math.min(a,b),c);
    // the max and the min value cancel, the median remains
a = a^b^c^max^min;
pending…
Array sort
a = [a, b, c].sort()[1];
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