Slide 1

Slide 1 text

Vladimir Agafonkin High Performance Data Visualizations in JavaScript November 2013

Slide 2

Slide 2 text

/mourner

Slide 3

Slide 3 text

data visualization

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

not static anymore

Slide 11

Slide 11 text

responding to user actions clicking, hovering, scrolling, touch gestures, etc.

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

navigating through data

Slide 16

Slide 16 text

filtering

Slide 17

Slide 17 text

demand for real-time interactivity is increasing

Slide 18

Slide 18 text

responsibility for processing data is shifting from the server to the browser

Slide 19

Slide 19 text

lots of data + lots of rendering = big performance problem

Slide 20

Slide 20 text

pure JS fast rendering, DOM slow

Slide 21

Slide 21 text

rule #1 the less stuff we render the better

Slide 22

Slide 22 text

data processing rendering

Slide 23

Slide 23 text

data processing rendering

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

data reduction

Slide 26

Slide 26 text

search

Slide 27

Slide 27 text

loading data once ! searching data lots of times

Slide 28

Slide 28 text

search index

Slide 29

Slide 29 text

grid

Slide 30

Slide 30 text

tree data structures •binary heap •binary search tree •range tree •k-d tree •quadtree •R-tree

Slide 31

Slide 31 text

points – quadtree

Slide 32

Slide 32 text

rectangles – R-tree

Slide 33

Slide 33 text

var tree = rbush(); ! tree.insert([5, 10, 15, 25, obj]); ! ... ! tree.search([7, 7, 12, 12]); github.com/mourner/rbush

Slide 34

Slide 34 text

greedy rendering

Slide 35

Slide 35 text

•avoids rendering many objects in the same spot •free index for instant mouse/touch interaction greedy rendering

Slide 36

Slide 36 text

kothic.org/js

Slide 37

Slide 37 text

collision detection

Slide 38

Slide 38 text

Crossfilter (many dimensions)

Slide 39

Slide 39 text

geometric clipping

Slide 40

Slide 40 text

polyline clipping Cohen-Sutherland algorithm

Slide 41

Slide 41 text

polygon clipping Sutherland-Hodgeman algorithm

Slide 42

Slide 42 text

polyline simplification

Slide 43

Slide 43 text

distance-based simplification

Slide 44

Slide 44 text

Douglas-Peucker simplification

Slide 45

Slide 45 text

mourner.github.com/simplify-js

Slide 46

Slide 46 text

clustering grouping objects which are close to each other

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

hierarchical clustering once for all zoom levels

Slide 50

Slide 50 text

data loading and processing

Slide 51

Slide 51 text

UI JS browser freezes on heavy calculations UI

Slide 52

Slide 52 text

Web Workers

Slide 53

Slide 53 text

pure JS fast ! rendering, DOM slow

Slide 54

Slide 54 text

Worker •isolated DOM-less environment, freaking fast •runs in its own thread, doesn’t lock the UI •sends and receives messages

Slide 55

Slide 55 text

Web Workers you’ve been using it all wrong

Slide 56

Slide 56 text

! ... worker.postMessage(HUGE_DATA_ARRAY); ... loading and sending to Worker

Slide 57

Slide 57 text

UI Worker JS browser freezes on data loading and sending data loading UI

Slide 58

Slide 58 text

importScripts('data.js'); ! ... ! onmessage = function (e) { var result = searchData(e.data.query); ! postMessage(result); } Loading in Worker

Slide 59

Slide 59 text

UI Worker JS browser freezes when receiving data from Worker data loading UI UI

Slide 60

Slide 60 text

var array = new Float16Array(len); ... ! var buffer = array.buffer; ! postMessage(buffer, [buffer]); ! // buffer stops being available transferable objects (all browsers except IE)

Slide 61

Slide 61 text

UI Worker JS browser doesn't freeze, data is sent as ArrayBuffer data loading UI UI

Slide 62

Slide 62 text

function addNumbers(a, b) { 'use asm'; ! a = a | 0; // int b = +b; // double ! return +(a + b); // double } asm.js

Slide 63

Slide 63 text

asm.js •useful for computational bottlenecks •supported in FF only •backwards compatibility! •V8 optimizes without annotations

Slide 64

Slide 64 text

V8: lets make it as fast as asm.js but without the need for special syntax

Slide 65

Slide 65 text

rendering backends SVG, Canvas, WebGL

Slide 66

Slide 66 text

SVG •fast native events for interactivity •easy to update separate objects •easy to animate •slows down the browser (with a large number of objects)

Slide 67

Slide 67 text

Canvas •doesn't affect browser performance after rendering •you can draw something once and copy •pixel data can be manipulated or generated in a Worker

Slide 68

Slide 68 text

Canvas •expensive to redraw with each update •hard to implement interactivity

Slide 69

Slide 69 text

WebGL •main way to visualize in 3D •very fast in 2D if you need to draw lots of sprites •performance gain vs Canvas-2D is questionable in other cases •much more difficult to use; limitations •no support in iOS and IE9-10, difficult in IE11

Slide 70

Slide 70 text

low number of objects: use SVG ! lots of stuff to draw: use Canvas

Slide 71

Slide 71 text

Canvas Performance

Slide 72

Slide 72 text

redraw partially

Slide 73

Slide 73 text

use several Canvas layers

Slide 74

Slide 74 text

draw once to an offscreen canvas and copy

Slide 75

Slide 75 text

function drawStar(ctx, x, y) { ... } ! drawStar(ctx, 10, 20); drawStar(ctx, 50, 70); ...

Slide 76

Slide 76 text

function drawStar() { ... return canvas; } ! var star = drawStar(); ! ctx.drawImage(star, 10, 20); ctx.drawImage(star, 50, 70); ...

Slide 77

Slide 77 text

minimize stroke/fill

Slide 78

Slide 78 text

function drawLine(x1, x2, y1, y2) { ! ctx.strokeStyle = 'red'; ! ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ! ctx.stroke(); } ! drawLine(10, 20, 30, 40); drawLine(200, 10, 0, 50); drawLine(30, 40, 70, 0);

Slide 79

Slide 79 text

function drawLine(x1, x2, y1, y2) { ! ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); } ! ctx.strokeStyle = 'red'; ! drawLine(10, 20, 30, 40); drawLine(200, 10, 0, 50); drawLine(30, 40, 70, 0); ! ctx.stroke();

Slide 80

Slide 80 text

generate or manipulate raw pixel data in a Worker

Slide 81

Slide 81 text

var data = ctx.getImageData(0, 0, width, height).data; ! worker.postMessage(data.buffer, [data.buffer]); ! ... ! worker.onmessage = function (e) { var imageData = ctx.createImageData(width, height); ! imageData.data.set(e.data); ! ctx.putImageData(imageData, 0, 0); } Canvas + Worker

Slide 82

Slide 82 text

var pixels = new Uint8ClampedArray( width * height); ! function drawPixel(x, y, r, g, b, a) { var i = 4 * (256 * y + x); ! pixels[i] = r; pixels[i + 1] = g; pixels[i + 2] = b; pixels[i + 3] = a; } ! ... ! postMessage(pixels.buffer, [pixels.buffer]); drawing pixels in a Worker

Slide 83

Slide 83 text

var pixels = new Uint8ClampedArray(data); ! for (var x = 0; x < width; x++) { for (var y = 0; y < height; y++) { var i = 4 * (256 * y + x); ! pixels[i] = 2 * pixels[i]; pixels[i + 1] = 2 * pixels[i + 1]; pixels[i + 2] = 2 * pixels[i + 2]; } } ! ... ! postMessage(pixels.buffer, [pixels.buffer]); processing pixels in a Worker

Slide 84

Slide 84 text

dynamic hill shading

Slide 85

Slide 85 text

still too much for the browsers? resorting to the server

Slide 86

Slide 86 text

Cavnas lookup table

Slide 87

Slide 87 text

UTFGrid •65535 symbols •each symbol is 4х4 pixels •1-3 KB per 256х256 tile in average

Slide 88

Slide 88 text

Thanks! Questions? Vladimir Agafonkin [email protected]