Native Try/Catch vs. Custom tryCatch Function in a Loop (v13)

Revision 13 of this benchmark created on


Description

Investigating a means of replacing the try catch block with a custom solution for performance intensive operations in a loop.

Setup

function tryCatch (tryFn, catchFn) {
      try {
        tryFn()
      } catch (e) {
        catchFn(e)
      }
    }
    
    function tryError (fn) {
      try {
        fn()
      } catch (e) {
        return e
      }
    }
    
    function cachedTry () {
      if (Math.random() > 0.9) {
        throw new Error
      }
    }
    
    function cachedCatch (e) {
      console.log(e)
    }

Test runner

Ready to run.

Testing in
TestOps/sec
try/catch
try {
  if (Math.random() > 0.9) {
    throw new Error
  }
} catch (e) {
  console.log(e)
}
ready
tryCatch(inline)
tryCatch(function () {
  if (Math.random() > 0.9) {
    throw new Error
  }
}, function (e) {
  console.log(e)
})
ready
tryCatch(cachedTry)
tryCatch(cachedTry, function (e) {
  console.log(e)
})
ready
tryCatch(cachedCatch)
tryCatch(function () {
  if (Math.random() > 0.9) {
    throw new Error
  }
}, cachedCatch)
ready
tryError(inline)
var e = tryError(function () {
  if (Math.random() > 0.9) {
    throw new Error
  }
})

if (e) {
  console.log(e)
}
ready
tryError(cachedTry)
var e = tryError(cachedTry)

if (e) {
  console.log(e)
}
ready

Revisions

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