Commit 595ea63
authored
fix(perf): remove expensive computations from getMetaMaskAccounts (#38256)
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->
## **Description**
This selector depends on so much state that on re-renders we hardly hit
the memoization, and instead it ends up being recomputed multiple times.
That comes with a performance impact, as the isAddressCompatible
computations are quite expensive and we are running them multiple times
for each account (we are trying out every possible non evm chain until
we have a match. This times the total number of accounts (solana + btc +
tron).
This PR suggests another approach, where we can identify the account by
cross referencing its scope with the network providers chainId (as
suggested by @ccharly). On a local build the performance of the
application greatly improved (for example on of the long running tasks
on the popup initialisation went from 1.8s to take approx 180ms).
<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->
[](https://codespaces.new/MetaMask/metamask-extension/pull/38256?quickstart=1)
## **Changelog**
<!--
If this PR is not End-User-Facing and should not show up in the
CHANGELOG, you can choose to either:
1. Write `CHANGELOG entry: null`
2. Label with `no-changelog`
If this PR is End-User-Facing, please write a short User-Facing
description in the past tense like:
`CHANGELOG entry: Added a new tab for users to see their NFTs`
`CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker`
(This helps the Release Engineer do their job more quickly and
accurately)
-->
CHANGELOG entry: null
## **Related issues**
Fixes:
## **Screenshots/Recordings**
<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->
### **Before**
<img width="430" height="385" alt="Screenshot 2025-11-25 at 16 33 49"
src="https://github.com/user-attachments/assets/14ce7b27-78e6-4772-a9fd-cbafb4f02b10"
/>
<!-- [screenshots/recordings] -->
### **After**
<!-- [screenshots/recordings] -->
<img width="398" height="141" alt="Screenshot 2025-11-25 at 16 34 05"
src="https://github.com/user-attachments/assets/58bbc186-2483-40a9-b052-035a7ddddc28"
/>
## **Pre-merge author checklist**
- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
## **Pre-merge reviewer checklist**
- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Replaces multichain network detection in `getMetaMaskAccounts` with a
scope-based `chainId` match to avoid expensive address compatibility
checks.
>
> - **Selectors**:
> - `ui/selectors/selectors.js`:
> - Optimize non-EVM path in `getMetaMaskAccounts` by replacing
`network.isAddressCompatible(...)` lookup with `internalAccount.scopes`
includes `network.chainId`.
> - Keeps balance resolution logic, now keyed by the matched network’s
`chainId`.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
63ef130. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 87bbb8a commit 595ea63
1 file changed
+1
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
482 | 482 | | |
483 | 483 | | |
484 | 484 | | |
485 | | - | |
| 485 | + | |
486 | 486 | | |
487 | 487 | | |
488 | 488 | | |
| |||
0 commit comments