Overview
WebWorker is a separate process that hosts JavaScript workers, providing:- Concurrent execution: Run scripts in parallel with the main thread
- Isolation: Workers run in their own global scope
- Message passing: Communication via structured clone algorithm
- Standards compliance: Implements the WHATWG Web Workers specification
Architecture
Process model
WebWorker runs as a separate service process, similar to WebContent:Key components
WorkerHost
Manages the lifecycle of individual workers:- Script loading and execution
- Global scope management
- Message queue handling
- Error reporting
Services/WebWorker/WorkerHost.cpp
PageHost
Coordinates between the main page and workers:- Worker creation and termination
- Message routing
- Resource management
Services/WebWorker/PageHost.cpp
ConnectionFromClient
Handles IPC communication with the browser:- Worker lifecycle commands
- Message passing between threads
- Event dispatching
Services/WebWorker/ConnectionFromClient.cpp
Worker types
Dedicated Workers
Workers tied to a single page:Shared Workers
Workers accessible from multiple browsing contexts:Worker features
Global scope
Workers have access to:self- Worker global scopepostMessage()- Send messages to parentimportScripts()- Load additional scriptssetTimeout(),setInterval()- Timersfetch()- Network requestsXMLHttpRequest- HTTP requests
Restrictions
Workers cannot access:- DOM elements
windowobjectdocumentobjectparentobject
Communication
Message passing
Structured clone algorithm for data transfer:Transferable objects
Transfer ownership of objects for zero-copy performance:Error handling
Worker errors
Errors in workers are reported to the main thread:Unhandled rejections
Lifecycle
Worker creation
- Browser creates worker request
- WebWorker process spawned (if not running)
- Worker script loaded and parsed
- Global scope initialized
- Script executed
Worker termination
Performance considerations
- Workers have startup overhead (script loading, parsing)
- Message passing involves serialization/deserialization
- Use transferable objects for large data
- Consider worker pools for frequent operations
Implementation status
WebWorker support is under active development. Some advanced features may not be fully implemented.
Workers run in a separate process for security and stability, similar to WebContent processes.
See also
- WebContent service - Main rendering process
- LibJS - JavaScript engine
- Process architecture - Multi-process model