Repository navigation
Conversation
| * 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. |
There was a problem hiding this comment.
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;
}There was a problem hiding this comment.
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
Please just add a changeset entry. I'll handle it before releasing. |
ca07861 to
49e1d5e
Compare
🦋 Changeset detectedLatest commit: 49e1d5e The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
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 thancollectGraphemes().Motivation & Use Case
My use case involves encoding a string to UTF-8 via
TextEncoder.encodeInto(str, destU8Array)and maintaining a parallel 1:1Uint8Arraymarker 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()orcollectGraphemes()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.