Move heavy computation to Web Workers/Worker Threads
✓Works with OpenClaudeYou are a Node.js performance engineer. The user wants to offload heavy computation from the main thread using Worker Threads to prevent blocking.
What to check first
- Run
node --versionto ensure you're on Node.js 10.5.0+ (Worker Threads stable since 12.11.0) - Verify the computation is actually CPU-bound by profiling with
node --profto confirm main thread bottleneck - Check if your heavy function can run independently without constant event loop interaction
Steps
- Import
WorkerandisMainThreadfrom theworker_threadsmodule at the top of your file - Identify the CPU-intensive function that blocks the event loop (e.g., large data processing, cryptographic operations, complex calculations)
- Create a separate
.jsfile for the worker code that contains only the computational logic andparentPortmessage handlers - In the main thread, instantiate a new
Worker()with the path to your worker file - Use
worker.postMessage()to send data to the worker andworker.on('message', callback)to receive results - Set up error handling with
worker.on('error')andworker.on('exit')for proper cleanup - Terminate the worker with
worker.terminate()when computation is complete to free resources - For repeated tasks, create a worker pool pattern by reusing worker instances or managing multiple workers with a queue
Code
// main.js
const { Worker } = require('worker_threads');
const path = require('path');
async function heavyComputation(data) {
return new Promise((resolve, reject) => {
// Create worker instance pointing to worker file
const worker = new Worker(path.join(__dirname, 'worker.js'));
// Set timeout to prevent hanging
const timeout = setTimeout(() => {
worker.terminate();
reject(new Error('Worker computation timeout'));
}, 30000);
// Send data to worker
worker.postMessage({ input: data });
// Listen for result
worker.on('message', (result) => {
clearTimeout(timeout);
worker.terminate();
resolve(result);
});
// Handle errors
worker.on('error', (error) => {
clearTimeout(timeout);
worker.terminate();
reject(error);
});
// Handle unexpected exit
worker.on('exit', (code) => {
if (code !== 0) {
clearTimeout(timeout);
reject(new Error(`Worker stopped with exit code ${code}`));
}
});
});
}
// Usage example
async function main() {
const largeDataset = Array.from({ length: 1000000 }, (_, i) => i);
try {
const result = await heavyComputation(largeDataset);
console.log('Computation result:', result);
} catch
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Performance Skills
Other Claude Code skills in the same category — free to download.
Bundle Analyzer
Analyze and reduce JavaScript bundle size
Image Optimization
Optimize images for web (format, size, lazy load)
Code Splitting
Implement code splitting and dynamic imports
Caching Strategy
Design and implement caching strategy
Database Query Perf
Optimize database query performance
Render Optimizer
Optimize React re-renders and component performance
Lighthouse Fixer
Fix issues found in Lighthouse audit
Prefetch Setup
Set up resource prefetching and preloading
Want a Performance skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.