typeof与instanceof性能测试

测试结果:用typeof性能要高于instanceof,约为20+倍。

var count = 10000000;
var func = function () {};
var startTime = new Date();
console.log(typeof func === "function");
for (var j = 0; j < count; j++){
typeof func === "function";
}
console.log('[typeof func === "function"] ' + (new Date().getTime() - startTime.getTime()));
startTime = new Date();
console.log(func instanceof Function);
for (var k = 0; k < count; k++){
func instanceof Function;
}
console.log('[func instanceof Function] ' + (new Date().getTime() - startTime.getTime()));

某一次测试结果:

true
[typeof func === "function"] 23
true
[func instanceof Function] 500




posted @ 2012-04-01 10:34  BlackTea  阅读(820)  评论(0)    收藏  举报