hasOwnProperty vs in vs undefined vs ... (v28)

Revision 28 of this benchmark created on


Description

Optimistic case, method exists 99%.

Setup

function A () {};
    A.prototype.has$method = true
    A.prototype.method = function (arg) { return arg; };
    A.prototype.supports = 0x11111;
    A.prototype.send = function (name)
    {try{
      return this[name]();
    }catch (e){ return null; } }
    
    function send (o, name)
    {try{
      return o[name]();
    }catch (e){ return null; } }
    
    function $mm (name) { return; }
    
    var obj = new A();
    var METHOD = 0x1;
    var hasOwn = Object.prototype.hasOwnProperty;
    var methodString = "method";

Test runner

Ready to run.

Testing in
TestOps/sec
hasOwnProperty
if (obj.hasOwnProperty("method"))
  obj.method();
 
ready
in
if ("method" in obj)
  obj.method();
 
ready
undefined
if (typeof obj.method !== 'undefined')
  obj.method();
 
ready
undefined cmp
if (obj.method !== undefined)
  obj.method();
 
ready
binary indicator
if ((obj.supports & METHOD) !== 0)
  obj.method();
ready
undefined cmp (dynamic)
if (obj['method'] !== undefined)
  obj.method();
ready
undefined dynamic
if (typeof obj['method'] !== 'undefined')
  obj.method();
ready
hasOwnProperty prototype
if (Object.prototype.hasOwnProperty.call(obj, "method"))
  obj.method();
ready
hasOwnProperty var saved
if (hasOwn.call(obj, "method"))
  obj.method();
ready
Truthy
if (!!obj['method'])
  obj.method();
ready
not void 0
if (obj['method'] !== void 0)
  obj.method();
ready
try/catch
try { obj.method() } catch (e){}
ready
obj.send()
obj.send('method');
ready
send(obj)
send(obj, 'method');
ready
$mm
(obj.method || $mm('method')).call(obj)
ready
has$method
if (obj.has$method)
  obj.method()
 
ready
native call
obj.method()
ready
hasOwnProperty with var
if (obj.hasOwnProperty(methodString))
  obj.method();
 
ready
in with var
if (methodString in obj)
  obj.method();
 
ready

Revisions

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