MetaMask / MetaMask/core

[utils] RegEx is slow (can we make faster hex functions?)

Open
#10,208 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
413
Forks
308
Avg merge
1d 4h
Merged PRs (30d)
253

Description

Noticed a recent attempt at speeding up some functions by switching to RegEx. But RegExes are usually slower than iterating over strings.

For example, this function:

https://github.com/MetaMask/utils/blob/1bea7f378c37117260912af1921556bc57110d20/src/hex.ts#L60

Is probably about 4-5x slower than something like this:

```
function isHexAddress(str) {
if (typeof str !== "string" || str.length !== 42) return false;
if (str[0] !== '0' || str[1] !== 'x') return false;
for (let i = 2; i < 42; i++) {
let code = str.charCodeAt(i);
// numbers and lowercase a - f
if (!((code >= 48 && code <= 57) || (code >= 97 && code <= 102))) {
return false;
}
}
return true;
}
```

(don't take my word for it. this should be tested further)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at src/hex.ts around the linked function and identify the regex-based hex helpers involved. Benchmark the current behavior against the proposed string iteration, then verify that valid and invalid inputs retain their existing results. Done means the chosen change has measured performance evidence and preserves the helpers’ behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
performance
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.