instanceof vs. typeof

Benchmark created on


Preparation HTML

<script>
  var things = [
  void 0, null, 123, "456", ["a", "b", "c", "d", "e"],
  {
   a: "b",
   c: "d",
   e: "f"
  }];
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
typeof
var types = [];
for (var i, len = things.length; i < len; i++) {
 var value = things[i];
 types[i] = value !== null && typeof value === "object";
}
ready
typeof2
var types = [];
for (var i, len = things.length; i < len; i++) {
 var value = things[i];
 types[i] = typeof value === "object" && value !== null;
}
ready
instanceof
var types = [];
for (var i, len = things.length; i < len; i++) {
 types[i] = things[i] instanceof Object;
}
ready
instanceof2
var types = [],
    O = Object;
for (var i, len = things.length; i < len; i++) {
 types[i] = things[i] instanceof O;
}
ready

Revisions

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