codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎨

HTML & CSS

35 / 59 topics
34HTML5 History API35HTML5 Web Workers36HTML5 Service Workers37HTML5 Fetch API38HTML5 WebSockets39HTML5 Geolocation API40HTML5 Device Orientation API41HTML5 Payment Request API42HTML5 Push Notifications
Tutorials/HTML & CSS/HTML5 Web Workers
🎨HTML & CSS

HTML5 Web Workers

Updated 2026-04-20
4 min read

Introduction to HTML5 Web Workers

HTML5 Web Workers are a powerful feature that allows you to run scripts in background threads, separate from the main execution thread of a web application. This enables developers to perform complex computations or handle time-consuming tasks without blocking the user interface, thus improving the overall performance and responsiveness of web applications.

In this tutorial, we will explore how to create and use Web Workers, understand their lifecycle, and discuss best practices for integrating them into your projects.

What are Web Workers?

Web Workers allow you to run JavaScript in background threads. Each worker runs in its own global context, separate from the main thread where the user interface is rendered. This separation ensures that long-running scripts do not block the UI thread, leading to a smoother and more responsive user experience.

Key Features of Web Workers

  • Concurrency: Web Workers enable true concurrency by running scripts in separate threads.
  • Isolation: Each worker has its own global context, which means it cannot directly access the DOM or modify the main thread's state.
  • Communication: Workers can communicate with the main thread via message passing using postMessage and onmessage events.

Creating a Web Worker

To create a Web Worker, you need to define a separate JavaScript file that will be executed in the background. This file should contain the code that you want to run asynchronously.

Example: Basic Web Worker

Let's start by creating a simple Web Worker that performs a basic computation.

  1. Create the Worker Script

    Create a new file named worker.js:

    // worker.js
    self.onmessage = function(event) {
      const data = event.data;
      console.log('Received data from main thread:', data);
    
      // Perform some computation
      const result = data * 2;
    
      // Send the result back to the main thread
      self.postMessage(result);
    };
    
  2. Create the Main Script

    In your main HTML file, create a script that initializes and communicates with the Web Worker:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Web Workers Example</title>
    </head>
    <body>
      <button id="startWorker">Start Worker</button>
      <p id="result"></p>
    
      <script>
        // Check if Web Workers are supported
        if (window.Worker) {
          const worker = new Worker('worker.js');
    
          document.getElementById('startWorker').addEventListener('click', () => {
            const dataToSend = 10;
            console.log('Sending data to worker:', dataToSend);
            worker.postMessage(dataToSend);
    
            worker.onmessage = function(event) {
              const result = event.data;
              console.log('Received result from worker:', result);
              document.getElementById('result').textContent = `Result: ${result}`;
            };
          });
        } else {
          console.error('Web Workers are not supported in this browser.');
        }
      </script>
    </body>
    </html>
    

Explanation

  • Worker Script (worker.js):

    • The worker listens for messages from the main thread using self.onmessage.
    • When a message is received, it performs a computation (in this case, multiplying the input by 2) and sends the result back to the main thread using self.postMessage.
  • Main Script:

    • The main script checks if Web Workers are supported in the browser.
    • It creates a new Worker instance by passing the path to the worker script (worker.js).
    • An event listener is added to a button, which sends data to the worker when clicked.
    • The main thread listens for messages from the worker using worker.onmessage and updates the UI with the received result.

Advanced Web Workers

Dedicated vs. Shared Workers

There are two types of Web Workers: dedicated workers and shared workers.

  • Dedicated Workers: These are exclusive to a single script that created them. They can only communicate with the main thread or other scripts that share the same origin.

  • Shared Workers: These can be accessed by multiple scripts, even if they are in different browsing contexts (e.g., different windows, tabs, or frames). Shared workers use a port object for communication.

Example: Using Shared Workers

Let's create a simple example of using a shared worker.

  1. Create the Shared Worker Script

    Create a new file named sharedWorker.js:

    // sharedWorker.js
    const connections = [];
    
    onconnect = function(event) {
      const port = event.ports[0];
      connections.push(port);
    
      port.onmessage = function(event) {
        const data = event.data;
        console.log('Received data from a client:', data);
    
        // Broadcast the message to all connected clients
        connections.forEach(client => {
          if (client !== port) {
            client.postMessage(data);
          }
        });
      };
    };
    
  2. Create the Main Script

    In your main HTML file, create scripts that connect to and communicate with the shared worker:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Shared Workers Example</title>
    </head>
    <body>
      <input type="text" id="messageInput">
      <button id="sendMessage">Send Message</button>
    
      <script>
        // Check if Shared Workers are supported
        if (window.SharedWorker) {
          const sharedWorker = new SharedWorker('sharedWorker.js');
    
          document.getElementById('sendMessage').addEventListener('click', () => {
            const message = document.getElementById('messageInput').value;
            console.log('Sending message to shared worker:', message);
            sharedWorker.port.postMessage(message);
    
            // Listen for messages from the shared worker
            sharedWorker.port.onmessage = function(event) {
              const receivedMessage = event.data;
              console.log('Received message from shared worker:', receivedMessage);
              alert(`Received: ${receivedMessage}`);
            };
          });
        } else {
          console.error('Shared Workers are not supported in this browser.');
        }
      </script>
    </body>
    </html>
    

Explanation

  • Shared Worker Script (sharedWorker.js):

    • The shared worker maintains a list of connected ports.
    • When a new connection is established, it adds the port to the connections array.
    • It listens for messages on each port and broadcasts them to all other connected clients.
  • Main Script:

    • The main script checks if Shared Workers are supported in the browser.
    • It creates a new SharedWorker instance by passing the path to the shared worker script (sharedWorker.js).
    • An event listener is added to a button, which sends messages to the shared worker when clicked.
    • The main thread listens for messages from the shared worker using sharedWorker.port.onmessage and displays them in an alert.

Best Practices

  1. Keep Workers Lightweight: Web Workers should not perform heavy DOM manipulations or access global variables directly. They are designed for computation-heavy tasks.

  2. Handle Errors Gracefully: Use try-catch blocks within your worker scripts to handle any exceptions that may occur during execution.

  3. Communicate Efficiently: Minimize the amount of data being sent between the main thread and workers. Large objects or complex data structures can lead to performance issues.

  4. Use Shared Workers Wisely: Shared Workers are powerful but should be used judiciously, especially in multi-origin scenarios where security is a concern.

  5. Test Across Browsers: Web Workers are widely supported, but always test your implementation across different browsers to ensure compatibility.

Conclusion

HTML5 Web Workers provide a robust mechanism for performing background tasks in web applications without blocking the user interface. By understanding how to create and manage dedicated and shared workers, you can enhance the performance and responsiveness of your web projects. Always consider the use case and requirements of your application when deciding whether to implement Web Workers.

In this tutorial, we covered the basics of creating and using Web Workers, explored their lifecycle, and discussed best practices for integrating them into your projects. With these tools at your disposal, you can build more efficient and interactive web applications.


PreviousHTML5 History APINext HTML5 Service Workers

Recommended Gear

HTML5 History APIHTML5 Service Workers