Multithreaded Javascript

Book description

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

Publisher resources

View/Submit Errata

Table of contents

  1. Foreword
  2. Preface
    1. Target Audience
    2. Goals
    3. Conventions Used in This Book
    4. Using Code Examples
    5. O’Reilly Online Learning
    6. How to Contact Us
    7. Acknowledgments
  3. 1. Introduction
    1. What Are Threads?
    2. Concurrency Versus Parallelism
    3. Single-Threaded JavaScript
    4. Hidden Threads
    5. Threads in C: Get Rich with Happycoin
      1. With Only the Main Thread
      2. With Four Worker Threads
  4. 2. Browsers
    1. Dedicated Workers
      1. Dedicated Worker Hello World
      2. Advanced Dedicated Worker Usage
    2. Shared Workers
      1. Shared Worker Hello World
      2. Advanced Shared Worker Usage
    3. Service Workers
      1. Service Worker Hello World
      2. Advanced Service Worker Concepts
    4. Message Passing Abstractions
      1. The RPC Pattern
      2. The Command Dispatcher Pattern
      3. Putting It All Together
  5. 3. Node.js
    1. Before We Had Threads
    2. The worker_threads Module
      1. workerData
      2. MessagePort
    3. Happycoin: Revisited
      1. With Only the Main Thread
      2. With Four Worker Threads
    4. Worker Pools with Piscina
    5. A Pool Full of Happycoins
  6. 4. Shared Memory
    1. Intro to Shared Memory
      1. Shared Memory in the Browser
      2. Shared Memory in Node.js
    2. SharedArrayBuffer and TypedArrays
    3. Atomic Methods for Data Manipulation
      1. Atomics.add()
      2. Atomics.and()
      3. Atomics.compareExchange()
      4. Atomics.exchange()
      5. Atomics.isLockFree()
      6. Atomics.load()
      7. Atomics.or()
      8. Atomics.store()
      9. Atomics.sub()
      10. Atomics.xor()
    4. Atomicity Concerns
    5. Data Serialization
      1. Booleans
      2. Strings
      3. Objects
  7. 5. Advanced Shared Memory
    1. Atomic Methods for Coordination
      1. Atomics.wait()
      2. Atomics.notify()
      3. Atomics.waitAsync()
    2. Timing and Nondeterminism
      1. Example of Nondeterminism
      2. Detecting Thread Preparedness
    3. Example Application: Conway’s Game of Life
      1. Single-Threaded Game of Life
      2. Multithreaded Game of Life
    4. Atomics and Events
  8. 6. Multithreaded Patterns
    1. Thread Pool
      1. Pool Size
      2. Dispatch Strategies
      3. Example Implementation
    2. Mutex: A Basic Lock
    3. Streaming Data with Ring Buffers
    4. Actor Model
      1. Pattern Nuances
      2. Relating to JavaScript
      3. Example Implementation
  9. 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
  10. 8. Analysis
    1. When Not to Use
      1. Low Memory Constraints
      2. Low Core Count
      3. Containers Versus Threads
    2. When to Use
    3. Summary of Caveats
  11. A. Structured Clone Algorithm
  12. Index

Product information

  • Title: Multithreaded Javascript
  • Author(s): Thomas Hunter, Bryan English
  • Release date: September 2021
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098104436