Type checking

Benchmark created on


Description

A test comparing instanceof, typeof, property access and in.

Setup

function Test()
    {
    this.property = true;
    }
    
    function Test2()
    {
    }
    
    var arr = [], ts2 = [];
    var result;
    for(var i = 0; i < 1000; i++) {
    arr.push(new Test());
        ts2.push(new Test2());
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Instanceof success
for(var i = 0; i < 1000; ++i)
{
var t = arr[i];
result = t instanceof Test;
}
ready
Property success
for(var i = 0; i < 1000; ++i)
{
var t = arr[i];
result = t.property;
}
ready
Instanceof fail
for(var i = 0; i < 1000; ++i)
{
var t = ts2[i];
result = t instanceof Test;
}
ready
Property fail
for(var i = 0; i < 1000; ++i)
{
var t = ts2[i];
result = t.property;
}
ready
Typeof success
for(var i = 0; i < 1000; ++i)
{
var t = arr[i];
result = typeof t === 'object';
}
ready
Typeof fail
for(var i = 0; i < 1000; ++i)
{
var t = ts2[i];
result = typeof t === 'string';
}
ready
In success
for(var i = 0; i < 1000; ++i)
{
var t = arr[i];
result = 'property' in t;
}
ready
In fail
for(var i = 0; i < 1000; ++i)
{
var t = ts2[i];
result = 'property' in t;
}
ready

Revisions

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