我的问题源自这里的Jsperf测试:http://jsperf.com/the-fastest-way-to-truncate-an-array/7
设置代码如下:
Benchmark.prototype.setup = function() {
var test_array = [];
for (var j=0; j< 10000; j++){
test_array[j] = 'AAAAAAAAAA';
}
};
还有测试:
// Array.slice() method
result = test_array.slice(0,100);
// Array.splice() method
test_array.splice(100);
// Modifying Array.length
test_array.length = 100;
// Array.pop() method
while (test_array.length > 100) test_array.pop();
JSPerf的结果显示,Array.pop()
方法比其他方法快得多,有些实现甚至快了80倍。
这是怎么回事呢?在循环中使用Array.pop()
真的比其他测试要快这么多吗?还是我没有看到测试中的缺陷?