power and performance€¦ · 35% reduction in cpu power usage testing conducted by apple in june...

239
These are confidential sessions—please refrain from streaming, blogging, or taking pictures Optimizing your website for great battery life and responsive scrolling Session 607 Power and Performance Simon Fraser Safari and WebKit Engineer

Upload: others

Post on 03-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

These are confidential sessions—please refrain from streaming, blogging, or taking pictures

Optimizing your website for great battery life and responsive scrolling

Session 607

Power and Performance

Simon FraserSafari and WebKit Engineer

Page 2: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 3: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 4: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 5: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 6: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 7: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 8: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 9: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Will Learn

Page 10: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Will Learn

•New power-saving features in Safari

Page 11: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Will Learn

•New power-saving features in Safari•Making power-efficient webpages

Page 12: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Will Learn

•New power-saving features in Safari•Making power-efficient webpages• Safari responsive scrolling

Page 13: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Power-Saving Features

Page 14: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Power-Saving Features

Page 15: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Power-Saving Features

•App Nap for Safari tabs

Page 16: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Power-Saving Features

•App Nap for Safari tabs• Power Saver

Page 17: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

App Nap for Safari TabsNew in Safari 7

Page 18: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

App Nap for Safari TabsNew in Safari 7

Page 19: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What Is App Nap for Safari Tabs?

Page 20: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What Is App Nap for Safari Tabs?

• Each tab has its own Safari Web Content process

Page 21: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What Is App Nap for Safari Tabs?

• Each tab has its own Safari Web Content process•App Nap is applied to the Safari Web Content process for that tab

Page 22: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What Is App Nap for Safari Tabs?

• Each tab has its own Safari Web Content process•App Nap is applied to the Safari Web Content process for that tab • Saves power by napping background tabs

Page 23: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

When Are Tabs Napped?

Page 24: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

When Are Tabs Napped?

• Tab is not visible■ Background tab■ Minimized window■ In another space■ Screen saver is on■ Occluded window

Page 25: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

When Are Tabs Napped?

• Tab is not visible■ Background tab■ Minimized window■ In another space■ Screen saver is on■ Occluded window

• Idle windows

Page 26: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Effects of App Nap for Safari Tabs

Page 27: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Effects of App Nap for Safari Tabs

• JavaScript timers are rate-limited

Page 28: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Effects of App Nap for Safari Tabs

• JavaScript timers are rate-limited■ setTimeout(), setInterval()

Page 29: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Effects of App Nap for Safari Tabs

• JavaScript timers are rate-limited■ setTimeout(), setInterval()■ requestAnimationFrame()

Page 30: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Effects of App Nap for Safari Tabs

• JavaScript timers are rate-limited■ setTimeout(), setInterval()■ requestAnimationFrame()

•Napping prevented when

Page 31: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Effects of App Nap for Safari Tabs

• JavaScript timers are rate-limited■ setTimeout(), setInterval()■ requestAnimationFrame()

•Napping prevented when■ Playing audio

Page 32: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Effects of App Nap for Safari Tabs

• JavaScript timers are rate-limited■ setTimeout(), setInterval()■ requestAnimationFrame()

•Napping prevented when■ Playing audio■ Loading

Page 33: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

Page 34: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

•Often nothing

Page 35: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

•Often nothing• But be prepared for timer rate-limiting

Page 36: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

New in Safari 7App Nap for Safari Tabs

Page 37: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

New in Safari 7Safari Power Saver

Page 38: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 39: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

35%Reduction In CPU Power Usage

Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with 4GB of RAM and prerelease OS X v10.9. Tested with prerelease Safari 7.0 with Power Saver on (default) vs. off. Systems tested with WPA2 Wi-Fi network connection while running on battery power. Performance will vary based on system configuration, application workload, and other factors.

Up to

Page 40: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 41: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 42: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 43: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 44: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 45: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 46: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 47: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 48: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Smart Power-Saving

Page 49: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Smart Power-Saving

• “Primary” plug-ins continue to play

Page 50: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Smart Power-Saving

• “Primary” plug-ins continue to play• Plug-ins that are not being used for visual purposes continue to play

Page 51: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Smart Power-Saving

• “Primary” plug-ins continue to play• Plug-ins that are not being used for visual purposes continue to play• Remember the user’s choice

Page 52: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

How Safari Power Saver Works

Page 53: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

How Safari Power Saver Works

•What happens to a plug-in when power-saved:

Page 54: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

How Safari Power Saver Works

•What happens to a plug-in when power-saved:■ Runs until it has drawn a useful snapshot

Page 55: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

How Safari Power Saver Works

•What happens to a plug-in when power-saved:■ Runs until it has drawn a useful snapshot■ Replaced with the snapshot

Page 56: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

How Safari Power Saver Works

•What happens to a plug-in when power-saved:■ Runs until it has drawn a useful snapshot■ Replaced with the snapshot■ If the user clicks the snapshot, plug-in is re-created

Page 57: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 58: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 59: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

Page 60: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

•Use fewer plug-ins

Page 61: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

•Use fewer plug-ins■ Don’t use a plug-in when you could use an existing web feature

Page 62: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

•Use fewer plug-ins■ Don’t use a plug-in when you could use an existing web feature

• Be prepared for plug-ins to be destroyed and re-created

Page 63: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

•Use fewer plug-ins■ Don’t use a plug-in when you could use an existing web feature

• Be prepared for plug-ins to be destroyed and re-created■ Request state from the page, not vice versa

Page 64: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

New in Safari 7Safari Power Saver

Page 65: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

How to detect and fix problemsMaking Power-Efficient Webpages

Page 66: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Making Power-Efficient Webpages

Page 67: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Making Power-Efficient Webpages

•New web API

Page 68: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Making Power-Efficient Webpages

•New web API • Tools to detect power-hungry pages

Page 69: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Making Power-Efficient Webpages

•New web API • Tools to detect power-hungry pages• Common mistakes

Page 70: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Making Power-Efficient Webpages

•New web API • Tools to detect power-hungry pages• Common mistakes

Page 71: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Web APIs for Power-Smart Pages

Page 72: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Web APIs for Power-Smart Pages

• Page Visibility

Page 73: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Web APIs for Power-Smart Pages

• Page Visibility• requestAnimationFrame()

Page 74: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

Page 75: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

Page 76: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

•Web API for content to detect when:■ Page is hidden■ Visibility changes

Page 77: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

•Web API for content to detect when:■ Page is hidden■ Visibility changes

• Pages hidden in:■ Background tabs■ Minimized windows■ Occluded windows

Page 78: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

Page 79: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

•When hidden:

Page 80: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

•When hidden:■ JavaScript timers are rate-limited

Page 81: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

•When hidden:■ JavaScript timers are rate-limited■ requestAnimationFrame() is paused

Page 82: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

•When hidden:■ JavaScript timers are rate-limited■ requestAnimationFrame() is paused■ CSS Transitions and Animations paused

Page 83: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

•Query current statedocument.hiddendocument.visibilityState

“hidden”, “visible”, “prerender”

Page 84: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

•Query current statedocument.hiddendocument.visibilityState

“hidden”, “visible”, “prerender”

Page 85: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

•Query current statedocument.hiddendocument.visibilityState

“hidden”, “visible”, “prerender”

Page 86: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

•Query current statedocument.hiddendocument.visibilityState

“hidden”, “visible”, “prerender”

• Listen for state changesdocument.addEventListener('visibilitychange', function(event) {

if (document.hidden) { ...}

}, false);

Page 87: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

•Query current statedocument.hiddendocument.visibilityState

“hidden”, “visible”, “prerender”

• Listen for state changesdocument.addEventListener('visibilitychange', function(event) {

if (document.hidden) { ...}

}, false);

Page 88: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

•Query current statedocument.hiddendocument.visibilityState

“hidden”, “visible”, “prerender”

• Listen for state changesdocument.addEventListener('visibilitychange', function(event) {

if (document.hidden) { ...}

}, false);

Page 89: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Page Visibility

Safari 6.1 Safari 7 iOS7

Page 90: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Scripted Animation

Page 91: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Scripted Animation

• Problems using JavaScript timers for animation

Page 92: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Scripted Animation

• Problems using JavaScript timers for animation■ How often to run?

Page 93: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Scripted Animation

• Problems using JavaScript timers for animation■ How often to run?■ Browser can’t know that you’re just animating

Page 94: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

requestAnimationFrame()

Page 95: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

requestAnimationFrame()

•Web API for animation:window.requestAnimationFrame(callback);

Page 96: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

requestAnimationFrame()

•Web API for animation:window.requestAnimationFrame(callback);window.cancelAnimationFrame(id);

Page 97: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

requestAnimationFrame()

•Web API for animation:window.requestAnimationFrame(callback);window.cancelAnimationFrame(id);

•Advantages

Page 98: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

requestAnimationFrame()

•Web API for animation:window.requestAnimationFrame(callback);window.cancelAnimationFrame(id);

•Advantages■ Display-refresh friendly

Page 99: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

requestAnimationFrame()

•Web API for animation:window.requestAnimationFrame(callback);window.cancelAnimationFrame(id);

•Advantages■ Display-refresh friendly■ Pausable

Page 100: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

requestAnimationFrame()

•Web API for animation:window.requestAnimationFrame(callback);window.cancelAnimationFrame(id);

•Advantages■ Display-refresh friendly■ Pausable

Page 101: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

function doAnimation() {// Compute offset into animation// using new Date().…// Update visuals.…// Use setTimeout() to get called in about 16ms.setTimeout(doAnimation, 16);

}

Before requestAnimationFrame()

Page 102: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

function doAnimation(currentTime) {// Compute offset into animation using currentTime.// currentTime is milliseconds since the document started to load.…// Update visuals.…// Optionally, queue up another callback.requestAnimationFrame(doAnimation);

}

With requestAnimationFrame()

Page 103: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

function doAnimation(currentTime) {// Compute offset into animation using currentTime.// currentTime is milliseconds since the document started to load.…// Update visuals.…// Optionally, queue up another callback.requestAnimationFrame(doAnimation);

}

With requestAnimationFrame()

Page 104: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

function doAnimation(currentTime) {// Compute offset into animation using currentTime.// currentTime is milliseconds since the document started to load.…// Update visuals.…// Optionally, queue up another callback.requestAnimationFrame(doAnimation);

}

With requestAnimationFrame()

Page 105: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

requestAnimationFrame()

Safari 6.1 Safari 7 iOS7Safari 6.1 Safari 7 iOS7

Page 106: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

Page 107: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

•Use Page Visibility to stop doing work in hidden pages

Page 108: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

•Use Page Visibility to stop doing work in hidden pages• If you have to do JavaScript animation, use requestAnimationFrame()

Page 109: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

•Use Page Visibility to stop doing work in hidden pages• If you have to do JavaScript animation, use requestAnimationFrame()• Separate business logic from visual updates

Page 110: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

•Use Page Visibility to stop doing work in hidden pages• If you have to do JavaScript animation, use requestAnimationFrame()• Separate business logic from visual updates

■ Remember that transitions and animations are affected by visibility

Page 111: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

DemoPage Visibility

Tim HortonSafari and WebKit Engineer

Page 112: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Making Power-Efficient Webpages

•New web API • Tools to detect power-hungry pages• Common mistakes

Page 113: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Making Power-Efficient Webpages

•New web API • Tools to detect power-hungry pages• Common mistakes

Page 114: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

How to Detect Power-Hungry Webpages

Page 115: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

How to Detect Power-Hungry Webpages

• System tools

Page 116: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

How to Detect Power-Hungry Webpages

• System tools•Web Inspector

Page 117: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Detecting Power-Hungry PagesActivity Monitor

Page 118: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Detecting Power-Hungry PagesActivity Monitor

Page 119: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Detecting Power-Hungry PagesBattery Menu

Page 120: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Detecting Power-Hungry PagesSafari Web Inspector

Page 121: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Detecting Power-Hungry PagesSafari Web Inspector

Page 122: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Detecting Power-Hungry PagesSafari Web Inspector

Page 123: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Detecting Power-Hungry PagesSafari Web Inspector

Page 124: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Detecting Power-Hungry PagesSafari Web Inspector

Page 125: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Making Power-Efficient Webpages

•New web API • Tools to detect power-hungry pages• Common mistakes

Page 126: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Making Power-Efficient Webpages

•New web API • Tools to detect power-hungry pages• Common mistakes

Page 127: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Avoid Power-Hungry Mistakes

Page 128: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Avoid Power-Hungry Mistakes

• Causing too much layout

Page 129: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Avoid Power-Hungry Mistakes

• Causing too much layout• Painting too much

Page 130: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Things That Can Force Layout

Page 131: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Things That Can Force Layout

offsetLeft, offsetTop, offsetWidth, offsetHeightclientLeft, clientTop, clientWidth, clientHeightscrollLeft, scrollTop, scrollWith, scrollHeightscrollX, scrollY, scrollBy(), scrollTo()document.width, document.heightimage.width, image.heightgetClientRects(), getBoundingClientRect()getPropertyCSSValue() innerText

Page 132: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 133: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 134: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Forcing Layout in a Loop

for (...) {totalHeight += currElement.offsetHeight;...currElement.style.top = totalHeight + 'px';

}

Page 135: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Forcing Layout in a Loop

for (...) {totalHeight += currElement.offsetHeight;...currElement.style.top = totalHeight + 'px';

}

Page 136: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Forcing Layout in a Loop

for (...) {totalHeight += currElement.offsetHeight;...currElement.style.top = totalHeight + 'px';

}

Page 137: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Forcing Layout in a Loop

for (...) {totalHeight += currElement.offsetHeight;...currElement.style.top = totalHeight + 'px';

}

Page 138: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Too Much Painting

Page 139: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Too Much Painting

Page 140: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Too Much Painting

Page 141: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Too Much Painting

•Animated images■ Don’t leave “loading” animated GIFs in the background-image style

Page 142: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Too Much Painting

•Animated images■ Don’t leave “loading” animated GIFs in the background-image style

• Even if they are not visible in the window

Page 143: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 144: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 145: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Use the Right Tool for the Job

Page 146: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Use the Right Tool for the Job

•Animation

Page 147: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Use the Right Tool for the Job

•Animation■ CSS Transitions and Animations

Page 148: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Use the Right Tool for the Job

•Animation■ CSS Transitions and Animations■ requestAnimationFrame()

Page 149: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Use the Right Tool for the Job

•Animation■ CSS Transitions and Animations■ requestAnimationFrame()

• Responsive layouts

Page 150: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Use the Right Tool for the Job

•Animation■ CSS Transitions and Animations■ requestAnimationFrame()

• Responsive layouts■ CSS Media Queries

Page 151: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Use the Right Tool for the Job

•Animation■ CSS Transitions and Animations■ requestAnimationFrame()

• Responsive layouts■ CSS Media Queries■ CSS Flexible Boxes

Page 152: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Use the Right Tool for the Job

•Animation■ CSS Transitions and Animations■ requestAnimationFrame()

• Responsive layouts■ CSS Media Queries■ CSS Flexible Boxes

• CSS calc()

Page 153: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Extensions

Page 154: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Extensions

• Efficiency even more important

Page 155: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Extensions

• Efficiency even more important■ Scripts can be injected into every page

Page 156: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Extensions

• Efficiency even more important■ Scripts can be injected into every page

•Use Page Visibility

Page 157: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Extensions

• Efficiency even more important■ Scripts can be injected into every page

•Use Page Visibility■ Avoid doing work in hidden pages

Page 158: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Extensions

• Efficiency even more important■ Scripts can be injected into every page

•Use Page Visibility■ Avoid doing work in hidden pages■ Only show UI for visible pages

Page 159: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Responsive Scrolling

Page 160: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

People Love to Scroll

Page 161: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Legacy Scrolling

Page 162: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Legacy Scrolling

• Every scroll requires painting

Page 163: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Legacy Scrolling

• Every scroll requires painting• Scrolling happens synchronously

Page 164: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Legacy Scrolling

• Every scroll requires painting• Scrolling happens synchronously•Webpage can interfere with scrolling

Page 165: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Responsive Scrolling

Page 166: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Responsive Scrolling

Page 167: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Responsive Scrolling

[layer setPosition:]

Page 168: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Responsive Scrolling

Page 169: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Responsive Scrolling

Page 170: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Responsive Scrolling

Page 171: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Responsive Scrolling

Layout Paint Layout Paint

Safari

Web Content Process

Main Thread

Event Thread

Scrolling Thread

Main Thread

Threading model

Page 172: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Responsive Scrolling

Layout Paint Layout Paint

Safari

Web Content Process

Main Thread

Event Thread

Scrolling Thread

Main Thread

[layer setPosition:]

Threading model

Page 173: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Special Scrolling Behaviors

Page 174: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Special Scrolling Behaviors

• Fixed position

Page 175: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Special Scrolling Behaviors

• Fixed position■ position:fixed

Page 176: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Fixed Position

Page 177: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Fixed Position

Page 178: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Fixed Position

Page 179: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

#social-links {position: fixed;right: 0;top: 50px;

}

Fixed Position

Page 180: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Fixed Position

#social-links {position: fixed;right: 0;top: 50px;

}

Page 181: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Fixed Position

#social-links {position: fixed;right: 0;top: 50px;

}

Page 182: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Special Scrolling Behaviors

• Fixed position■ position:fixed

Page 183: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Special Scrolling Behaviors

• Fixed position■ position:fixed

• Fixed backgrounds

Page 184: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Special Scrolling Behaviors

• Fixed position■ position:fixed

• Fixed backgrounds■ background-attachment:fixed

Page 185: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Fixed Backgrounds

Page 186: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

body {background-image:url('beach.jpg)';

}

Fixed Backgrounds

Page 187: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Fixed Backgrounds

body {background-image:url('beach.jpg)';

background-attachment: fixed;

}

Page 188: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

body {background-image:url('beach.jpg)';

}

Fixed Backgrounds

body {background-image:url('beach.jpg)';

background-attachment: fixed;

}

Page 189: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

body {background-image:url('beach.jpg)';

}

Fixed Backgrounds

body {background-image:url('beach.jpg)';

background-attachment: fixed;

}

Page 190: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

body {background-image:url('beach.jpg)';

}

Fixed Backgrounds

body {background-image:url('beach.jpg)';

background-attachment: fixed;

}

Page 191: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Fixed Backgrounds

Page 192: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

•Only use on the page background

Fixed Backgrounds

Page 193: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

•Only use on the page background■ On <html> or <body>

Fixed Backgrounds

Page 194: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

•Only use on the page background■ On <html> or <body>■ Backgrounds on just one or the other, not both

Fixed Backgrounds

Page 195: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

•Only use on the page background■ On <html> or <body>■ Backgrounds on just one or the other, not both

Fixed Backgrounds

Page 196: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Fixed BackgroundsCSS 2.1

Page 197: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Fixed BackgroundsCSS 2.1

Page 198: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Responsive Scrolling and Scroll Events

Page 199: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Responsive Scrolling and Scroll Events

• Scroll events are asynchronous

Page 200: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Responsive Scrolling and Scroll Events

• Scroll events are asynchronous• But often used for sticky behavior

Page 201: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 202: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 203: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 204: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with
Page 205: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Special Scrolling Behaviors

• Fixed position■ position:fixed

• Fixed backgrounds■ background-attachment:fixed

Page 206: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Special Scrolling Behaviors

• Fixed position■ position:fixed

• Fixed backgrounds■ background-attachment:fixed

• Sticky position

Page 207: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Sticky Position

Page 208: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Sticky Position

• CSS value:■ position:-webkit-sticky

Page 209: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Sticky Position

• CSS value:■ position:-webkit-sticky

•Use for an element which:■ Takes up space■ Stays in the viewport or scrollable ancestor

Page 210: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Sticky Position

• In-flow positioned■ Like position:relative

Sticky

Page 211: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Sticky Position

• Constrained by viewport■ Like position:fixed

Sticky

Page 212: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Sticky Position

• Constrained bycontaining block

Sticky

Page 213: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Sticky Position

• Constrained bycontaining block

Sticky

Page 214: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Sticky Position

Page 215: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Sticky Position

#side-box {position: -webkit-sticky;top: 10px;

}

Page 216: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Sticky Position

#side-box {position: -webkit-sticky;top: 10px;

}

Page 217: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Sticky Position

#side-box {position: -webkit-sticky;top: 10px;

}

Page 218: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Sticky Position

#side-box {position: -webkit-sticky;top: 10px;

}

Page 219: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Sticky Position

#side-box {position: -webkit-sticky;top: 10px;

}

Page 220: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Sticky Position

Safari 6.1 Safari 7 iOS7

Page 221: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

Page 222: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

• Fixed backgrounds

Page 223: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

• Fixed backgrounds■ Use only on <body>

Page 224: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

• Fixed backgrounds■ Use only on <body>

• Event handlers

Page 225: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

• Fixed backgrounds■ Use only on <body>

• Event handlers■ Avoid mousewheel handlers

Page 226: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

• Fixed backgrounds■ Use only on <body>

• Event handlers■ Avoid mousewheel handlers■ Reduce dependence on scroll events

Page 227: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

• Fixed backgrounds■ Use only on <body>

• Event handlers■ Avoid mousewheel handlers■ Reduce dependence on scroll events

■ Use position:-webkit-sticky

Page 228: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

What You Should Do

• Fixed backgrounds■ Use only on <body>

• Event handlers■ Avoid mousewheel handlers■ Reduce dependence on scroll events

■ Use position:-webkit-sticky

•Don’t rely on overflow:scroll, <frame> or <iframe>

Page 229: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

DemoFast scrolling, sticky position

Tim HortonSafari and WebKit Engineer

Page 230: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Safari Responsive Scrolling

Page 231: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Summary

Page 232: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Summary

Page 233: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Summary

• Safari■ App Nap for Safari tabs■ Power Saver■ Responsive scrolling

Page 234: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Summary

• Safari■ App Nap for Safari tabs■ Power Saver■ Responsive scrolling

•Webpages■ Reduce work in background pages■ Make power-efficient web content■ Get responsive scrolling

Page 235: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

More Information

John GeleynseDirector, Technology [email protected]

DocumentationSafari Dev CenterSafari Extensions Development Guidehttps://developer.apple.com/devcenter/safari/

Apple Developer Forumshttp://devforums.apple.com

Page 236: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Related Sessions

What’s New in Safari and WebKit for Web Developers MissionTuesday 9:00AM

Maximizing Battery Life on OS X MissionTuesday 11:30AM

Improving Power Efficiency with App Nap Pacific HeightsWednesday 10:15AM

Getting to Know Web Inspector Russian HillTuesday 10:15AM

Getting the Most Out of Web Inspector Russian HillTuesday 11:30AM

Page 237: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Labs

Web Content Optimization Lab Media Lab AWednesday 10:15AM

Safari and WebKit Open Lab Media Lab AThursday 9:00AM

Safari and Web Tools Lab Media Lab AFriday 2:00PM

Web Content Optimization Lab Media Lab AThursday 12:45AM

Safari and WebKit Open Lab Media Lab AFriday 9:00AM

Page 238: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with

Labs

Web Content Optimization Lab Media Lab AWednesday 10:15AM

Safari and WebKit Open Lab Media Lab AThursday 9:00AM

Safari and Web Tools Lab Media Lab AFriday 2:00PM

Web Content Optimization Lab Media Lab AThursday 12:45AM

Safari and WebKit Open Lab Media Lab AFriday 9:00AM

Page 239: Power and Performance€¦ · 35% Reduction In CPU Power Usage Testing conducted by Apple in June 2013 using production 1.8GHz Intel Core i5-based 13-inch MacBook Air systems with