sort compare func

JavaScript performance comparison

Test case created by Tracy Alers

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  // make an array of 1000 random numbers
  var arr = [];
  for (var i=0, t=1000; i<t; i++) {
      arr.push(Math.round(Math.random() * t))
  }

};
</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
Readable
function SortFn(a, b) {
    if (a > b) {
        return 1;
    }
    else if (a === b) {
        return 0;
    }

    return -1;
}

arr.sort(SortFn);
pending…
Obfuscated
function SortFn(a, b) {
    return +(a > b) || +(a === b) - 1;
}

arr.sort(SortFn)
pending…

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

0 Comments