128
Timers, Power Consumpti and Performance flickr.com/photos/barbourians/ 6662357209/ Nicholas C. Zakas Chief Architect, WellFurnished

JavaScript Timers, Power Consumption, and Performance

Embed Size (px)

DESCRIPTION

E

Citation preview

Page 1: JavaScript Timers, Power Consumption, and Performance

Timers, Power Consumption, and Performance

flickr.com/photos/barbourians/6662357209/

Nicholas C. ZakasChief Architect, WellFurnished

Page 2: JavaScript Timers, Power Consumption, and Performance

New

Page 3: JavaScript Timers, Power Consumption, and Performance

@slicknet(Complaints:@souders)

Page 4: JavaScript Timers, Power Consumption, and Performance

flickr.com/photos/jepoirrier/954701212/

UI Thread

Update UI Execute JavaScript

Page 5: JavaScript Timers, Power Consumption, and Performance

flickr.com/photos/55733754@N00/3325000738/

Page 6: JavaScript Timers, Power Consumption, and Performance

<button id="btn" style="font-size: 30px; padding: 0.5em 1em">Click Me</button>

<script>

window.onload = function(){

document.getElementById("btn").onclick = function(){

//do something

};

};

</script>

Page 7: JavaScript Timers, Power Consumption, and Performance

Before ClickUI Thread

UI Queuetime

Page 8: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclick

Page 9: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclickDraw down state

Page 10: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclick

Page 11: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI Update UIonclick

Draw up state

Page 12: JavaScript Timers, Power Consumption, and Performance

No UI updates while JavaScript is executing

Page 13: JavaScript Timers, Power Consumption, and Performance

JavaScript May Cause UI Update<button id="btn" style="font-size: 30px; padding: 0.5em

1em">Click Me</button>

<script>

window.onload = function(){

document.getElementById("btn").onclick = function(){

var div = document.createElement("div");

div.className = "tip";

div.innerHTML = "You clicked me!";

document.body.appendChild(div);

};

};

</script>

Page 14: JavaScript Timers, Power Consumption, and Performance

Each UI update applies

ALL CHANGESsince the last UI update

Page 15: JavaScript Timers, Power Consumption, and Performance

I gonna make a

namination!!

flickr.com/photos/oakleyoriginals/3065393607/

Page 16: JavaScript Timers, Power Consumption, and Performance

function naminate(element){

// start here

element.style.left = "10px";

// move to here

element.style.left = "30px";

// then to here

element.style.left = "50px";

// finally to here

element.style.left = "70px";

}

Page 17: JavaScript Timers, Power Consumption, and Performance
Page 18: JavaScript Timers, Power Consumption, and Performance
Page 19: JavaScript Timers, Power Consumption, and Performance

Why you no work???

flickr.com/photos/tudor/318123668/

Page 20: JavaScript Timers, Power Consumption, and Performance

function namimate(element){

// start here

element.style.left = "10px";

// move to here

element.style.left = "30px";

// then to here

element.style.left = "50px";

// finally to here

element.style.left = "70px";

}

Last state wins

Page 21: JavaScript Timers, Power Consumption, and Performance
Page 22: JavaScript Timers, Power Consumption, and Performance

setTimeout()

Page 23: JavaScript Timers, Power Consumption, and Performance

var tId = setTimeout(function(){

// do something

}, 1500);

// optional

clearTimeout(tId)

Code to execute

Delay in milliseconds

Page 24: JavaScript Timers, Power Consumption, and Performance

setTimeout()DOES NOT SAY

“Run this code after this delay”

Page 25: JavaScript Timers, Power Consumption, and Performance

setTimeout()DOES SAY

“Add this code to the queue after this delay”

Page 26: JavaScript Timers, Power Consumption, and Performance

<button id="btn" style="font-size: 30px; padding: 0.5em 1em">Click Me</button>

<script>

window.onload = function(){

document.getElementById("btn").onclick = function(){

setTimeout(function() {

//do something

}, 25);

};

};

</script>

Page 27: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclick

Page 28: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclickDraw down state

Page 29: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclick

Page 30: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI Update UI

Draw up state

onclick

Page 31: JavaScript Timers, Power Consumption, and Performance

After 25msUI Thread

UI Queuetime

Update UI Update UIonclick

JavaScript

Added to back of queue

Page 32: JavaScript Timers, Power Consumption, and Performance

FutureUI Thread

UI Queuetime

Update UI Update UIonclick JavaScript

Page 33: JavaScript Timers, Power Consumption, and Performance

FutureUI Thread

UI Queuetime

Update UI Update UIonclick JavaScript

Update UI

If the JavaScript changes the UI, it triggers another

update

Page 34: JavaScript Timers, Power Consumption, and Performance

setTimeout() sends your code into the future

Page 35: JavaScript Timers, Power Consumption, and Performance

setTimeout(function(){

element.style.left = "10px";

}, 50);

setTimeout(function(){

element.style.left = "30px";

}, 100);

setTimeout(function(){

element.style.left = "50px";

}, 150);

setTimeout(function(){

element.style.left = "70px";

}, 200);

Page 36: JavaScript Timers, Power Consumption, and Performance
Page 37: JavaScript Timers, Power Consumption, and Performance

(function(){

var delay = 100;

function moveTheThing(){

// actually move the thing

setTimeout(moveTheThing, delay);

}

setTimeout(moveTheThing, delay);

}());

Animation Loop

Page 38: JavaScript Timers, Power Consumption, and Performance

(function(){

var msg = "Some reasonably long text that keeps scrolling.",

len = 25,

pos = 0,

padding = msg.replace(/./g, " ").substr(0,len)

finalMsg = padding + msg,

delay = 100;

function updateText(){

var curMsg = finalMsg.substr(pos++, len);

window.status = curMsg;

if (pos == finalMsg.length){

pos = 0;

}

setTimeout(updateText, delay);

}

setTimeout(updateText, delay);

}());

Page 39: JavaScript Timers, Power Consumption, and Performance

And More!

Page 40: JavaScript Timers, Power Consumption, and Performance

setTimout() all the things!!!

Page 41: JavaScript Timers, Power Consumption, and Performance

Problems

Page 42: JavaScript Timers, Power Consumption, and Performance

15.6

Page 43: JavaScript Timers, Power Consumption, and Performance

The default system-wide timer resolution in Windows is 15.6 ms, which means that every 15.6 ms the operating system receives a clock interrupt from the system timer hardware.

-Timers, Timer Resolution, and Development of Efficient Code (Microsoft)

Page 44: JavaScript Timers, Power Consumption, and Performance

var tId = setTimeout(function(){

// do something

}, 10);

What does it mean?

Page 45: JavaScript Timers, Power Consumption, and Performance

http://ejohn.org/blog/accuracy-of-javascript-time/

Page 46: JavaScript Timers, Power Consumption, and Performance
Page 47: JavaScript Timers, Power Consumption, and Performance

60

Page 48: JavaScript Timers, Power Consumption, and Performance
Page 49: JavaScript Timers, Power Consumption, and Performance

(function(){

var delay = 17;

function moveTheThing(){

// actually move the thing

setTimeout(moveTheThing, delay);

}

setTimeout(moveTheThing, delay);

}());

Animation Loop

Pretty please?!?!

Page 50: JavaScript Timers, Power Consumption, and Performance

http://ejohn.org/blog/analyzing-timer-performance/

Page 51: JavaScript Timers, Power Consumption, and Performance

Jumpy animationsInaccurate benchmarks

Page 52: JavaScript Timers, Power Consumption, and Performance
Page 53: JavaScript Timers, Power Consumption, and Performance

1ms all the timers!!!

Page 54: JavaScript Timers, Power Consumption, and Performance

http://www.belshe.com/2010/06/04/chrome-cranking-up-the-clock/

Page 55: JavaScript Timers, Power Consumption, and Performance

timeBeginPeriod()

Page 56: JavaScript Timers, Power Consumption, and Performance

timeBeginPeriod(1)

Page 57: JavaScript Timers, Power Consumption, and Performance

setTimout() all the things!!!

Page 58: JavaScript Timers, Power Consumption, and Performance

flickr.com/photos/antonfomkin/3046849320/

Page 59: JavaScript Timers, Power Consumption, and Performance

Modern CPUs are narcoleptic

Page 60: JavaScript Timers, Power Consumption, and Performance

ActiveC0

C1C2

C3

x86 CPU States

Low Power

HaltStop-Clock

SleepDeep Sleep

Enhanced Deep SleepDeep Power Down

C4C5C6

Page 61: JavaScript Timers, Power Consumption, and Performance

http://software.intel.com/en-us/articles/cpu-power-utilization-on-intel-architectures/

Page 62: JavaScript Timers, Power Consumption, and Performance

CPUs go to sleep when idle

Page 63: JavaScript Timers, Power Consumption, and Performance

The default timer resolution on Windows 7 is 15.6 milliseconds (ms). Some applications reduce this to 1 ms, which reduces the battery run time on mobile systems by as much as 25 percent.

-Timers, Timer Resolution, and Development of Efficient Code (Microsoft)

Laptops!

Page 64: JavaScript Timers, Power Consumption, and Performance
Page 65: JavaScript Timers, Power Consumption, and Performance

Plugged In 4ms 4ms 4ms 4ms 4ms

Battery 4ms 15.6ms 4ms 15.6ms 4ms

Background 1s 1s 4ms 1s* 4ms

* Internet Explorer 10

Web Timer Resolution Today

Page 66: JavaScript Timers, Power Consumption, and Performance
Page 67: JavaScript Timers, Power Consumption, and Performance

Battery 4ms 10ms 10ms 10ms 4ms

Background Tab - 10ms 10ms 10ms 1s

Web Timer Resolution Today

Background App - * * * 1s

* “Catches up” when switched back

Page 68: JavaScript Timers, Power Consumption, and Performance

Experiment• Hard shutdown and restart so

no other apps are running• Turn off brightness auto-adjust• Turn off screen locking• Leave WiFi/Mobile on• Load test page in browser• Profit!

Page 69: JavaScript Timers, Power Consumption, and Performance

Experiment• Test single timer at different

intervals:• 1000ms – 10ms

Page 70: JavaScript Timers, Power Consumption, and Performance

Low Frequency >= 1000ms

High Frequency < 1000ms

Page 71: JavaScript Timers, Power Consumption, and Performance

Time For 10% Power UseBy Frequency

Minutes 52-56 42-48 62-65

Page 72: JavaScript Timers, Power Consumption, and Performance

Timer frequency doesn’t matter

Page 73: JavaScript Timers, Power Consumption, and Performance

http://googlecode.blogspot.com/2009/07/gmail-for-mobile-html5-series-using.html

Page 74: JavaScript Timers, Power Consumption, and Performance

Experiment• Test single timer at different

intervals:• 1000ms – 10ms

• Test multiple timers at different intervals• 1000ms – 10ms x 10

Page 75: JavaScript Timers, Power Consumption, and Performance

Time For 10% Power UseBy Count

Minutes 52-56 42-48 62-65

(same)

Page 76: JavaScript Timers, Power Consumption, and Performance

Number of timers doesn’t matter

Page 77: JavaScript Timers, Power Consumption, and Performance
Page 78: JavaScript Timers, Power Consumption, and Performance

Number of timers does matter(accuracy)

Page 79: JavaScript Timers, Power Consumption, and Performance

http://ejohn.org/apps/timers

Page 80: JavaScript Timers, Power Consumption, and Performance

http://ejohn.org/apps/timers

Page 81: JavaScript Timers, Power Consumption, and Performance

http://ejohn.org/apps/timers

Page 82: JavaScript Timers, Power Consumption, and Performance

Flooding the QueueUI Thread

UI Queuetime

timer

timer

timer

Page 83: JavaScript Timers, Power Consumption, and Performance

Too many timers affects rendering

Page 84: JavaScript Timers, Power Consumption, and Performance

Elsewhere…

Page 85: JavaScript Timers, Power Consumption, and Performance

http://www.w3.org/TR/css3-animations/

Page 86: JavaScript Timers, Power Consumption, and Performance

Optimized animations using CSS

Page 87: JavaScript Timers, Power Consumption, and Performance

div {

animation-name: diagonal-slide;

animation-duration: 5s;

animation-iteration-count: 10;

}

@keyframes diagonal-slide {

from {

left: 0;

top: 0;

}

to {

left: 100px;

top: 100px;

}

}

Hey browser! I’m animating!

Page 88: JavaScript Timers, Power Consumption, and Performance

var tId = setTimeout(function(){

// do something

}, 1500);

Hey browser!

I want to do something

later

Could be animation. Could be polling.

Don’t sweat it.

Page 89: JavaScript Timers, Power Consumption, and Performance

http://www.w3.org/2010/webperf/

Page 90: JavaScript Timers, Power Consumption, and Performance

http://www.w3.org/TR/animation-timing/

moz webkit

Page 91: JavaScript Timers, Power Consumption, and Performance

var rId = requestAnimationFrame(function(time){

// do something

});

// optional

clearAnimationFrame(rId)

Code to execute

Time when the paint will happen

Page 92: JavaScript Timers, Power Consumption, and Performance

(function(){

function moveTheThing(){

// actually move the thing

requestAnimationFrame(moveTheThing);

}

requestAnimationFrame(moveTheThing);

}());

New Animation Loop

Hey browser! I’m animating!

Page 93: JavaScript Timers, Power Consumption, and Performance

(function(){

var element = document.getElementById("box");

function moveTheThing(){

element.style.left = (element.offsetLeft + 5) + "px";

requestAnimationFrame(moveTheThing);

}

requestAnimationFrame(moveTheThing);

}());

New Animation Loop

Page 94: JavaScript Timers, Power Consumption, and Performance

(function(){

var element = document.getElementById("box"),

start = Date.now();

function moveTheThing(time){

var since = (time || Date.now()) – start;

element.style.left = (element.offsetLeft + since)+ "px";

requestAnimationFrame(moveTheThing);

}

requestAnimationFrame(moveTheThing);

}());

New Animation Loop

Page 95: JavaScript Timers, Power Consumption, and Performance

<button id="btn" style="font-size: 30px; padding: 0.5em 1em">Click Me</button>

<script>

window.onload = function(){

document.getElementById("btn").onclick = function(){

requestAnimationFrame(function() {

//do something

});

};

};

</script>

Page 96: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclick

Page 97: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclickDraw down state

Page 98: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclick

Page 99: JavaScript Timers, Power Consumption, and Performance

Call to requestAnimationFrame()UI Thread

UI Queuetime

Update UI

Update UI

onclick

Changes

Anim Frame

Page 100: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI Update UI

Draw up state

onclick

Changes

Anim Frame

Page 101: JavaScript Timers, Power Consumption, and Performance

Before Next FrameUI Thread

UI Queuetime

Update UI Update UIonclick

Anim Frame

Changes

Page 102: JavaScript Timers, Power Consumption, and Performance

Naminate!UI Thread

UI Queuetime

Update UI Update UIonclick Changes Anim Frame

Draw whatever changes are necessary

Page 103: JavaScript Timers, Power Consumption, and Performance

[requestAnimationFrame()’s framerate is] capped at 1000/(16 + N) fps, where N is the number of ms it takes your callback to execute. If your callback takes 1000ms to execute, then it's capped at under 1fps. If your callback takes 1ms to execute, you get about 60fps.

-Boris Zbarsky (Mozilla) via Paul Irish (Google)

Page 104: JavaScript Timers, Power Consumption, and Performance

Animate all the things!!!…with CSS and requestAnimationFrame

Page 105: JavaScript Timers, Power Consumption, and Performance

What about other things?

Page 106: JavaScript Timers, Power Consumption, and Performance

https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html

Page 107: JavaScript Timers, Power Consumption, and Performance

var iId = setImmediate(function(){

// do something

});

// optional

clearImmediate(iId)

Code to execute

msSetImmediate()msClearImmediate()

Page 108: JavaScript Timers, Power Consumption, and Performance

<button id="btn" style="font-size: 30px; padding: 0.5em 1em">Click Me</button>

<script>

window.onload = function(){

document.getElementById("btn").onclick = function(){

setImmediate(function() {

//do something

});

};

};

</script>

Page 109: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclick

Page 110: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclickDraw down state

Page 111: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclick

Page 112: JavaScript Timers, Power Consumption, and Performance

Call to setImmediate()UI Thread

UI Queuetime

Update UI

Update UI

onclick

ChangesAlways added after the last UI update in

the queue

Page 113: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI Update UI

Draw up state

onclick

Changes

Page 114: JavaScript Timers, Power Consumption, and Performance

Immediately!UI Thread

UI Queuetime

Update UI Update UIonclick Changes

Page 115: JavaScript Timers, Power Consumption, and Performance

https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html

ON HOLD!

Page 116: JavaScript Timers, Power Consumption, and Performance

http://www.w3.org/TR/workers/

Page 117: JavaScript Timers, Power Consumption, and Performance

//in page

var worker = new Worker("process.js");

worker.onmessage = function(event){

useData(event.data);

};

worker.postMessage(values);

Page 118: JavaScript Timers, Power Consumption, and Performance

//in process.js

self.onmessage = function(event){

var items = event.data;

for (var i=0,len=items.length; i < len; i++){

process(items[i]);

}

self.postMessage(items);

};

Page 119: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclick

Page 120: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclickDraw down state

Page 121: JavaScript Timers, Power Consumption, and Performance

When ClickedUI Thread

UI Queuetime

Update UI

Update UI

onclick

Page 122: JavaScript Timers, Power Consumption, and Performance

Create Web WorkerUI Thread

UI Queuetime

Update UI

Update UI

onclick

Creates a background

thread/process/etc.

Page 123: JavaScript Timers, Power Consumption, and Performance

postMessage()UI Thread

UI Queuetime

Update UI Update UIonclick

process

Page 124: JavaScript Timers, Power Consumption, and Performance

Worker CompleteUI Thread

UI Queuetime

Update UI Update UIonclick

onmessage

Page 125: JavaScript Timers, Power Consumption, and Performance

FutureUI Thread

UI Queuetime

Update UI Update UIonclick onmessage

Page 126: JavaScript Timers, Power Consumption, and Performance

Recommendations

Page 127: JavaScript Timers, Power Consumption, and Performance

• Use as few as necessary• If multiple are necessary, use a single timer that can

accommodate allTimers

• Use CSS transitions and animations first• If not possible, use requestAnimationFrame()Animation

• Use web workers for efficient data processing• If no other options, use timers (see first point)Other

Page 128: JavaScript Timers, Power Consumption, and Performance

EtceteraMy company: • wellfurnished.com

My blog: • nczonline.net

Twitter • @slicknet

These Slides: • slideshare.net/nzakas