reverse vs negate compare

Benchmark created on


Setup

function compare(a, b) {
      return b - a;
    }
    var A = new Array(10000).join(",").split(",").map(function() { return Math.random(); });

Test runner

Ready to run.

Testing in
TestOps/sec
reverse
A.sort(compare);
A.reverse();
A.sort(compare);
ready
wrapper
var ascending = true;
A.sort(function(a, b) {
  return compare(a, b) * (ascending  ? -1 : +1);
});

ascending = false
A.sort(function(a, b) {
  return compare(a, b) * (ascending  ? -1 : +1);
});
ready

Revisions

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