if-vs-try-catch

Benchmark created by NiGhTTraX on


Setup

var SIZE = 100,
        THROW_HERE = 50,
        A = [],
        X = 0;
    
    
    for (var i = 0; i < SIZE; i++) {
      A.push({foo: 2});
    }
    
    
    A[THROW_HERE] = null;

Test runner

Ready to run.

Testing in
TestOps/sec
if
for (var i = 0; i < SIZE; i++) {
  if (A[i] !== null) {
    X += A[i].foo;
  }
}
console.log(X);
ready
try catch
for (var i = 0; i < SIZE; i++) {
  try {
    X += A[i].foo;
  } catch(e) {};
}
console.log(X);
ready

Revisions

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

  • Revision 1: published by NiGhTTraX on