Top 10 of 1M items in Node: sort is 88x slower than a heap

Sorting 1 million objects to show the top 10 took 328 ms in Node.js 22, while a size-10 min-heap found the same items in 3.7 ms, which is 88 times faster, and at 5 million objects the gap was 172

Sort time stayed near 300 to 330 ms whatever K was, while the heap rose from 2.7 ms at K of 10 to 105 ms at K of 100,000, where quickselect took 31 ms.

If the data lives in Postgres, ORDER BY score DESC LIMIT 10 took 24 ms without an index and 0.12 ms with one, against 688 ms for fetching every row and sorting in Node.

Read the full post

Read the full post