Instanceof vs Typeof function

JavaScript performance comparison

Revision 3 of this test case created by Eamon Nerbonne

Preparation code

<script>
function SomeFunc(x,y,z) { return x + y * x; }

var vals = [
  15,
  SomeFunc,
  "joey!",
  [1,2,3],
  {},
  function() {},
];
var a1=false; //ensure no dead code removal.
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var a2=false;
  var cachedFunction = Function;

};

Benchmark.prototype.teardown = function() {
  a1 = a1 != a2;

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Typeof ===
for(var i=0;i<vals.length;i++)
  a2= a2 != (typeof vals[i] === "function");

pending…
Typeof ==
for(var i=0;i<vals.length;i++)
  a2= a2 != (typeof vals[i] == "function");
pending…
constructor (==)
for(var i=0;i<vals.length;i++)
  a2= a2 != (vals[i].constructor == Function);

pending…
Instanceof
for(var i=0;i<vals.length;i++)
  a2= a2 != (vals[i] instanceof Function);
pending…
Instanceof (cached)
for(var i=0;i<vals.length;i++)
  a2= a2 != (vals[i] instanceof cachedFunction);
pending…
constructor
for(var i=0;i<vals.length;i++)
  a2= a2 != (vals[i].constructor === Function);

pending…
constructor (cached)
for(var i=0;i<vals.length;i++)
  a2= a2 != (vals[i].constructor === cachedFunction);

pending…

Compare results of other browsers

Revisions

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

0 Comments