better performance with webworkers - github€¦ · better performance with webworkers konsultaner...

17
Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax: + 49 (0) 3 529 / 52 902 44 www.konsultaner.de [email protected]

Upload: others

Post on 03-Jun-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Better performance withWebWorkers

Konsultaner GmbH & Co. KGLugturmstr. 4501809 Heidenau

Tel: + 49 (0) 3 529 / 52 902 40Fax: + 49 (0) 3 529 / 52 902 44

[email protected]

Page 2: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

About us

» Web technology agency

» Java WebSocket-Server + sub protocol = connectanum

» Several JavaScript Modules

» PHP-CMS

» Custom BI-Websoftware

» Apps

Page 3: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

What are WebWorkers

» Isolated Threads in JavaScript

Page 4: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Can i use

caniuse.com/#feat=webworkers

caniuse.com/#search=shared

Page 5: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Why WebWorkers

agiert agiert / reagiert

Why

» Prevent ui-thread blocking» Enhance performance» (With) concurrent computation

Easy multithreading

» No locking, no synchronization, no atomics» Immutable» Comes with simple message system» But no shared memory

Page 6: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Implementation

index.js

var worker = new Worker('doWork.js');worker.addEventListener('message', function(e) { console.log('Worker said: ', e.data);}, false);worker.postMessage('Hello World');

doWorker.js

self.addEventListener('message', function(e) { self.postMessage(e.data);}, false);

Source: html5rocks.com

Page 7: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Implementation

Easy to solve problems» XML-parsing » Just use one of a milltion tools to compensate it » i.e. https://github.com/tobiasnickel/tXml » Angular 2 runs in a WebWorker

» The missing load event » Write your own » When all your initialization (i.e. server requests) is done, send a ready message

» Serialization is really fast, everything important works as expected » Bigger data can be passed as ArrayBuffer in the transferList » BUT: Functions are not serializable! » I know eval, but I wouldn't use it » You can add functionality during your build process

Page 8: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Implementation

Non-trivial problems » WebWorker only provide a simple asynchronous message send and receive logic

» Nice to have features » Multiple threads » RPC calls » Promises » Progress events (thread.js) » If you prefer the actor pattern (akka.js)

Page 9: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Performance

Serialization

» Is serialization too slow? » http://runspired.github.io/webworker-performance/

» Chrome52 on this Laptop » ~14kbyte String => 133ms {String[]} => 238ms {String[]} as transferable => 290ms

» ~35kbyte String => 303ms {*} => 968ms {*} => 611ms

Page 10: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Performance

Computing

» Google says » In many cases you can move pure computational work to Web Workers, [...] Data manipulation or traversal, like sorting or searching, are often good fits for this model, as are loading and model generation

» But the worker has extra » Source load time » Creation time » Data transfer time

Source: https://developers.google.com/web/fundamentals/performance/rendering/optimize-javascript-execution

Page 11: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Performance

Computing

» Load the Worker once, not for each computation » Measure performance Everything under 3-4ms is not worth thinking about [google] » Use worker when ever javascript blocks the ui-thread

Page 12: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Performance

Example with Ionic

» Ionic app, page transition optimization » Transition from dashboard to a heavy list view » Ng-repeat is very slow » Manual DOM creation with angular services not too fast » 3-4 Seks processing time on samsung galaxy S5 with crosswalk to finish the transition with ng-repeat

» ~1Sek processing time in browser

Page 13: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Performance

Profile not optimized» DOM generated in directive

Page 14: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Performance

Profile not optimized» DOM generated in worker

Page 15: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Performance

Example Calendar» Approves performance of ionic example (~26ms)

Page 16: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Performance

Multicore» Calculate 2x mod 97777 using n threads on a Intel i7 6700 Quadcore (8 logical cores) @2.6GHz

Source: http://pmav.eu/stuff/javascript-webworkers/

Page 17: Better performance with WebWorkers - GitHub€¦ · Better performance with WebWorkers Konsultaner GmbH & Co. KG Lugturmstr. 45 01809 Heidenau Tel: + 49 (0) 3 529 / 52 902 40 Fax:

Thank You!