Skip to content

Add collectGraphemeIndices() as a fast method - #152

Open
wincentj wants to merge 1 commit into
cometkim:mainfrom
wincentj:collect-grapheme-indices
Open

wincentj wants to merge 1 commit into
cometkim:mainfrom
wincentj:collect-grapheme-indices

Conversation

@wincentj

Copy link
Copy Markdown

Summary

This PR introduces collectGraphemeIndices(), a fast method designed to collect input string start indices into a reusable buffer with zero string allocations and 2-4x faster performance than collectGraphemes().


Motivation & Use Case

My use case involves encoding a string to UTF-8 via TextEncoder.encodeInto(str, destU8Array) and maintaining a parallel 1:1 Uint8Array marker map to flag which bytes correspond to the start of grapheme clusters.

To achieve this, I would first collect the JavaScript string indices marking the start of each grapheme cluster with collectGraphemeIndices(). Afterward, I would run a secondary linear scan that processes the string and matches the collected indices, incrementing along the way based on the UTF-8 length of each code point to map them correctly to UTF-8 byte offsets.

Because this processing happens in performance-sensitive hot paths, allocating temporary strings or arrays via Intl.Segmenter.segment() or collectGraphemes() or [...splitGraphemes()] creates unnecessary GC pressure. collectGraphemeIndices() solves this by writing start indices directly into a reusable buffer in-place, enabling faster index collection with zero memory churn before I run a secondary UTF-8 mapping scan.

Comment thread src/grapheme.js
* the number of indices written. Useful for zero-allocation iteration or buffer reuse.
*
* @param {string} input
* @param {number[]} dest - The destination array to write start indices into.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is an input array strictly necessary for your use case? While it might be desirable to further reduce the number of temporary objects, I’d prefer to keep the API surface area as small as possible, or use a wrapper to reduce duplication.

To maintain consistency with the existing collect API, we would likely need to add a separate method something like collectGraphemeIndiciesInto().

So it would be collectGraphemeIndiciesInto() as you added ans plus:

function collectGraphemeIndicies(input) {
  let ret = [];
  collectGraphemeIndiciesInto(input, ret);
  return ret;
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is necessary for my use case. I'm ok with splitting it into those two methods if that's the direction you'd like to take.

@codecov

codecov Bot commented Nov 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (d9d3c4e) to head (ca07861).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #152   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            9         9           
  Lines         1044      1098   +54     
=========================================
+ Hits          1044      1098   +54     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cometkim

Copy link
Copy Markdown
Owner

Please just add a changeset entry. I'll handle it before releasing.

@wincentj
wincentj force-pushed the collect-grapheme-indices branch from ca07861 to 49e1d5e Compare November 17, 2026 19:32
@changeset-bot

changeset-bot Bot commented Nov 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 49e1d5e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
unicode-segmenter Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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