测试结果:用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




转载于:https://www.cnblogs.com/blacktea/archive/2012/04/01/2428138.html

更多推荐