My question is spawned by the Jsperf here:
http://jsperf.com/the-fastest-way-to-truncate-an-array/7
The setup code:
Benchmark.prototype.setup = function() {
var test_array = [];
for (var j=0; j< 10000; j++){
test_array[j] = 'AAAAAAAAAA';
}
};
And the tests:
// 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's result shows that the Array.pop() method is completing faster than the others by a large margin - upwards of 80 times faster on some implementations.
What's going on here? Is Array.pop() in a loop really this much faster than the other tests? Is there a flaw in the test that I'm not seeing?