Slide 1

Slide 1 text

@JoeKarlsson1 ! 1 JS + Parallel Processing(!?) www.joekarlsson.com

Slide 2

Slide 2 text

@JoeKarlsson1 ! 2 Joe Karlsson @JOEKARLSSON1 @JOEKARLSSON JOEKARLSSON.COM

Slide 3

Slide 3 text

@JoeKarlsson1 ! 3 How is this going to work?

Slide 4

Slide 4 text

@JoeKarlsson1 ! 4 JavaScript was designed to run on a single thread

Slide 5

Slide 5 text

@JoeKarlsson1 ! 5 Can Do Anything Multithreads Can do Easy to Program Easy to Port Code *For non-data intensive operations *Faster Than Multithreads Easy to Debug/Test Why Single Threads?

Slide 6

Slide 6 text

@JoeKarlsson1 ! 6 What’s the problem?

Slide 7

Slide 7 text

@JoeKarlsson1 ! 7

Slide 8

Slide 8 text

@JoeKarlsson1 ! 8 Single thread is SHARED with the browser

Slide 9

Slide 9 text

@JoeKarlsson1 ! 9

Slide 10

Slide 10 text

@JoeKarlsson1 ! 10 • Parsing large JSON set • Image Processing • Sound Processing • Real Time Analytics Things that take a lot of time

Slide 11

Slide 11 text

@JoeKarlsson1 ! 11

Slide 12

Slide 12 text

@JoeKarlsson1

Slide 13

Slide 13 text

@JoeKarlsson1 ! 13 This even is worse on mobile

Slide 14

Slide 14 text

@JoeKarlsson1 ! 14 So, how do we deal with slow processes?

Slide 15

Slide 15 text

@JoeKarlsson1 ! 15 We could send the CPU intense process to a server

Slide 16

Slide 16 text

@JoeKarlsson1 ! 16

Slide 17

Slide 17 text

@JoeKarlsson1 ! 17 Web Workers Multiple Threads in the browser!

Slide 18

Slide 18 text

@JoeKarlsson1 ! 18 Background scripts that run on a different thread

Slide 19

Slide 19 text

@JoeKarlsson1 ! 19 • Faster • More Responsive • Battery efficient • Support in every major browser (IE 10+)

Slide 20

Slide 20 text

@JoeKarlsson1 ! 20 Web Workers cannot directly access the Window Object or the DOM

Slide 21

Slide 21 text

@JoeKarlsson1 ! 21 How do Web Workers work?

Slide 22

Slide 22 text

@JoeKarlsson1 ! 22

Slide 23

Slide 23 text

@JoeKarlsson1 ! 23 Each Web Worker you create === a new thread

Slide 24

Slide 24 text

@JoeKarlsson1 ! 24

Slide 25

Slide 25 text

@JoeKarlsson1 ! 25 They communicate via Message Channel

Slide 26

Slide 26 text

@JoeKarlsson1 ! 26

Slide 27

Slide 27 text

@JoeKarlsson1 ! 27 Web Workers operate in a different context They cannot directly access the Window or DOM

Slide 28

Slide 28 text

@JoeKarlsson1 ! 28 But they can do pretty much everything else the main thread can do

Slide 29

Slide 29 text

@JoeKarlsson1 ! 29 Let’s look at real code Code: https://github.com/JoeKarlsson/live-code/tree/master/web-workers

Slide 30

Slide 30 text

@JoeKarlsson1 ! 30

Slide 31

Slide 31 text

@JoeKarlsson1 ! 31

Slide 32

Slide 32 text

@JoeKarlsson1 ! 32

Slide 33

Slide 33 text

@JoeKarlsson1 ! 33

Slide 34

Slide 34 text

@JoeKarlsson1 ! 34 /js/bigLoop.js

Slide 35

Slide 35 text

@JoeKarlsson1 ! 35

Slide 36

Slide 36 text

@JoeKarlsson1 ! 36 How do we stop a Web Worker?

Slide 37

Slide 37 text

@JoeKarlsson1 ! 37

Slide 38

Slide 38 text

@JoeKarlsson1 ! 38

Slide 39

Slide 39 text

@JoeKarlsson1 ! 39

Slide 40

Slide 40 text

@JoeKarlsson1 ! 40 Errors?

Slide 41

Slide 41 text

@JoeKarlsson1 ! 41 js errror

Slide 42

Slide 42 text

@JoeKarlsson1 ! 42

Slide 43

Slide 43 text

@JoeKarlsson1 ! 43 How to check for browser support?

Slide 44

Slide 44 text

@JoeKarlsson1 ! 44 browser

Slide 45

Slide 45 text

@JoeKarlsson1 ! 45

Slide 46

Slide 46 text

@JoeKarlsson1 ! 46 Future of Web Workers

Slide 47

Slide 47 text

@JoeKarlsson1 ! 47

Slide 48

Slide 48 text

@JoeKarlsson1 ! 48 WebGL and Canvas2D

Slide 49

Slide 49 text

@JoeKarlsson1 ! 49

Slide 50

Slide 50 text

@JoeKarlsson1 ! 50

Slide 51

Slide 51 text

@JoeKarlsson1 ! 51 • Web Workers are relatively heavy- weight, and are not intended to be used in large numbers • Web Workers are amazing but hard to manage

Slide 52

Slide 52 text

@JoeKarlsson1 ! 52 Questions ?

Slide 53

Slide 53 text

@JoeKarlsson1 ! 53 Thank You!

Slide 54

Slide 54 text

@JoeKarlsson1 ! 54 Let’s Stay In Touch @JOEKARLSSON1 @JOEKARLSSON JOEKARLSSON.COM

Slide 55

Slide 55 text

@JoeKarlsson1 ! 55 Additional Resources 1. Code: https://github.com/JoeKarlsson/live-code/tree/master/web-workers 2. Slides: 3. Web Workers: https://developer.mozilla.org/en-US/docs/Web/API/ Web_Workers_API/Functions_and_classes_available_to_workers
 4. Web Workers: http://techslides.com/html5-web-workers-for-ajax-requests
 5. Workers: https://html.spec.whatwg.org/multipage/workers.html#shared- workers-introduction

Slide 56

Slide 56 text

@JoeKarlsson1 ! 56