Synchronous Error Passing vs Throwing

JavaScript performance comparison

Test case created by Athan Clark

Info

Here we will test to see if passing values and including the error is faster than throwing an error if one were to happen. In all these tests, an error will not be thrown/passed - to see real-world speeds.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var console = console ? console : { "log": function() {} };
   
    function recurse(loops) {
      if (loops >= 0) {
        recurse(--loops);
      } else {
        return 0;
      }
    }
   
    function cjsCallback(loops, callback) {
      if (loops >= 0) {
        cjsCallback(--loops, callback);
      } else {
        callback.call(null, 0);
      }
    }
};
</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
Try/Catch Recurse
try {
  recurse(10);
} catch(e) {
  console.log(e);
}
pending…
Instanceof/Return Recurse
var res = recurse(10);
if (res instanceof Error) {
  console.log(res);
}
pending…
CJS Callback Recurse
cjsCallback(10, function back(err, res) {
  if (err === null) {
  } else {
    console.log(err);
  }
});
pending…
Try/Catch Anonymous
try { (function() {
(function() {
(function() {
(function() {
(function() {
(function() {
(function() {
(function() {
(function() {
(function() {
  return 0;
})()
})()
})()
})()
})()
})()
})()
})()
})()
})()
} catch(e) {
  console.log(e);
}
pending…
Instanceof/Return Anonymous
var res = (function() {
(function() {
(function() {
(function() {
(function() {
(function() {
(function() {
(function() {
(function() {
(function() {
  return 0;
})()
})()
})()
})()
})()
})()
})()
})()
})()
})();

if (res instanceof Error) {
  console.log(res);
}
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