Testing Member vs. Catching Undefined

Benchmark created by whatgoodisaroad on


Preparation HTML

<script>
  var obj = {
   foo: 4,
   bar: "hello"
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Using Ternary
var x = obj.hasOwnProperty("y") ? obj.y : null;
ready
Using If
var x;
if (obj.hasOwnProperty("y")) {
 x = obj.y;
}
else {
 x = null;
}
ready
Using Try
var x;
try {
 x = obj.y;
}
catch (exc) {
 x = null;
}
ready

Revisions

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

  • Revision 1: published by whatgoodisaroad on