{ "id": "guide/web-worker", "title": "Background processing using web workers", "contents": "\n\n\n
Web workers allow you to run CPU-intensive computations in a background thread,\nfreeing the main thread to update the user interface.\nIf you find your application performs a lot of computations, such as generating CAD drawings or doing heavy geometrical calculations, using web workers can help increase your application's performance.
\nThe CLI does not support running Angular itself in a web worker.
\nTo add a web worker to an existing project, use the Angular CLI ng generate
command.
You can add a web worker anywhere in your application.\nFor example, to add a web worker to the root component, src/app/app.component.ts
, run the following command.
The command performs the following actions.
\nConfigures your project to use web workers, if it isn't already.
\nAdds the following scaffold code to src/app/app.worker.ts
to receive messages.
Adds the following scaffold code to src/app/app.component.ts
to use the worker.
After you generate this initial scaffold, you must refactor your code to use the web worker by sending messages to and from the worker.
\nSome environments or platforms, such as @angular/platform-server
used in Server-side Rendering, don't support web workers. To ensure that your application will work in these environments, you must provide a fallback mechanism to perform the computations that the worker would otherwise perform.