Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

Binary Search vs Array.includes() on a 50,000-Row Catalog

说明与用途

将 Array.includes() 替换为二分查找,将 50,000 元素数组的查找时间从 408 ms 降至不到 1 ms,消除了明显的 UI 延迟。

问题

Searching large sorted arrays with Array.includes() is slow compared to binary search, causing noticeable UI delays.

结果

Binary search outperformed Array.includes() by 432× for 10,000 lookups on 50,000 rows, turning a 408 ms delay into sub‑1 ms latency.

工作流

  • 1. Build a sorted array of 50,000 numbers.
  • 2. Generate 2,000 lookup targets (80 % hits, 20 % misses).
  • 3. Run Array.includes() for each target and time the total.
  • 4. Run a binarySearch function for each target and time the total.
  • 5. Compare timings and verify hit counts match.
  • 6. If binary search is faster, replace includes() with binarySearch in the codebase.
  • 7. Add a unit test covering edge cases (empty array, single item, out‑of‑range targets).

证据

Array.includes() took 408ms for 10,000 lookups against 50,000 rows, binary search took under 1ms

来源

Binary search needs a sorted array, so an unsorted catalog has to pay a one-time sort cost first

来源

A one-line swap cut a 408ms lookup loop to under 1ms with zero new dependencies

来源

  • 记录日期: 2026-10-09T09:56:07.002Z