Skip to content
Create a gist now

Instantly share code, notes, and snippets.

@dfellis /results
Last active Dec 19, 2015

Beware JS Arrays as FIFO queues over 100k items
double queue 100000 12
double queue 200000 18
reverse queue 100000 2374
reverse queue 200000 23373
queue 100000 9
queue 200000 28699
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.