Repository navigation
Description
Proposal: Optimize EmojiTrie Build Performance
Background
The app uses a custom prefix trie for emojis. This data-structure is used to enable fast autocomplete. The buildEmojisTrie is called on App startup, along with locale loading logic.
Problem
When a user opens the emoji picker for the first time, the trie build blocks the JS thread longer than necessary. There are 4 inefficiencies we found:
Recursive inserts with string copies: Trie.add recurses once per character and calls substring(1) at each level, allocating a new string on every step
Double trie traversal per keyword: Every keyword does a search to check if it exists, then a separate add or update — walking the same path twice
Suggestions array rebuilt on every collision: When two emojis share a keyword, the entire suggestions array is recreated via spread rather than appended to
Quadratic name-part allocation: getNameParts uses slice(index).join('_') for each suffix of the name, generating O(n²) intermediate arrays and strings
Solution
Iterative inserts: Rewrite Trie.add, search, and update to use index-based character access in a loop, eliminating all per-character string allocations
Single-pass getOrCreate: Add a method that walks to the end node once and returns {node, isNew}, replacing the search + add/update double traversal everywhere
Mutable suggestions: Initialize suggestions as a stable array and push/unshift into it directly. Since the normalized keyword node shares the same metadata object by reference, it stays in sync automatically — no extra update needed
Linear name-part generation: Replace the map+slice+join in getNameParts with a single indexOf loop that appends suffixes directly with no intermediate allocations
Results:
Web: 26ms - 16ms (-38.4%)
iOS: 140ms - 95ms (-32.14%)
Upwork Automation - Do Not Edit
- Upwork Job URL: https://www.upwork.com/jobs/~022039022812847691682
- Upwork Job ID: 2039022812847691682
- Last Price Increase: 2026-03-31
Activity
melvin-bot commentedon Mar 31, 2026
Current assignee @shubham1206agra is eligible for the External assigner, not assigning anyone new.
[-]Optimize EmojiTrie Build Performance[/-][+][$250] Optimize EmojiTrie Build Performance[/+]melvin-bot commentedon Mar 31, 2026
Job added to Upwork: https://www.upwork.com/jobs/~022039022812847691682
roryabraham commentedon Mar 31, 2026
@shubham1206agra is acting as contributor for this one. Help not needed for now
MelvinBot commentedon Mar 31, 2026
Proposal
What is the root cause of that problem?
The
buildEmojisTriefunction and underlyingTrieclass have four compounding algorithmic inefficiencies that cause excessive memory allocation and CPU work during trie construction:O(N²) string allocations per word in all Trie methods.
Trie.add()(src/libs/Trie/index.ts:21-36) recurses once per character and callssubstring(1)at each level, creating N intermediate strings of decreasing length. The same pattern exists insearch()andupdate()(lines 42-70) which usesubstring(1)in while loops. Additionally,add()redundantly callstoLowerCase()on every recursive frame (line 22), even though the string is already lowercased from the first call.Double trie traversal per keyword. In
addKeywordsToTrie(src/libs/EmojiTrie.ts:26-49) andcreateTrie(lines 80-93), every keyword is first searched viatrie.search(keyword), then separately added/updated viatrie.add(keyword, ...)ortrie.update(keyword, ...). Both operations walk the same character-by-character path, doubling the traversal cost. When accent-normalized forms differ from the original, this becomes up to 4 traversals per keyword.O(K²) suggestions array copies on keyword collisions. When multiple emojis share a keyword (
src/libs/EmojiTrie.ts:37-43), the entire existing suggestions array is spread into a new array ([...(keywordNode.metaData.suggestions ?? []), suggestion]) instead of being mutated withpush()/unshift(). For K emojis sharing a keyword, this copies O(K²) total elements.O(N²) intermediate allocations in
getNameParts. The function (src/libs/EmojiTrie.ts:58-61) usesnameSplit.map((_, index) => nameSplit.slice(index).join('_')), which for each suffix creates a new array viaslice()and a new string viajoin(), yielding quadratic work in the number of underscore-separated parts.What changes do you think we should make in order to solve the problem?
Iterative index-based Trie methods: Rewrite
add(),search(), andupdate()to use a character index variable in a loop (e.g.,word.charCodeAt(i)) instead ofsubstring(1). CalltoLowerCase()once at the entry point only. This eliminates all per-character string allocations, reducing each operation from O(N²) to O(N) character work.Single-pass
getOrCreatemethod: Add a new method toTriethat walks to the end node once and returns{node, isNew}. Replace thesearch+add/updatedouble-traversal pattern in bothaddKeywordsToTrieandcreateTriewith this single-pass operation.Mutable suggestions arrays: Initialize
suggestionsas a stable array and usepush()/unshift()directly. Since the trie node's metadata references the same array object, changes are reflected everywhere without needing to create new arrays or metadata objects.Linear
getNameParts: Replace themap+slice+joinpattern with a singleindexOf('_')loop that usesstring.substring(pos)to extract each suffix directly from the original string, avoiding all intermediate array allocations.What alternative solutions did you explore? (Optional)
The proposed in-place algorithmic improvements are the simplest and most effective approach: they directly reduce the work done without architectural changes.
Next Steps for Contributor+ team: Reply with
@MelvinBot implement thisto create a draft PR,@MelvinBot <your feedback>to refine this analysis, or explain why you are rejecting Melvin's proposal.rinej commentedon Apr 3, 2026
@elirangoshen draft PR #86313 touches the same area - it both defers the trie build to first use and replaces the Trie with a flat Map + sorted array with binary search
There's overlap, but I think the safest path forward might be to combine the best parts of both:
Defer the trie build to first use (from Eli's PR) - right now buildEmojisTrie is called eagerly in LocaleContextProvider on startup. Eli's PR introduces a lazy getEmojiSearchIndex() that only builds on the first actual emoji interaction. The same lazy init pattern can be applied to the existing Trie without replacing the data structure - should be a big win for startup with minimal risk
Optimize the Trie internals (from this proposal) - iterative inserts, single-pass getOrCreate, mutable suggestions, and linear name-part generation are great, without changing the public API.
Skip the full data structure replacement - swapping the Trie for a Map + binary search has the largest regression risk (new search semantics, new API for consumers in EmojiUtils.tsx, test changes across multiple files) and may not be worth the risk, the part 1 and ) together should address the performance concerns
Curious what do you think?
23 remaining items
jponikarchuk commentedon Apr 9, 2026
Deploy Blocker #87453 was identified to be related to the PR #87253 that attempted to fix this issue.
melvin-bot commentedon Apr 16, 2026
Payment Summary
Upwork Job
Reviewer: @shubham1206agra owed $250 via NewDotBugZero Checklist (@mallenexpensify)
roryabraham commentedon Apr 16, 2026
I ended up doing the reviewer checklist on this one, so I don't think any payments are due
roryabraham commentedon Apr 16, 2026
No payments due
roryabraham commentedon Apr 16, 2026
Sorry, I made a mistake. I think we had two separate pull requests for this same issue:
mallenexpensify commentedon Apr 17, 2026
Payment Summary
Contributor+: @shubham1206agra due $250 via NewDot
Contributor+: @rojiphil due $250 via NewDot
JmillsExpensify commentedon May 1, 2026
$250 approved for @shubham1206agra
quinthar commentedon Jun 29, 2026
Approved $250 to @rojiphil