Skip to content

perf: use packed arrays with binary search for lookups#14

Merged
sindresorhus merged 4 commits intosindresorhus:mainfrom
privatenumber:perf/packed-arrays
Feb 18, 2026
Merged

perf: use packed arrays with binary search for lookups#14
sindresorhus merged 4 commits intosindresorhus:mainfrom
privatenumber:perf/packed-arrays

Conversation

@privatenumber
Copy link
Copy Markdown
Contributor

@privatenumber privatenumber commented Feb 15, 2026

Problem

The independent lookup functions in lookup.js use long boolean chains (179 and 136 conditions for isAmbiguous and isWide respectively), which produce a large minified output and require O(n) linear scans.

Changes

  • Update scripts/build.js to generate flat [start, end] pair arrays with a shared isInRange binary search function for all independent lookup functions
  • Add tests for all 6 East Asian Width categories

Bundle size

Minified: 5,582 → 4,170 bytes (-25%)

Performance

O(n) boolean chains → O(log n) binary search. Benchmarked with 10M iterations per test:

Benchmark Before After Speedup
isAmbiguous (worst case) 556ms 143ms 3.9x
isWide (worst case) 468ms 146ms 3.2x
isAmbiguous (hit) 358ms 120ms 3.0x
getCategory (mixed) 6,637ms 1,922ms 3.5x

Ref: sindresorhus/string-width#71

Replace boolean chain functions for isAmbiguous and isWide with flat [start, end] arrays and O(log n) binary search. Reduces minified bundle size by 25% (5,582 → 4,170 bytes).

Ref: sindresorhus/string-width#71
@sindresorhus
Copy link
Copy Markdown
Owner

Move the packed array + binary search logic into scripts/build.js
so lookup.js is properly generated instead of hand-edited.
@privatenumber privatenumber marked this pull request as ready for review February 15, 2026 11:20
Comment thread scripts/build.js Outdated
Comment thread lookup.js Outdated
Comment thread lookup.js Outdated
@privatenumber
Copy link
Copy Markdown
Contributor Author

Addressed!

@sindresorhus sindresorhus merged commit 49edb8b into sindresorhus:main Feb 18, 2026
2 checks passed
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