Check if object has property (v3)

Revision 3 of this benchmark created on


Description

Comparing use of prop in object vs. typeof object !== 'undefined'

Setup

window.checkObjectTestExample = {
      'pronic': 42
    };

Teardown


    delete window.checkObjectTestExample;
  

Test runner

Ready to run.

Testing in
TestOps/sec
prop in object
if ('pronic' in window.checkObjectTestExample) {
  window.checkObjectTestExample.pronic = 56;
}
ready
typeof object
if (typeof window.checkObjectTestExample.pronic !== 'undefined') {
  window.checkObjectTestExample.pronic = 56;
}
ready
explicit undef check
if (window.checkObjectTestExample.pronic !== undefined) {
  window.checkObjectTestExample.pronic = 56;
}
ready
implicit falsey cast
if (window.checkObjectTestExample.pronic) {
  window.checkObjectTestExample.pronic = 56;
}
ready
key deref
if (window.checkObjectTestExample['pronic'] !== undefined) {
  window.checkObjectTestExample.pronic = 56;
}
ready
loose null comparison
if (window.checkObjectTestExample.pronic != null) {
  window.checkObjectTestExample.pronic = 56;
}
ready
hasOwnProperty
if (window.checkObjectTestExample.hasOwnProperty('pronic')) {
  window.checkObjectTestExample.pronic = 56;
}
ready

Revisions

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