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