Sitemap
JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

🧠 How Web Workers Saved My JavaScript App from Freezing on Every CSV Upload

3 min readMay 13, 2025

--

Ever uploaded a file and watched your app turn into a brick?

💡 Where It Went Wrong

⚙️ What Was Happening Behind the Scenes

const reader = new FileReader();
reader.onload = (e) => {
const text = e.target.result;
const rows = parseCSV(text); // heavy loop!
const stats = generateStats(rows); // CPU-heavy
renderDashboard(stats); // React state update
};
reader.readAsText(file);

🔥 Enter Web Workers

🛠️ Step-by-Step: How I Refactored with Web Workers

📁 Create a worker file

// csvWorker.js
self.onmessage = function (event) {
const csvText = event.data;
const rows = parseCSV(csvText); // custom parser
const stats = generateStats(rows); // summary metrics
self.postMessage(stats);
};
// inside App.jsx
const worker = new Worker(new URL('./csvWorker.js', import.meta.url), { type: 'module' });

worker.postMessage(csvText);

worker.onmessage = (e) => {
const stats = e.data;
setDashboardData(stats); // back on the main thread
};

🎉 The Results

🧠 Lessons I Learned

🔚 Final Thoughts

🙌 Enjoyed the post?

Thank you for being a part of the community

--

--

JavaScript in Plain English
JavaScript in Plain English

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Sachin Kasana
Sachin Kasana

Written by Sachin Kasana

Principal Engineer & Architect | Node.js, React, AWS I build tools, write about clean code, and simplify system design. 🌐Portfolio: sachinkasana-dev.vercel.app

No responses yet