Podcast
Questions and Answers
What is a primary use case for Worker threads in Node.js?
What is a primary use case for Worker threads in Node.js?
How do Worker threads in Node.js share memory?
How do Worker threads in Node.js share memory?
What should be used to inform diagnostic tools about the correlation between tasks and their outcomes when implementing a worker pool?
What should be used to inform diagnostic tools about the correlation between tasks and their outcomes when implementing a worker pool?
What type of options do Worker threads inherit by default?
What type of options do Worker threads inherit by default?
Signup and view all the answers
What happens if an object marked as untransferable occurs in the transfer list of a port.postMessage() call?
What happens if an object marked as untransferable occurs in the transfer list of a port.postMessage() call?
Signup and view all the answers
Which event is emitted for any incoming message?
Which event is emitted for any incoming message?
Signup and view all the answers
What happens when port.close() is called?
What happens when port.close() is called?
Signup and view all the answers
In which version did the MessagePort class start inheriting from EventTarget?
In which version did the MessagePort class start inheriting from EventTarget?
Signup and view all the answers
What is emitted when deserializing a message fails?
What is emitted when deserializing a message fails?
Signup and view all the answers
Which of the following was added to the list of cloneable types in v15.6.0?
Which of the following was added to the list of cloneable types in v15.6.0?
Signup and view all the answers
Study Notes
Node:worker_threads Module
- Enables the use of threads that execute JavaScript in parallel for CPU-intensive operations.
- Does not help with I/O-intensive work, as Node.js built-in asynchronous I/O operations are more efficient.
- Allows threads to share memory by transferring ArrayBuffer instances or sharing SharedArrayBuffer instances.
Worker Threads
- Useful for performing CPU-intensive JavaScript operations.
- Can be spawned for each task, but a pool of Workers is recommended to avoid overhead.
- Inherit non-process-specific options by default, but can be customized using Worker constructor options.
Worker Thread Pool
- Use the AsyncResource API to inform diagnostic tools about the correlation between tasks and their outcomes.
- Implementing a worker pool provides asynchronous stack traces.
markAsUntransferable() Method
- Marks an object as not transferable, throwing an error if it's in the transfer list of a port.postMessage() call.
- No-op if the object is a primitive value.
- Use for objects that can be cloned, rather than transferred, and are used by other objects on the sending side.
MessagePort Class
- Represents one end of an asynchronous, two-way communications channel.
- Can be used to transfer structured data, memory regions, and other MessagePorts between different Workers.
- Inherits from EventTarget rather than EventType (as of v14.7.0).
MessagePort Events
'close' Event
- Emitted once either side of the channel has been disconnected.
- Fired when a port is closed.
'message' Event
- Emitted for any incoming message, containing the cloned input of port.postMessage().
- Listeners receive a clone of the value parameter as passed to postMessage().
'messageerror' Event
- Emitted when deserializing a message failed.
- Usually occurs when there's an error instantiating the posted JS object on the receiving end.
port.close() Method
- Disables further sending of messages on either side of the connection.
- Emits the 'close' event on both MessagePort instances that are part of the channel.
port.postMessage() Method
- Sends a message with an optional transfer list.
- Throws an error if an untransferable object is in the transfer list (as of v21.0.0).
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Quiz about the Node.js worker_threads module, its features, and usage for parallel JavaScript execution.