3 JavaScript Performance Mistakes That Cost You Users (And How to Fix Them)

2026-11-29来源 dev.toEnglish

3 JavaScript Performance Mistakes That Cost You Users (And How to Fix Them)

Your app works on your MacBook Pro with 32GB RAM and fiber internet. But for a user on a $120 Android phone with 2GB RAM and spotty 3G, it is unusable.

Performance is not a luxury. It is accessibility. Here are three mistakes I see in almost every codebase I audit, and the exact fixes that work.

Mistake 1: Loading Everything Up Front

// What most apps do import { Chart, Map, Editor, VideoPlayer } from './heavy-components'; 

Even if only 10% of users open the dashboard with the chart, everyone pays the download and parse cost.

The fix: Route-level code splitting

import { lazy, Suspense } from 'react'; const Chart = lazy(() => import('./Chart')); const Map = lazy(() => import('./Map')); function Dashboard() { return ( <Suspense fallback={<Spinner />}> <Chart /> </Suspense> ); } 

Split at the route level, not the component level. Too many small chunks create network waterfalls. Aim for 3–5 chunks per route.

Mistake 2: Trusting Your Build Output

Vite reports "184KB." You celebrate. But the real transfer size is 1.2MB because:

  • You imported all of lodash instead of lodash/debounce
  • A transitive dependency pulled in 3 versions of tslib
  • An icon library shipped 4,000 SVGs you do not use

The fix: Audit before you optimize

# See what is actually in your bundle npx vite-bundle-visualizer # Find duplicate dependencies npm ls some-library npm dedupe 

Replace barrel imports with direct paths:

// Bad import { Button, Modal } from 'huge-ui-lib'; // Good import Button from 'huge-ui-lib/Button'; import Modal from 'huge-ui-lib/Modal'; 

One audit I ran found moment (65KB), dayjs (8KB), and date-fns (12KB) all in the same app. Pick one date library. Delete the others.

Mistake 3: Blocking the Main Thread

Heavy computation freezes the UI. Users cannot scroll, click, or type while JavaScript runs.

// This blocks for 2+ seconds on large datasets const result = massiveArray.map(transform).filter(check).sort(compare); 

The fix: Yield to the browser

For data processing, use requestIdleCallback or chunk the work:

async function processInChunks(items, chunkSize = 100) { const results = []; for (let i = 0; i < items.length; i += chunkSize) { const chunk = items.slice(i, i + chunkSize); results.push(...chunk.map(transform)); // Yield control back to the browser await new Promise(resolve => setTimeout(resolve, 0)); } return results; } 

For truly heavy work, use Web Workers. They run on a separate thread and do not block the UI.

The 80/20 of Frontend Performance

You do not need a perfect Lighthouse score. You need a usable experience for real users on real devices.

  1. Split your code at the route level
  2. Audit your bundle and remove dead weight
  3. Yield the main thread during heavy work

Do these three things, and you will outperform 90% of web apps without touching a caching strategy or CDN config.

What performance mistake have you fixed that had the biggest impact? Drop it in the comments.

原文:https://www.a8q9.cn/en/article/3-javascript-performance-mistakes-that-cost-you-users-and-how-to-fix-them-8f9a35.html

相关文章