queue-comparison.js 758 B

123456789101112131415161718192021222324252627282930313233
  1. const { Bench } = require('tinybench');
  2. const { ArrayTaskQueue, FixedQueue } = require('..');
  3. const QUEUE_SIZE = 100_000;
  4. const bench = new Bench({ time: 100 });
  5. bench
  6. .add('ArrayTaskQueue full push + full shift', async () => {
  7. const queue = new ArrayTaskQueue();
  8. for (let i = 0; i < QUEUE_SIZE; i++) {
  9. queue.push(i);
  10. }
  11. for (let i = 0; i < QUEUE_SIZE; i++) {
  12. queue.shift();
  13. }
  14. })
  15. .add('FixedQueue full push + full shift', async () => {
  16. const queue = new FixedQueue();
  17. for (let i = 0; i < QUEUE_SIZE; i++) {
  18. queue.push(i);
  19. }
  20. for (let i = 0; i < QUEUE_SIZE; i++) {
  21. queue.shift();
  22. }
  23. });
  24. (async () => {
  25. await bench.warmup();
  26. await bench.run();
  27. console.table(bench.table());
  28. })();