{"id":38940,"date":"2017-10-23T21:01:43","date_gmt":"2017-10-23T13:01:43","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=38940"},"modified":"2025-04-04T02:49:57","modified_gmt":"2025-04-03T18:49:57","slug":"web-workers-javascript-api","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/","title":{"rendered":"An Introduction to Web Workers JavaScript API"},"content":{"rendered":"<p><strong><a target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Web_Workers_API\" rel=\"noopener\">Web Workers<\/a><\/strong> is a JavaScript API that allows you to <strong>run scripts in a separate thread from the main one<\/strong>.  It can come in handy when you don\u2019t want any hindrance in the execution of the main scripts,  due to background-esque scripts.<\/p>\n<p>The Web Workers API is <strong>supported in almost all browsers<\/strong>, for more detailed information, have a look at the <a target=\"_blank\" href=\"https:\/\/caniuse.com\/webworkers\" rel=\"noopener\">CanIUse docs<\/a>. Before getting into the code, let\u2019s see a couple of scenarios where you might want to use this API so that you can get an idea of what I meant by <q>background-esque scripts<\/q>.<\/p>\n<p class=\"note\"><strong>Read Also:<\/strong> <a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/synchronous-asynchronous-javascript\/\" rel=\"noopener\">Understanding Synchronous and Asynchronous JavaScript<\/a><\/p>\n<h2>Use cases<\/h2>\n<p>Let\u2019s say there\u2019s a script that <strong>fetches and processes a file<\/strong>. If a file is <em>considerably<\/em> large it\u2019ll take a long time to be processed! Which might stall other scripts that were invoked later from getting executed.<\/p>\n<p>However, if the <strong>file processing is moved to a background thread<\/strong>, known as the <strong>worker thread<\/strong>, other events won\u2019t be blocked until the former one is over.<\/p>\n<p>The script <strong>executed in a background worker thread<\/strong> is known as the <strong>worker script<\/strong> or just the <strong>worker<\/strong>.<\/p>\n<p>For another example, imagine there is a <strong>big form, arranged in tabs<\/strong>. It\u2019s scripted in a way that updating controls in one tab <strong>affects some of the controls in others<\/strong>.<\/p>\n<p>If the update of the other tabs takes some time the user <strong>can\u2019t continuously use the current tab<\/strong> without its events being put on hold. This might freeze the UI, to user\u2019s dismay.<\/p>\n<p>Since a user won\u2019t be seeing the other tabs while filling out a current one, you can <strong>update the controls of the other tabs in a background thread<\/strong>. This way, the user can continue using the current tab he is filling out, without any of its scripts being blocked by the update process of controls in other tabs.<\/p>\n<p>Likewise, if you find a scenario where a script <strong>might block a user from using the user interface<\/strong> until its execution is done, you might consider moving it to a worker thread, so that it could be executed in the background.<\/p>\n<h2>Scopes and types of workers<\/h2>\n<p>The Web Workers API is probably one of the simplest APIs to work with. It has pretty straightforward methods to <strong>create worker threads<\/strong> and <strong>communicate with them from the main script<\/strong>.<\/p>\n<p>The global scope of a worker thread is in a different context from the main thread. You <strong>can\u2019t access the methods and properties of <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\" target=\"_blank\" rel=\"noopener\"><code>window<\/code><\/a> object<\/strong> such as <code>alert()<\/code> inside a worker thread. You also <strong>can\u2019t change the DOM directly<\/strong> from a worker thread.<\/p>\n<p>However, you <strong><em>can<\/em> use many APIs that come under <code>window<\/code><\/strong>, for instance <code>Promise<\/code> and <code>Fetch<\/code>, in your worker thread (see the <a target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Web_Workers_API\/Functions_and_classes_available_to_workers\" rel=\"noopener\">full list<\/a>).<\/p>\n<p>You can also have <strong>nested worker threads<\/strong>: worker threads created from another worker thread. A worker created by another one is called a <strong>subworker<\/strong>.<\/p>\n<p>There are also <strong>many <em>types<\/em> of workers<\/strong>. The two main ones are <strong>dedicated and shared workers<\/strong>.<\/p>\n<p>Dedicated workers <strong>belong to the same browsing context<\/strong> that their main thread belongs to. Shared workers, however, are <strong>present in a different browsing context<\/strong> (for instance, in an iframe) from the main script. In both cases, the main script and the workers <strong>have to be in the same domain<\/strong>.<\/p>\n<p>The example in this tutorial will be <strong>about dedicated worker<\/strong>, which is the most common type.<\/p>\n<h2>API methods<\/h2>\n<p>See the below diagram for a <strong>quick overview of all the main methods<\/strong> that constitute the Web Workers API.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/web-workers-javascript-api\/web-workers.jpg\" alt=\"Methods of the Web Worker API\" width=\"1000\" height=\"473\"><\/figure>\n<p>The <strong><a target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Worker\/Worker\" rel=\"noopener\"><code>Worker()<\/code><\/a><\/strong> constructor <strong>creates a dedicated worker thread<\/strong> and <strong>returns its reference object<\/strong>. Then, we use this object to communicate with that specific worker.<\/p>\n<p>The <strong><a target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Worker\/postMessage\" rel=\"noopener\"><code>postMessage()<\/code><\/a><\/strong> method is used in both the main and worker scripts to <strong>send data to each other<\/strong>. The sent data is then received on the other side by the <strong><a target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Worker\/onmessage\" rel=\"noopener\"><code>onmessage<\/code><\/a><\/strong> event handler.<\/p>\n<p>The <strong><a target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Worker\/terminate\" rel=\"noopener\"><code>terminate()<\/code><\/a><\/strong> method <strong>terminates a worker thread from the main script<\/strong>. This termination is <strong>immediate<\/strong>: any current script execution and pending scripts  will be cancelled. The <strong><a target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/WorkerGlobalScope\" rel=\"noopener\"><code>close()<\/code><\/a><\/strong> method does the same thing, but it\u2019s <strong>called by the worker thread closing itself<\/strong>.<\/p>\n<h2>Example code<\/h2>\n<p>Now, let\u2019s see some sample code. The <code>index.html<\/code> page holds the <strong>main script<\/strong> inside a <code>&lt;script&gt;<\/code> tag, while the <strong>worker script<\/strong> is held in a JavaScript file called <code>worker.js<\/code>.<\/p>\n<pre>\r\n&lt;button&gt;Click me&lt;\/button&gt;\r\n&lt;script&gt;\r\n&lt;!-- main script goes here --&gt;\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>We start with the <strong>creation of the worker thread from the main script<\/strong>.<\/p>\n<pre>\r\nw = new Worker('worker.js');\r\n<\/pre>\n<p>The <code>Worker()<\/code> constructor <strong>takes the URL of the worker file as its argument<\/strong>.<\/p>\n<p>Then, we add an event handler for the <code>onmessage<\/code> event of the newly created worker instance to <strong>receive data from it<\/strong>. The <code>data<\/code> property of the <code>e<\/code> event will hold the received data.<\/p>\n<pre>\r\nw = new Worker('worker.js');\r\nw.onmessage = (e)=&gt;{\r\n  console.log(`Received from worker: ${e.data}`);\r\n}\r\n<\/pre>\n<p>Now, we use <code>postMessage()<\/code> to <strong>send some data to the worker thread<\/strong> on the click of a button. The <code>postMessage()<\/code> method can take two arguments. The first can be of any type (string, array\u2026). It\u2019s the data <strong>to be sent to the worker thread<\/strong> (or to the main script, when the method is present in the worker thread).<\/p>\n<p>The second, optional parameter is an array of objects that <strong>can be used by the worker threads<\/strong> (but not by the main script, or vice-versa). These kinds of objects are called <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Transferable\" target=\"_blank\" rel=\"noopener nofollow\"><code>Transferable<\/code><\/a> objects.<\/p>\n<pre>\r\ndocument.querySelector('button').onclick = ()=&gt;{\r\n  w.postMessage('john');\r\n}\r\n<\/pre>\n<p>I\u2019m just sending a string value to the worker thread.<\/p>\n<p>In the worker thread, we need to add an <code>onmessage<\/code> event handler that <strong>will receive the data<\/strong> sent to it by the main script on button click. Inside the handler, we <strong>concatenate the received string with another<\/strong> and send the result back to the main script.<\/p>\n<pre>\r\nconsole.info('worker created');\r\nonmessage = (e)=&gt;{\r\n  postMessage(`Hi ${e.data}`);\r\n}\r\n<\/pre>\n<p>Unlike in the main script where we had to use the <code>w<\/code> reference object to <strong>refer to the specific worker thread<\/strong> on which the script then uses the <code>onmessage<\/code> and <code>postMessage<\/code> methods, there\u2019s <strong>no need for a reference object in the worker thread<\/strong> to point to the main thread.<\/p>\n<p>The code works as follows. When the browser loads <code>index.html<\/code>, the console will show the <code>\"worker created\"<\/code> message as soon as the <strong><code>worker()<\/code> constructor is executed in the main thread<\/strong>, creating a new worker.<\/p>\n<p>When you click the button on the page, you\u2019ll get the <code>\"Received from worker: Hi john\"<\/code> message in the console, which is the string that was <strong>concatenated in the worker thread<\/strong> with the data sent to it, and then was <strong>sent back to the main script<\/strong>.<\/p>\n<p class=\"note\"><strong>Read Also:<\/strong> <a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/javascript-promise-object\/\" rel=\"noopener\">Getting Started with JavaScript Promises<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Web Workers is a JavaScript API that allows you to run scripts in a separate thread from the main one. It can come in handy when you don\u2019t want any hindrance in the execution of the main scripts, due to background-esque scripts. The Web Workers API is supported in almost all browsers, for more detailed&hellip;<\/p>\n","protected":false},"author":145,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3392],"tags":[3497,4117,511],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>An Introduction to Web Workers JavaScript API - Hongkiat<\/title>\n<meta name=\"description\" content=\"Web Workers is a JavaScript API that allows you to run scripts in a separate thread from the main one. It can come in handy when you don&#039;t want any\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"An Introduction to Web Workers JavaScript API\" \/>\n<meta property=\"og:description\" content=\"Web Workers is a JavaScript API that allows you to run scripts in a separate thread from the main one. It can come in handy when you don&#039;t want any\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2017-10-23T13:01:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-03T18:49:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/assets.hongkiat.com\/uploads\/web-workers-javascript-api\/web-workers.jpg\" \/>\n<meta name=\"author\" content=\"Preethi Ranjit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Preethi Ranjit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/web-workers-javascript-api\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/web-workers-javascript-api\\\/\"},\"author\":{\"name\":\"Preethi Ranjit\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\"},\"headline\":\"An Introduction to Web Workers JavaScript API\",\"datePublished\":\"2017-10-23T13:01:43+00:00\",\"dateModified\":\"2025-04-03T18:49:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/web-workers-javascript-api\\\/\"},\"wordCount\":1024,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/web-workers-javascript-api\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/web-workers-javascript-api\\\/web-workers.jpg\",\"keywords\":[\"Javascript Library\",\"Javascripts\",\"Web Developers\"],\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/web-workers-javascript-api\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/web-workers-javascript-api\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/web-workers-javascript-api\\\/\",\"name\":\"An Introduction to Web Workers JavaScript API - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/web-workers-javascript-api\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/web-workers-javascript-api\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/web-workers-javascript-api\\\/web-workers.jpg\",\"datePublished\":\"2017-10-23T13:01:43+00:00\",\"dateModified\":\"2025-04-03T18:49:57+00:00\",\"description\":\"Web Workers is a JavaScript API that allows you to run scripts in a separate thread from the main one. It can come in handy when you don't want any\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/web-workers-javascript-api\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/web-workers-javascript-api\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/web-workers-javascript-api\\\/#primaryimage\",\"url\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/web-workers-javascript-api\\\/web-workers.jpg\",\"contentUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/web-workers-javascript-api\\\/web-workers.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/web-workers-javascript-api\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"An Introduction to Web Workers JavaScript API\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\",\"name\":\"Preethi Ranjit\",\"description\":\"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/preethi\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"An Introduction to Web Workers JavaScript API - Hongkiat","description":"Web Workers is a JavaScript API that allows you to run scripts in a separate thread from the main one. It can come in handy when you don't want any","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/","og_locale":"en_US","og_type":"article","og_title":"An Introduction to Web Workers JavaScript API","og_description":"Web Workers is a JavaScript API that allows you to run scripts in a separate thread from the main one. It can come in handy when you don't want any","og_url":"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2017-10-23T13:01:43+00:00","article_modified_time":"2025-04-03T18:49:57+00:00","og_image":[{"url":"https:\/\/assets.hongkiat.com\/uploads\/web-workers-javascript-api\/web-workers.jpg","type":"","width":"","height":""}],"author":"Preethi Ranjit","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Preethi Ranjit","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/"},"author":{"name":"Preethi Ranjit","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3"},"headline":"An Introduction to Web Workers JavaScript API","datePublished":"2017-10-23T13:01:43+00:00","dateModified":"2025-04-03T18:49:57+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/"},"wordCount":1024,"commentCount":0,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/web-workers-javascript-api\/web-workers.jpg","keywords":["Javascript Library","Javascripts","Web Developers"],"articleSection":["Coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/","url":"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/","name":"An Introduction to Web Workers JavaScript API - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/#primaryimage"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/web-workers-javascript-api\/web-workers.jpg","datePublished":"2017-10-23T13:01:43+00:00","dateModified":"2025-04-03T18:49:57+00:00","description":"Web Workers is a JavaScript API that allows you to run scripts in a separate thread from the main one. It can come in handy when you don't want any","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/#primaryimage","url":"https:\/\/assets.hongkiat.com\/uploads\/web-workers-javascript-api\/web-workers.jpg","contentUrl":"https:\/\/assets.hongkiat.com\/uploads\/web-workers-javascript-api\/web-workers.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/web-workers-javascript-api\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"An Introduction to Web Workers JavaScript API"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3","name":"Preethi Ranjit","description":"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.","url":"https:\/\/www.hongkiat.com\/blog\/author\/preethi\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-a84","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/38940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=38940"}],"version-history":[{"count":2,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/38940\/revisions"}],"predecessor-version":[{"id":73737,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/38940\/revisions\/73737"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=38940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=38940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=38940"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=38940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}