instanceof vs. typeof (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script>
  var things = [
  void 0, null, 123, "456", ["a", "b", "c", "d", "e"],
  {
   a: "b",
   c: "d",
   e: "f"
  }];
  
  function instanceof1() {
   var types = [];
   for (var i, len = things.length; i < len; i++) {
    types[i] = things[i] instanceof Object;
   }
  }
  
  function instanceof2() {
   var types = [],
       O = Object;
   for (var i, len = things.length; i < len; i++) {
    types[i] = things[i] instanceof O;
   }
  }
  
  function typeof2() {
   var types = [];
   for (var i, len = things.length; i < len; i++) {
    var value = things[i];
    types[i] = typeof value === "object" && value !== null;
   }
  }
  
  function typeof1() {
   var types = [];
   for (var i, len = things.length; i < len; i++) {
    var value = things[i];
    types[i] = value !== null && typeof value === "object";
   }
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
typeof
typeof1();
ready
typeof2
typeof2();
ready
instanceof
instanceof1();
ready
instanceof2
instanceof2();
ready

Revisions

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