Try/Catch Cost In a Loop (v6)

Revision 6 of this benchmark created on


Description

How much does a try catch cost?

Setup

var obj = {
      a: 1,
      b: 2,
      c: 3,
      d: 4,
      e: 5
    };
    
    function trycatch(cb) {
      try {
        cb();
      } catch (e) {
        console.log(' ');
      }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Vanilla iteration
var blah = '';
for (var prop in obj) {
  blah += obj[prop];
}
ready
Iteration with internal try/catch
var blah = '';
for (var prop in obj) {
  trycatch(function() {
    blah += obj[prop];
  });
}
ready
try-catch outside of loop
var blah = '';
trycatch(function() {
  for (var prop in obj) {
    blah += obj[prop];
  }
});
ready

Revisions

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