Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Performanceadvanced

Worker Threads

Share

Move heavy computation to Web Workers/Worker Threads

Works with OpenClaude

You 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 --version to 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 --prof to confirm main thread bottleneck
  • Check if your heavy function can run independently without constant event loop interaction

Steps

  1. Import Worker and isMainThread from the worker_threads module at the top of your file
  2. Identify the CPU-intensive function that blocks the event loop (e.g., large data processing, cryptographic operations, complex calculations)
  3. Create a separate .js file for the worker code that contains only the computational logic and parentPort message handlers
  4. In the main thread, instantiate a new Worker() with the path to your worker file
  5. Use worker.postMessage() to send data to the worker and worker.on('message', callback) to receive results
  6. Set up error handling with worker.on('error') and worker.on('exit') for proper cleanup
  7. Terminate the worker with worker.terminate() when computation is complete to free resources
  8. 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

Quick Info

CategoryPerformance
Difficultyadvanced
Version1.0.0
AuthorClaude Skills Hub
performanceworkersconcurrency

Install command:

curl -o ~/.claude/skills/worker-threads.md https://claude-skills-hub.vercel.app/skills/performance/worker-threads.md

Related Performance Skills

Other Claude Code skills in the same category — free to download.

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.