Math conditions vs logical conditions

JavaScript performance comparison

Test case created by math vs log

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function calcMatch(r, p) {
      var r = r.split(':');
      var p = p.split(':');
     
      if (r[0] === p[0] && r[1] === p[1]) return 2;
      else if (
        r[0] < r[1] && p[0] < p[1] ||
        r[0] > r[1] && p[0] > p[1]) return 1;
      else return 0;
    }
   
    function calcMatch1(r, p) {
      var r = r.split(':');
      var p = p.split(':');
     
      if (r[0] === p[0] && r[1] === p[1]) return 2;
      else if ((r[0] - r[1]) * (p[0] - p[1]) > 0) return 1;
      else return 0;
    }
   
    var r = '3:4';
   
    var p1 = Math.ceil(Math.random() * 5);
    var p2 = Math.ceil(Math.random() * 5) + 5;
   
    var p = p1 + ':' + p2;
   
};

Benchmark.prototype.teardown = function() {
    p1 = Math.ceil(Math.random() * 5);
    p2 = Math.ceil(Math.random() * 5) + 5;
   
    p = p1 + ':' + p2;
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Logical
calcMatch(r,p);
pending…
Math
calcMatch1(r,p)
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