Instanceof vs Typeof function

JavaScript performance comparison

Revision 2 of this test case created by Eamon Nerbonne

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
0<script>
function SomeFunc(x,y,z) { return x + y * x; }
var constructed =new (function() {}); 

var vals = [
  15,
  SomeFunc,
  "joey!",
  [1,2,3],
  {},
  function() {},
  constructed,
];
var a1=false; //ensure no dead code removal.

function test_func(func) {
  return func(vals[0]) !=
    func(vals[1]) != 
    func(vals[2]) !=
    func(vals[3]) !=
    func(vals[4]) !=
    func(vals[5]) !=
    func(vals[6]);
}
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var a2=false;
  var cachedFunction = Function;

};

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

};
</script>

Preparation code output

0<script> function SomeFunc(x,y,z) { return x + y * x; } var constructed =new (function() {}); var vals = [ 15, SomeFunc, "joey!", [1,2,3], {}, function() {}, constructed, ]; var a1=false; //ensure no dead code removal. function test_func(func) { return func(vals[0]) != func(vals[1]) != func(vals[2]) != func(vals[3]) != func(vals[4]) != func(vals[5]) != func(vals[6]); } </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 (call)
a2= test_func(function(x) {
  return typeof x == "function";
});
pending…
Typeof (loop)
for(var i=0;i<vals.length;i++)
  a2= a2 != (typeof vals[i] == "function");
pending…
Typeof === (call)
a2= test_func(function(x) {
  return typeof x === "function";
});
pending…
Typeof === (loop)
for(var i=0;i<vals.length;i++)
  a2= a2 != (typeof vals[i] === "function");

pending…
Instanceof (call)
a2 = test_func(function(x) {
  return x instanceof Function;
});
pending…
Instanceof (loop)
for(var i=0;i<vals.length;i++)
  a2= a2 != (vals[i] instanceof Function);
pending…
constructor check (call)
a2 = test_func(function(x) {
  return x.constructor === cachedFunction;
});
pending…
constructor check (loop)
for(var i=0;i<vals.length;i++)
  a2= a2 != (vals[i].constructor === cachedFunction);

pending…
constructor check (call)
a2= test_func(function(x) {
  return x.constructor === Function;
});
pending…
constructor check (loop)
for(var i=0;i<vals.length;i++)
  a2= a2 != (vals[i].constructor === Function);

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

0