JavaScript isPlainObject

Benchmark created on


Setup

var objConstructor = {}.constructor;
    var obj = {
      a: 'b',
      c: 'd'
    };
    
    function jsCheck(obj) {
      if (obj == null) return false;
      return obj.constructor === objConstructor;
    }
    
    function jqueryCheck(obj) {
      if (obj == null) return false;
      var hasOwn = Object.prototype.hasOwnProperty;
      // Not own constructor property must be Object
      if (obj.constructor && !hasOwn.call(obj, "constructor") && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
        return false;
      }
    
      // Own properties are enumerated firstly, so to speed up,
      // if last one is own, then all properties are own.
    
      var key;
      for (key in obj) {}
    
      return key === undefined || hasOwn.call(obj, key);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery 1.4.4 similar
jqueryCheck(obj);
ready
other way
jsCheck(obj);
ready

Revisions

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