Stack vs Queue

2 years ago
User agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36
Test name Executions per second
Push/Pop 29460.6 Ops/sec
ES6 Spread 4.2 Ops/sec
Shift/Unshift 257.9 Ops/sec
Tests:
  • Push/Pop

    AخA
     
    const n = 10000;
    const stack = [];
    for (let i = 0; i < n; i++) {
      stack.push(i);
    }
    while (stack.length > 0) {
      let x = stack.pop();
    }
  • ES6 Spread

     
    const n = 10000;
    let stack = [];
    for (let i = 0; i < n; i++) {
      stack = [...stack, i];
    }
    let x;
    while (stack.length > 0) {
      [x, ...stack] = stack;
    }
  • Shift/Unshift

     
    const n = 10000;
    const queue = [];
    for (let i = 0; i < n; i++) {
      queue.unshift(i);
    }
    while (queue.length > 0) {
      let x = queue.shift();
    }
Open this result on MeasureThat.net