0%

Traditionally, JavaScript has been a single-threaded language. Nearly all online forum posts, books, online documentation, and libraries refer to the language as single threaded. Thanks to recent advancements in the language--such as the Atomics and SharedArrayBuffers objects and Web Workers in the browser--JavaScript is now a multi-threaded language. These features will go down as being the biggest paradigm shift for the world's most popular programming language.

Multithreaded JavaScript explores the various features that JavaScript runtimes have at their disposal for implementing multithreaded programming, providing both practical real-world examples, as well as reference material.

  • Learn what multithreaded programming is and how you can benefit from it
  • Understand the differences between a web worker, a service worker, and a worker thread
  • Know when and when not to use threads in an application
  • Orchestrate communication between threads by leveraging the Atomics object
  • Build high-performance applications using the knowledge you gain from this book
  • Benchmark performance to learn if you'll benefit from multithreading

Table of Contents

  1. Preface
    1. Conventions Used in This Book
    2. Using Code Examples
    3. O’Reilly Online Learning
    4. How to Contact Us
    5. Acknowledgments
  2. 1. Introduction
    1. What Are Threads?
    2. Concurrency vs. Parallelism
    3. Single-Threaded JavaScript
    4. Hidden Threads
    5. Threads in C: Get Rich with Happycoin
    6. With Only the Main Thread
    7. With Four Worker Threads
  3. 2. Browsers
    1. Dedicated Workers
    2. Dedicated Worker Hello World
    3. Advanced Dedicated Worker Usage
    4. Shared Workers
    5. Shared Worker Hello World
    6. Advanced Shared Worker Usage
    7. Service Workers
    8. Service Worker Hello World
    9. Advanced Service Worker Concepts
    10. Message Passing Abstractions
    11. The RPC Pattern
    12. The Command Dispatcher Pattern
    13. Putting it All Together
  4. 3. Node.js
    1. Before We Had Threads
    2. The worker_threads Module
    3. workerData
    4. MessagePort
    5. Happycoin: Revisited
    6. With Only the Main Thread
    7. With Four Worker Threads
    8. Worker Pools with Piscina
    9. A Pool Full of Happycoins
  5. 4. Shared Memory
    1. Intro to Shared Memory
    2. Shared Memory in the Browser
    3. Shared Memory in Node.js
    4. SharedArrayBuffer and TypedArrays
    5. Atomic Methods for Data Manipulation
    6. Atomics.add()
    7. Atomics.and()
    8. Atomics.compareExchange()
    9. Atomics.exchange()
    10. Atomics.isLockFree()
    11. Atomics.load()
    12. Atomics.or()
    13. Atomics.store()
    14. Atomics.sub()
    15. Atomics.xor()
    16. Atomicity Concerns
    17. Return Values Ignore Conversion
    18. Data Serialization
    19. Booleans
    20. Strings
    21. Objects
  6. 5. Advanced Shared Memory
    1. Atomic Methods for Coordination
    2. Atomics.wait()
    3. Atomics.notify()
    4. Atomics.waitAsync()
    5. Timing and Non-Determinism
    6. Example of Non-Determinism
    7. Detecting Thread Preparedness
    8. Example Application: Conway’s Game of Life
    9. Single-Threaded Game of Life
    10. Multithreaded Game of Life
    11. Atomics and Events
  7. 6. Multithreaded Patterns
    1. Thread Pool
    2. Pool Size
    3. Dispatch Strategies
    4. Example Implementation
    5. Mutex: A Basic Lock
    6. Streaming Data with Ring Buffers
    7. Actor Model
    8. Pattern Nuances
    9. Relating to JavaScript
    10. Example Implementation
  8. 7. WebAssembly
    1. Your First WebAssembly
    2. Atomic Operations in WebAssembly
    3. Compiling C Programs to WebAssembly with Emscripten
    4. Other WebAssembly Compilers
    5. AssemblyScript
    6. Happycoin in AssemblyScript
  9. 8. Analysis
    1. When Not to Use
    2. Low Memory Constraints
    3. Low Core Count
    4. Containers vs Threads
    5. When to Use
    6. Example Benchmark: Template Rendering
    7. Summary of Caveats
  10. A. Structured Clone Algorithm
18.118.32.213