Questions tagged [web-worker]

Questions with this tag should be about the "Web Workers" technology. Workers provide a simple means for web content to run scripts in background threads. Once created, a worker can send messages to the spawning task by posting messages to an event handler specified by the creator.

Web Workers are external JavaScript scripts which run in the background of a browser, useful for performing client-side CPU intensive tasks without disrupting the foreground tasks.

Only a subset of the JavaScript API is available in these scripts. Workers can include other scripts using the importScripts() method.

Web workers are a WHATWG specification and are available in most browsers.

Support can be checked looking for a window.Worker property:

function checkSupportForWebWorkers() {
  return typeof window.Worker === "function";
}

References

2223 questions
355
votes
31 answers

Web workers without a separate Javascript file?

As far as I can tell, web workers need to be written in a separate JavaScript file, and called like this: new Worker('longrunning.js') I'm using the closure compiler to combine and minify all my JavaScript source code, and I'd rather not have to…
Ben Dilts
  • 10,535
  • 16
  • 54
  • 85
236
votes
5 answers

Node.js and CPU intensive requests

I've started tinkering with Node.js HTTP server and really like to write server side Javascript but something is keeping me from starting to use Node.js for my web application. I understand the whole async I/O concept but I'm somewhat concerned…
Olivier Lalonde
  • 19,423
  • 28
  • 76
  • 91
212
votes
3 answers

What can service workers do that web workers cannot?

What can service workers do that web workers cannot? Or vice versa? It seems that web workers are a subset of the functionality of service workers. Is this correct?
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
190
votes
3 answers

What are the use-cases for Web Workers?

I am looking for real-world scenarious for using Web Workers API.
Sergey Ilinsky
  • 31,255
  • 9
  • 54
  • 56
145
votes
16 answers

Chrome can't load web worker

I am working on a project that uses a web worker. In my head section I have this code: var worker = new Worker("worker.js"); // More code This works fine in Safari, but Chrome reports the following error: Uncaught SecurityError: Failed to create a…
Progo
  • 3,452
  • 5
  • 27
  • 44
114
votes
7 answers

Which would be better for concurrent tasks on node.js? Fibers? Web-workers? or Threads?

I stumbled over node.js sometime ago and like it a lot. But soon I found out that it lacked badly the ability to perform CPU-intensive tasks. So, I started googling and got these answers to solve the problem: Fibers, Webworkers and Threads…
Parth Thakkar
  • 5,427
  • 3
  • 25
  • 34
102
votes
2 answers

How to Add a new native class to WebWorker's context in JavaScriptCore?

I have an application that extends JavaScript via JavaScriptCore, in a webkit-gtk browser. Right now I have several classes that I add to the global context like so: void create_js(gpointer context, char* className, JSClassDefinition clasDefinition)…
102
votes
12 answers

How to create a Web Worker from a string

How can I use create a Web worker from a string (which is supplied via a POST request)? One way I can think of, but I'm not sure how to implement it, is by creating a data-URI from the server response, and passing that to the Worker constructor, but…
bigblind
  • 12,539
  • 14
  • 68
  • 123
85
votes
1 answer

WebWorker calculates slow regexp matches significantly slower (3x) - firefox only

First I just created myself a regular expression that will match all unique external library paths in a list of all header files in a project. I asked a question regarding making that regexp a week ago. I started meddling around to see how it would…
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
83
votes
7 answers

Since JavaScript is single-threaded, how are web workers in HTML5 doing multi-threading?

I've been reading about web workers in HTML5, but I know JavaScript is single-threaded. How are web workers doing multi-threaded work then? or how are they simulating it if it's not truly multi-threaded?
James Drinkard
  • 15,342
  • 16
  • 114
  • 137
78
votes
5 answers

Get number of CPU cores in JavaScript?

Is there a way to determine the number of available CPU cores in JavaScript, so that you could adjust the number of web workers depending on that?
tsauerwein
  • 5,841
  • 3
  • 36
  • 49
77
votes
2 answers

Making WebWorkers a safe environment

In a quest to have an interface capable of running arbitrary javascript code inside the browser, without having a security hole the size of a typical yo-mama joke, Esailija proposed using Web Workers. They run in a semi-sandboxed environment (no DOM…
Zirak
  • 38,920
  • 13
  • 81
  • 92
75
votes
5 answers

AngularJS and web workers

How can angularJS use web workers to run processes in the background? Is there any pattern I should follow on doing this? Currently, I am using a service that has the model in a separate web worker. This service implements methods…
arg20
  • 4,893
  • 1
  • 50
  • 74
73
votes
5 answers

Web Workers - How To Import Modules

I am using ES2015 Import / Export modules. In my worker file, when I try to import functions like I normally do: worker.js import { a, b, c } from "./abc.js"; I get the error: SyntaxError: import declarations may only appear at top level of a…
Kayote
  • 14,579
  • 25
  • 85
  • 144
72
votes
5 answers

Accessing localStorage from a webWorker

Can a WebWorker access the localStorage? If not why not? Is it problematic from a security stand point?
ciochPep
  • 2,472
  • 4
  • 26
  • 30
1
2 3
99 100