Skip to content

perf: make priority dequeue 240% faster#244

Merged
sindresorhus merged 1 commit into
sindresorhus:mainfrom
saripovdenis:perf/cursor_queue
Apr 26, 2026
Merged

perf: make priority dequeue 240% faster#244
sindresorhus merged 1 commit into
sindresorhus:mainfrom
saripovdenis:perf/cursor_queue

Conversation

@saripovdenis
Copy link
Copy Markdown
Contributor

Summary

Replaces PriorityQueue#dequeue() Array#shift() with a head cursor and lazy compaction.

This avoids repeatedly moving remaining array entries on every dequeue while preserving queue behavior.

The previous shift() implementation also passes the new behavior tests; the tests verify behavior preservation, while the benchmarks show the performance gain.

Benchmarks

Node.js v20.19.5

Benchmark Previous implementation Current implementation Change
large-fifo-backlog 48.32 ops/sec ±6.96% 58.61 ops/sec ±3.55% +21.3%
priority-queue-dequeue 3,756 ops/sec ±2.42% 12,790 ops/sec ±0.27% +240.5%

@saripovdenis
Copy link
Copy Markdown
Contributor Author

@sindresorhus Hello! Let me know if something is unclear in this PR!

@sindresorhus sindresorhus merged commit 87077d0 into sindresorhus:main Apr 26, 2026
2 checks passed
@saripovdenis saripovdenis deleted the perf/cursor_queue branch April 26, 2026 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants