instanceof performance (v13)

Revision 13 of this benchmark created by eban on


Setup

function Test() {
      this.clazz = "Test";
    }
    
    function Test2() {
      this.clazz = "Test2";
    }
    
    function Test3() {
      this.clazz = "Test3";
    }
    
    Test2.prototype = new Test();
    Test3.prototype = new Test2();
    
    var result;
    var arr = [];
    for (var i = 0; i < 1000; i++) {
      arr.push(new Test());
    }
    
    var ts = [];
    for (var i = 0; i < 1000; i++) {
      ts.push(new Test());
    }
    var ts2 = [];
    for (var i = 0; i < 1000; i++) {
      ts2.push(new Test2());
    }
    var ts3 = [];
    for (var i = 0; i < 1000; i++) {
      ts3.push(new Test3());
    }

Test runner

Ready to run.

Testing in
TestOps/sec
instanceof 1
for (var i = 0; i < 1000; ++i) {
  var t = ts[i];
  result = t instanceof Test;
}
ready
instanceof 2
for (var i = 0; i < 1000; ++i) {
  var t = ts2[i];
  result = t instanceof Test2;
}
ready
opti worst
for (var i = 0; i < 1000; ++i) {
  var t = ts[i];
  result = t.clazz == "Test3" || t instanceof Test3;
}
ready
opti 0
for (var i = 0; i < 1000; ++i) {
  var t = ts3[i];
  result = t.clazz == "Test2" || t instanceof Test2;
}
ready
opti success
for (var i = 0; i < 1000; ++i) {
  var t = ts3[i];
  result = t.clazz == "Test3" || t instanceof Test3;
}
ready

Revisions

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