window.getComputedStyle(elem).width,
elem.offsetTop,
elem.getClientRects() etc.
→ reflow
Don't do this
for (var i = 1; i < containerElem.children.length; i++) {
containerElem.children[i].style.top =
containerElem.children[i-1].offsetTop + 10 + "px";
}
Avoiding forcing reflow
* Average result after 3~5 runs of test A and
test B.
Results for Firefox were particularly variable but were
generally only slightly faster since the test animates
the top property which does not trigger reflow
in Firefox.
Browser
Triggering reflow
Not doing that
Avg. improvement
Firefox 34
42.1fps*
45.8fps*
3.7fps / 9%
Chrome 36
10.5fps
23.2fps
12.7fps / 120%
IE 11
8.2fps
19.1fps
10.9fps / 132%
Try reading computed style (especially geometry) less often or not
at all.
Paint cost
✕
Paint flashing (Firefox)
Paint rectangles (Chrome)
Paint flashing #1
Paint flashing #2
Paint flashing #3
Paint complexity
box-shadow
border-radius
SVG filters…
Try replacing expensive effects with simpler ones
Pre-rendering
Browser
<iframe src="svg">
<img src="png">
Firefox 34
1.9 fps
49.5 fps
Chrome 36
11.18 fps
49.7 fps
IE 11
5.8 fps
50.9 fps
Pre-render expensive assets
Pre-rendering
* Some rendering defects
Browser
<iframe src="svg">
<img src="svg">
<img src="png">
Firefox 34
1.9 fps
49.1 fps
49.5 fps
Chrome 36
11.18 fps
13.0 fps*
49.7 fps
IE 11
5.8 fps
15.5 fps
50.9 fps
Try using <img> to embed SVG images instead
of <iframe> (or <object>,
<embed>).
Hardware acceleration
Paths, text
Not much
Filters
Coming
Compositing
OK
What gets a layer?
Inspecting layers
Firefox: about:config
→ layers.draw-borders to true
(requires layers.offmainthreadcomposition.enabled
to be true)
Chrome: DevConsole → Rendering →
Show composited layer borders
Inspecting layers
Inspecting layers
Layers and transitions
Layers and SVG Animation
Layers and scripted animation
Starting an animation
Enter will-change
will-change:<property>
will-change:transform
will-change:opacity
will-change:scroll-position
will-change:contents
transform:translateZ(0)
Firefox: need
layout.css.will-change.enabled in
about:config.
Applying will-change
Your browser is a jank
Compositor animation
Animation on the compositor
Not so fast…
Representable by compositor? (e.g. transform, opacity)