3种方法和Javascript的效率比较,以确定它是否是数组
代码如下:
VaR RET;
var o = {1,2,3};
无功tostr = {} ToString;
var数组{ };
数组{对象数组数组;
console.log(各种判断数组的速度比较!)
console.log(方法1:Array.isArray(O));
console.time(第一);
对于(var i = 0;i < 10000;i + +){
ret = array.isarray(O);
}
Console.timeEnd(第一);
console.log(RET);
console.log(方法二:O是阵);
console.time(二);
对于(var i = 0;i < 10000;i + +){
ret = O是阵列;
}
Console.timeEnd(第二);
console.log(RET);
console.log(三:o.constructor = =阵);
console.time(第三);
对于(var i = 0;i < 10000;i + +){
ret = o.constructor = =阵列;
}
Console.timeEnd(第三);
console.log(RET);
console.log(四方法:数组{ tostr.call(O)} = = 'array);
console.time(出);
对于(var i = 0;i < 10000;i + +){
ret =数组{ tostr.call(O)} = =阵列;
}
Console.timeEnd(出);
console.log(RET);