{ "id": "guide/web-worker", "title": "Background processing using web workers", "contents": "\n\n\n
\n mode_edit\n
\n\n\n
\n

Background processing using web workerslink

\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.

\n
\n

The CLI does not support running Angular itself in a web worker.

\n
\n

Adding a web workerlink

\n

To add a web worker to an existing project, use the Angular CLI ng generate command.

\n\nng generate web-worker <location>\n\n

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.

\n\nng generate web-worker app\n\n

The command performs the following actions.

\n\n

After you generate this initial scaffold, you must refactor your code to use the web worker by sending messages to and from the worker.

\n
\n

Some 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.

\n
\n\n \n
\n\n\n" }