public
Last active

Beware JS Arrays as FIFO queues over 100k items

  • Download Gist
results
1 2 3 4 5 6
double queue 100000 12
double queue 200000 18
reverse queue 100000 2374
reverse queue 200000 23373
queue 100000 9
queue 200000 28699
test.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
function testDoubleQueue(count) {
var startTime = Date.now();
var arr1 = [], arr2 = [];
for(var i = 0; i < count/2; i++) {
arr1.push(i);
arr2.push(i+count/2);
}
 
while(arr1.length) {
arr1.shift();
}
while(arr2.length) {
arr2.shift();
}
var endTime = Date.now();
console.log('double queue', count, endTime - startTime);
}
 
function testReverseQueue(count) {
var startTime = Date.now();
var arr = [];
for(var i = 0; i < count; i++) {
arr.unshift(i);
}
 
while(arr.length) {
arr.pop();
}
var endTime = Date.now();
console.log('reverse queue', count, endTime - startTime);
}
 
function testQueue(count) {
var startTime = Date.now();
var arr = [];
for(var i = 0; i < count; i++) {
arr.push(i);
}
while(arr.length) {
arr.shift();
}
var endTime = Date.now();
console.log('queue', count, endTime - startTime);
}
 
testDoubleQueue(100000);
testDoubleQueue(200000);
testReverseQueue(100000);
testReverseQueue(200000);
testQueue(100000);
testQueue(200000);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.