Stack vs Queue
Measuring time taken for removing elements from a stack vs those from a queue.
by Checha Man
HTML
<title>JS performance: Stack vs Queue</title>
<body>
<h2>Stack</h2>
<p>
A FILO/LIFO data structure with insertion and removal both from same end.
</p>
<h2>Queue</h2>
<p>
A FIFO/LILO data structure with insertion at rear end, and removal from front end.
</p>
<hr>
<h3> Removal times (ms)</h3>
<p>
Stack: <span id="time_stack"></span>
</p>
<p>
Queue: <span id="time_queue"></span>
</p>
</body>
JavaScript
var stack = [], queue = [];
var start = new Date();
var end;
const MAXNUM = 100000;
for(let i=0; i<MAXNUM; i++) {
stack.push(i);
queue.push(i);
}
console.log("Time to insert values in both: " + (new Date() - start));
// remove elements from stack
start = new Date();
while(stack.length > 0) {
stack.pop();
}
end = (new Date() - start);
console.log("Time to pop values from stack: " + end);
document.getElementById('time_stack').innerHTML = end;
// remove elements from queue
start = new Date();
while(queue.length > 0) {
queue.shift();
}
end = (new Date() - start);
console.log("Time to remove values from queue: " + end);
document.getElementById('time_queue').innerHTML = end;