1 of 38

Hey! I’m Nikhil👋🏻

Engineer @Postman

@niksharma1997

nik72619c

> npx nik72619c

2 of 38

Wait…React is Multi-threaded?

3 of 38

User Experience is Important!

4 of 38

5 of 38

6 of 38

Lets see the problem…

7 of 38

8 of 38

9 of 38

10 of 38

Let’s do the math

11 of 38

Javascript

Style

Layout

Paint

Composite

16ms

12 of 38

Javascript

Style

Layout

Paint

Composite

16ms

13 of 38

14 of 38

15 of 38

WEB WORKERS

16 of 38

Web workers allow us to do work in parallel using separate threads

17 of 38

Web workers allow us to do work in parallel using separate threads

Worker

18 of 38

Web workers allow us to do work in parallel using separate threads

Worker

new worker()

worker.postMessage()

self.addEventListener()

19 of 38

Web workers allow us to do work in parallel using separate threads

Worker

new worker()

worker.postMessage()

self.addEventListener()

worker.addEventListener()

self.postMessage()

20 of 38

Web workers allow us to do work in parallel using separate threads

Worker

new worker()

worker.postMessage()

self.addEventListener()

worker.addEventListener()

self.postMessage()

worker.terminate()

21 of 38

What about Concurrent mode ?

22 of 38

User event

Render phase

(Uninterruptible)

Commit phase

Next event

23 of 38

User event

Interruptible render phase

Commit phase

(Uninterruptible)

Interleaved events

24 of 38

25 of 38

Isn’t JS single-threaded?

26 of 38

Thread != CPU core

27 of 38

28 of 38

So why don’t we use it?

29 of 38

Message passing is too much effort

30 of 38

How to know when it’s complete?

31 of 38

How to coordinate between workers?

32 of 38

Promises! 🤞

33 of 38

useWorker

https://github.com/alewin/useWorker

COMLINK

https://github.com/GoogleChromeLabs/comlink

34 of 38

Define

Use

35 of 38

36 of 38

37 of 38

When to use

  1. Virtual DOM diffing
  2. Image manipulation and processing
  3. Canvas drawing

38 of 38

When not to use

  1. IO bound tasks
  2. DOM manipulation is being performed
  3. LocalStorage
  4. If it isn’t worth it