Apply the core principles of concurrency to both browser and server side development
Explore the latest tools and techniques at the forefront of concurrent programming, including JavaScript promises, web workers, and generators
Learn how concurrent and parallel programming can help you tackle the challenges of fast, data heavy web development
Who This Book Is For
This book is written for any JavaScript developer who wants to learn how to write more efficient, powerful, and maintainable applications that utilize the latest developments in JavaScript.
What You Will Learn
Understand exactly how JavaScript works in a web browser environment and how these mechanisms power our event-driven JavaScript code
Use promises to turn complex synchronization scenarios into readable and maintainable code
Compute values lazily and avoid unnecessary memory allocations using generators
Write concurrent code that doesn't feel like concurrent code by abstracting away boilerplate chores
Leverage true hardware parallelism with web workers to get better performance
Get to grips with the NodeJS model of concurrency and learn why it's good for I/O-intensive web applications
In Detail
JavaScript has evolved to adopt concurrent capabilities―one of the reasons why it is still at the forefront of modern web development. This book helps you dive into concurrent JavaScript and demonstrates how to apply its core principles, key techniques, and tools to a range of complex development challenges. Built around the three core principles of concurrency―parallelism, synchronization, and conservation―you will learn everything you need to unlock a more efficient and dynamic JavaScript in order to lay the foundations of even better user experiences.
Throughout the book you will learn how to put these principles into action by using a range of development approaches. Covering everything from JavaScript promises, web workers, and generators to functional programming techniques, everything you learn will have a real impact on the performance of your applications. You will also learn how to move between the client and server for a frictionless and fully realized approach to development. With further guidance on concurrent programming with NodeJS, JavaScript Concurrency is committed to making you a better web developer.
Chapter 1: Why JavaScript Concurrency? 1
Synchronous JavaScript 2
Synchronicity is easy to understand 2
Asynchronous is inevitable 3
Asynchronous browsers 4
Types of concurrency 5
Asynchronous actions 5
Parallel actions 6
JavaScript concurrency principles: Parallelize, Synchronize,
Conserve 8
Parallelize 9
Synchronize 10
The Promise API 10
Conserve 11
Chapter 2: The JavaScript Execution Model 15
Everything is a task 16
Meet the players 16
The Execution environment 17
Event loops 18
Task queues 18
Execution contexts 19
Maintaining execution state 20
Job queues 20
Creating tasks using timers 21
Using setTimeout() 22
Using setInterval() 23
Responding to DOM events 24
Event targets 24
Managing event frequency 26
Responding to network events 28
Making requests 28
Coordinating requests 29
Concurrency challenges with this model 30
Limited opportunity for parallelism 31
Synchronization through callbacks 31
Chapter 3: Synchronizing with Promises 33
Promise terminology 33
Promise 33
State 34
Executor 34
Resolver 34
Rejector 34
Thenable 34
Resolving and rejecting promises 35
Resolving promises 35
Rejecting promises 37
Empty promises 39
Reacting to promises 41
Resolution job queues 41
Using promised data 42
Error callbacks 43
Always reacting 45
Resolving other promises 46
Promise–like objects 47
Building callback chains 48
Promises only change state once 48
Immutable promises 50
Many then callbacks, many promises 51
Passing promises around 53
Synchronizing several promises 56
Waiting on promises 56
Cancelling promises 57
Promises without executors 59
Chapter 4: Lazy Evaluation with Generators 63
Call stacks and memory allocation 63
Bookmarking function contexts 64
Sequences instead of arrays 64
Creating generators and yielding values 66
Generator function syntax 66
Yielding values 67
Iterating over generators 68
Infinite sequences 69
No end in sight 69
Alternating sequences 70
Deferring to other generators 72
Selecting a strategy 73
Interweaving generators 76
Passing data to generators 79
Reusing generators 79
Lightweight map/reduce 81
Coroutines 85
Creating coroutine functions 86
Handling DOM events 88
Handling promised values 90
Chapter 5: Working with Workers 93
What are workers? 93
OS threads 94
Event targets 94
True parallelism 95
Types of workers 96
Dedicated workers 96
Sub-workers 97
Shared workers 98
Worker environments 99
What's available, what isn't? 99
Loading scripts 100
Communicating with workers 100
Posting messages 101
Message serialization 101
Receiving messages from workers 103
Sharing application state 104
Sharing memory 104
Fetching resources 106
Communicating between pages 108
Performing sub-tasks with sub-workers 110
Dividing work into tasks 110
A word of caution 114
Error handling in web workers 115
Error condition checking 115
Exception handling 116
Chapter 6: Practical Parallelism 119
Functional programming 119
Data in, data out 120
Immutability 121
Referential transparency and time 123
Do we need to go parallel? 125
How big is the data? 126
Hardware concurrency capabilities 128
Creating tasks and assigning work 129
Candidate problems 135
Embarrassingly parallel 135
Searching collections 135
Mapping and reducing 138
Keeping the DOM responsive 140
Bottom halves 140
Translating DOM manipulation 141
Translating DOM events 143
Chapter 7: Abstracting Concurrency 147
Writing concurrent code 147
Hiding the concurrency mechanism 148
Without concurrency 149
Worker communication with promises 151
Helper functions 151
Extending postMessage() 154
Synchronizing worker results 157
Lazy workers 159
Reducing overhead 159
Generating values in workers 160
Lazy worker chains 163
Using Parallel.js 166
How it works 166
Spawning workers 168
Mapping and reducing 169
Worker pools 172
Allocating pools 172
Scheduling jobs 174
Chapter 8: Evented IO with NodeJS 177
Single threaded IO 177
IO is slow 178
IO events 179
Multi-threading challenges 180
More connections, more problems 181
Deploying to the Internet 181
The C10K problem 182
Lightweight event handlers 183
Evented network IO 184
Handling HTTP requests 185
Streaming responses 186
Proxy network requests 189
Evented file IO 193
Reading from files 193
Writing to files 196
Streaming reads and writes 198
Chapter 9: Advanced NodeJS Concurrency 201
Coroutines with Co 201
Generating promises 202
Awaiting values 203
Resolving values 204
Asynchronous dependencies 206
Wrapping coroutines 207
Child Processes 208
Blocking the event loop 209
Forking processes 210
Spawning external processes 212
Inter-process communication 214
Process Clusters 217
Challenges with process management 218
Abstracting process pools 218
Server clusters 221
Proxying requests 221
Facilitating micro-services 222
Informed load balancing 224
Chapter 10: Building a Concurrent Application 229
Getting started 230
Concurrency first 230
Retrofitting concurrency 231
Application types 232
Requirements 232
The overall goal 233
The API 233
The UI 234
Building the API 235
The HTTP server and routing 235
Co-routines as handlers 237
The create chat handler 239
The join chat handler 240
The load chat handler 242
The send message handler. 242
Static handlers 244
Building the UI 245
Talking to the API 245
Implementing the HTML 249
DOM events and manipulation 250
Adding an API worker 254
Additions and improvements 258
Clustering the API 259
Cleaning up chats 259
Asynchronous entry points 259
Who's typing? 260
Leaving chats 260
Polling timeouts 260
No comments:
Post a Comment