try vs check null vs noop

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var noop = function(a, b) {};
    var real1 = function(a, b) {
        return a > b
        };
    var real2 = function(a, b) {
        return a < b
        };
    var real3 = function(a, b) {
        return a == b
        };
   
    var null_suit = [];
    var noop_suit = [];
   
    var n = 100;
    while (n--) {
      null_suit.push(real1, real2, null, real3, null, null);
      noop_suit.push(real1, real2, noop, real3, noop, noop);
    }
};
</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
Is Null
var n = null_suit.length;
while (n--) {
  if (null_suit[n] !== null) null_suit[n](Math.random(), Math.random());
}
pending…
Is Function
var n = null_suit.length;
while (n--) {
  if (null_suit[n] instanceof Function) null_suit[n](Math.random(), Math.random());
}
pending…
try
var n = null_suit.length;
while (n--) {
  try {
    null_suit[n](Math.random(), Math.random());
  } catch (e) {}
}
pending…
noop
var n = noop_suit.length;
while (n--) {
  noop_suit[n](Math.random(), Math.random());
}
pending…
!= noop
var n = noop_suit.length;
while (n--) {
  if (noop_suit[n] !== noop) noop_suit[n](Math.random(), Math.random());
}
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