Mutlithreading in JavaScript
Last Updated :
28 Apr, 2025
Multithreading is the ability of any program to execute multiple threads simultaneously. As we know JavaScript is a single-threaded programming language, which means it has a single thread that handles all the execution sequentially. Single-threaded means one line of code run at once. Originally, Javascript is single-threaded because it was just used in web browser scripting language for a single user but nowadays it evolve into something bigger and make the computation very huge.
There are various techniques to stimulate multithreading in JavaScript:
Web Workers
Web Workers are giving us the possibility to write multi-threaded JavaScript, which does not block the DOM. Even the Asynchronous operations block the DOM to some extent. On the other side, web workers help us to solve this problem, escaping the single-threaded environment and reaching a higher performance of our web pages. Basically, web workers run the script in the background thread and which avoids the interference in user interface. Due to the feature of a background thread, we can create or run a costly operation.
Example 1: In this example, we will show what is the difference in the behavior of our page with and without workers.
JavaScript
// Creating new web worker
const worker = new Worker('worker.js');
let message = 'Hello';
// Message using postMessage
worker.postMessage(message);
// Response
worker.onmessage = function (e) {
console.log(e.data);
};
JavaScript
self.onmessage = function (e) {
if (e.data !== undefined) {
let total = e.data + 'Geeks';
self.postMessage(total)
}
}
Output:
'Hello Geeks'
Explanation: In this example, we have created two JavaScript files: main.js and worker.js. main.js represent the main file and worker.js represent the worker file. In the example above, the worker is doing the work of concatenating the received string with the defined one and sending it back to the main.js file without interrupting the page.
Asynchronous calls the next statement gets executed without even waiting for the previous one’s execution. JavaScript provides us with callbacks, promises, and async/await for executing a non-blocking function. Through Asynchronous programming, we achieve a parallel-like behavior by initiating multithreading and also handling its completion.
Example 2: In this example, we are using the setTimeout() function for Asynchronous Programme to illustrate Multithreading in Javascript.
JavaScript
console.log("Starting...");
setTimeout(function () {
console.log("Welcome to geeksforgeeks");
}, 2000);
console.log("Ending...");
Output: The above example shows that when we use an asynchronous program, the code doesn't block in fact it executes the next line and the function executes after a specified time.
Starting...
Ending...
Welcome to geeksforgeeks
Similar Reads
Why doesn't JavaScript support multithreading? What is Multithreading? Multithreading is a sort of execution model that enables different threads to exist inside the setting of a process with the end goal that they execute autonomously yet share their process resources. A thread keeps up a rundown of data important to its execution including the
2 min read
Read JSON File Using JavaScript JSON (JavaScript Object Notation) is a lightweight format used for storing and exchanging data. In JavaScript, there are multiple ways to read and parse JSON files. These methods can be used both in browser environments and in Node.js.1. Using the fetch() API The fetch() API retrieves JSON files asy
3 min read
JavaScript String Exercise JavaScript string is a primitive data type and is used for storing and manipulating a sequence of characters. It can contain zero or more characters within single or double quotes. This article contains a wide collection of JavaScript Programming examples based on String. JavaScriptlet s = "Geeksfor
7 min read
What is JavaScript? JavaScript is a powerful and flexible programming language for the web that is widely used to make websites interactive and dynamic. JavaScript can also able to change or update HTML and CSS dynamically. JavaScript can also run on servers using tools like Node.js, allowing developers to build entire
6 min read
Getting Started with JavaScript JavaScript is a lightweight, single-threaded, dynamically typed, interpreted programming language with object-oriented capabilities. Primarily JavaScript is used to add interactivity and dynamism to web pages. It breathes life into static HTML and CSS, enabling features like dynamic content updates,
8 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read