Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster emojify() by avoiding string.replace() entirely #4049

Merged
merged 1 commit into from
Jul 3, 2017

Conversation

nolanlawson
Copy link
Contributor

(Previously: #4046)

This speeds up emojify() by avoiding string.replace(regexp) entirely. Instead I pull in a small extra dependency I wrote, substring-trie, which implements a Trie data structure to do a prefix-based lookup that's faster than what emojione.regUnicode was doing.

Testing on a Nexus 5X in Chrome, I see the cost of emojify for switching from the "getting started" column to the local timeline go from 24.4ms to 2.3ms:

screenshot 2017-07-02 19 10 18

screenshot 2017-07-02 19 09 19

I used performance.measure() to measure the cost of building the Trie itself, and I got 18.7ms, so it's worth the tradeoff. Also substring-trie is 407 bytes minified.

Note also the extra tests, which I wrote to ensure I didn't regress anything. One detail that's important is that regUnicode returns the longest matched substring, which is also the behavior of substring-trie (and there's a test to confirm this).

@akihikodaki akihikodaki added the performance Runtime performance label Jul 3, 2017
@unarist
Copy link
Contributor

unarist commented Jul 3, 2017

Looks good, and this also fixes #3732.

@Gargron Gargron merged commit e282580 into mastodon:master Jul 3, 2017
@charlag
Copy link

charlag commented Jul 24, 2017

Wow, that's impressive. Tries are really cool and I think it is a great use of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Runtime performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants