MetaMask / MetaMask/metamask-extension

[P1] Defer `TokenListController.initialize()` (backgroundConnect contention)

Open
#39,718 0 comments 0 reactions 0 assignees View on GitHub
area-performance INVALID-ISSUE-TEMPLATE team-extension-platform
Dominant language
TypeScript
Stars
13.2k
Forks
5.6k
Avg merge
2d 5h
Merged PRs (30d)
451

Description

## Summary

Defer `TokenListController.initialize()` to run after the critical startup path completes, reducing I/O contention during `backgroundConnect`.

**Parent Epic:** [#6669](https://github.com/MetaMask/MetaMask-planning/issues/6669) — Break Global Re-render Cascade

## Problem

Benchmarking revealed a **10% regression in backgroundConnect** (~12.6ms) caused by `TokenListController.initialize()` running synchronously during controller initialization. This call loads cached token lists from IndexedDB, competing with the critical startup handshake.

**Benchmark Results:**
| Metric | Main | Branch | Change |
|--------|------|--------|--------|
| backgroundConnect | 123.1ms | 135.7ms | +10.2% ⚠ |
| uiStartup | 812.7ms | 757.1ms | -6.8% ✓ |

## Current Code

```typescript
// app/scripts/controller-init/token-list-controller-init.ts
controller.initialize().catch((error: Error) => {
console.error(
'TokenListController: Failed to initialize from storage:',
error,
);
});
```

## Solution

Defer the initialization using `requestIdleCallback` (with `setTimeout` fallback):

```typescript
const deferredInit = () => {
controller.initialize().catch((error: Error) => {
console.error(
'TokenListController: Failed to initialize from storage:',
error,
);
});
};

// Use requestIdleCallback if available, otherwise setTimeout
if (typeof globalThis.requestIdleCallback === 'function') {
globalThis.requestIdleCallback(deferredInit, { timeout: 2000 });
} else {
setTimeout(deferredInit, 0);
}
```

## Files

- `app/scripts/controller-init/token-list-controller-init.ts`

## Acceptance Criteria

- [ ] Extension loads successfully
- [ ] Token list displays correctly after startup
- [ ] backgroundConnect regression eliminated or reduced
- [ ] No regression in existing tests
- [ ] Unit test verifies deferred initialization

## Estimated Effort

0.5 hours

## Labels

`area-performance`, `team-extension-platform`, `P1`

Contributor guide

Open the contributing guide

Research direction

Start with app/scripts/controller-init/token-list-controller-init.ts and inspect where TokenListController.initialize() runs during controller initialization. Add coverage for deferred initialization and run the existing test suite; done means startup still loads successfully, token lists display correctly, and backgroundConnect contention is reduced without test regressions.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
performance
Issue type
Refactor
Difficulty
2/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.