dom performance (jsnext bulgaria)

55
Nov 23, 2014 Nov 23, 2014 Sofia var title = “DOM Performance”; var info = { name: “Hristo Chakarov”, otherOptional: undefined };

Upload: hristo-chakarov

Post on 02-Jul-2015

2.036 views

Category:

Technology


1 download

DESCRIPTION

In the client-side development we manipulate the DOM very often. Some of these manipulations take some time to execute, others take even more time. In a complicated JS-based software, it is very important to find the optimal approaches in order to get best performance. We will meet few very common cases, where standard DOM manipulations are very expensive, and we will see what is the optimal way to achieve our goal.

TRANSCRIPT

Page 1: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Nov 23, 2014Sofia

var title = “DOM Performance”;

var info = { name: “Hristo Chakarov”, otherOptional: undefined };

Page 2: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

agenda();

• Why performance is so important • Event delegation • Stylesheet manipulation • DOM creation • DOM destruction • Questions

Page 3: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

PerformanceWhy so important?

Page 4: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Page 5: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Why performance matters?

Good performance improves User Experience (UX) • Slow performance is very obvious • Users don’t enjoy waiting

Page 6: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Page 7: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

DOM operations are heavy

• createElement is most expensive • The more elements you need to operate

with, the worse performance you get o Event handling o Styling

Page 8: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Event DelegationWhat is it & how it affects performance

Page 9: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Page 10: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Handling events on a single element

var cell = document.querySelector('td');cell.addEventListener( 'click', function ( event ) { // mark the cell in blue this.style.background = 'blue';});

Page 11: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Handling events on multiple elements

// imagine that we have 30’000 or more <td> elements…var cells = document.querySelectorAll('td');// create single event handlervar handler = function ( event ) { // mark the cell in blue this.style.background = 'blue';};

for ( var i=0, l=cells.length; i<l; i += 1 ) { // add listener to every single cell... cells[i].addEventListener( 'click', handler );}

Page 12: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Delegate?

Page 13: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Event delegation

Event delegation allows you to avoid adding event listeners to specific nodes; instead, the event listener is added to one parent.

Page 14: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Delegate event

// delegate click event to the tablevar table = document.querySelector('table');table.addEventListener( 'click', function ( event ) { // `this` is the whole <table>, but `event.target` is the cell that has been clicked event.target.style.background = 'blue';});

Page 15: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Delegate event

// delegate click event to the tablevar table = document.querySelector('table');table.addEventListener( 'click', function ( event ) { // `this` is the whole <table>, but `event.target` //is the element that has been clicked // find out the <td> element var cell = event.target;

while ( cell && cell.tagName != 'TD' && cell != this ) { cell = cell.parentNode; } cell.style.background = 'blue';});

Page 16: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Event delegation performance results

http://jsperf.com/delegate-event

Page 17: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Event delegation - pros & cons

• Drastically increases performance • It’s alive! Elements about to be appended

later automagically delegate the event to the parent

• Not suitable for some kinds of events o mouseenter/mouseleave o mouseover/mouseout

Page 18: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

StylingHow can we style thousands of elements without reducing

the performance?

Page 19: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Page 20: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Styling elements

document.querySelector('tr').style.height = '75px';

// what if we have 100’000 rows?[].slice.call( document.querySelectorAll('tr')).forEach( function ( tr ) { tr.style.height = '75px';});

Page 21: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Page 22: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Using a style sheet

document.head.appendChild(document.createElement('style')

).innerHTML = 'tr { height: 75px }';

Page 23: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Stylesheet API

// get the styleSheet objectvar sheet = document.querySelector('style').sheet;

// insert new CSS rule at the bottom of the stylesheetsheet.insertRule( 'tr { height: 75px }', sheet.cssRules.length );

// delete the top CSS rulesheet.deleteRule( 0 );

Page 24: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Still supporting IE8? No problem!

https://github.com/ickata/utilities/blob/master/JavaScript/Source/stylesheet-api.js

Page 25: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Styling performance results

http://jsperf.com/stylesheet-api

Page 26: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Stylesheet API - pros & cons

• Drastically increases performance • It’s alive! Elements about to be appended

later automagically inherit styles • Not cross-browser

o insertRule/addRule o deleteRule/removeRule o cssRules/rules o innerHTML/styleSheet.cssText

Page 27: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

DOM creationInteractive SPA requires a lot of new DOM elements

Page 28: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

createElement vs cloneNode vs innerHTML

• createElement is the slowest DOM operation

• cloning a single node or a structure via cloneNode is much faster

• old-school innerHTML is much faster as well o innerHTML is not approved by W3, but all

browsers support it

Page 29: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

DOM creation performance results

http://jsperf.com/dom-creation-4

Page 30: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Page 31: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Can we get even better results?

Page 32: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

DocumentFragment

DocumentFragment is a lightweight container that can hold DOM nodes. Various operations, such as inserting nodes as children of another Node, may take DocumentFragment objects as arguments; this results in all the child nodes of the DocumentFragment being moved to the child list of this node.

Page 33: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Clone a DocumentFragment

If we append 10 elements to a DocumentFragment, and then append to the same fragment a clone of it, as a result the fragment will contain 20 elements.

If we repeat the fragment clone/append operations 2 more times - we will get a fragment with a total of 80 elements!

Page 34: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Cloning fragments dramatically reduces the # of operations!

Page 35: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Clone fragments algorithm

• Create desired number of elements using DocumentFragment & cloneNode

• Use less operations as possible

Page 36: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Clone fragments algorithm

• Reduce the total # of desired elements by division of 2 until we get odd number or the simple numbers 2 or 3

• Subtract 1 from odd numbers & continue division by 2 unless result is 2 or 3. Store the step index for later (when we will have to add +1)

Page 37: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Clone fragments algorithm

• Create 2 or 3 clones of the source element • Multiply by 2 until we reach the desired

number of cloned elements • Add +1 to the result on each stored step

Page 38: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Clone fragments algorithm example

1000 = ((((3 * 2 + 1) * 2 + 1) * 2 + 1) * 2 * 2 + 1) * 2 * 2 * 2

Page 39: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

cloneMultiple

https://github.com/ickata/utilities/blob/master/JavaScript/Source/element-clone-multiple.js

Page 40: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

cloneNode vs cloneMultiple

Page 41: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

cloneNode vs cloneMultiple - Chrome

Page 42: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

cloneNode vs cloneMultiple - Firefox

Page 43: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

cloneNode vs cloneMultiple - IE8

Page 44: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

cloneMultiple - summary

• Makes sense to use for large amount of elements

• Reduces # of DOM operations to clone elements

• The more elements we clone, the less # of operations we have! o to clone 1000 elements, we need only 33

operations! o to clone 2000 - we need only 35!

Page 45: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Page 46: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

DOM destruction

Page 47: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Destroying elements

// remove a single elementparentNode.removeChild( element );// remove all descendants parentNode.innerHTML = ""; // wow.. that’s heavy

Page 48: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Destroying elements

• innerHTML destroys all elements one by one

• removeChild destroys the element with all its descendants

• design your system in a way so when you need to remove multiple elements you remove their parent o that’s a single operation!

Page 49: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Resources

Page 50: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Resources

• DocumentFragment • Performance Tests

o Event Delegation o Stylesheet Manipulation o DOM Creation

• DOM Performance (BG)

Page 51: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Page 52: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Questions?

Page 53: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Upcoming events

ISTA Conference 26-27 November http://istabg.org/

Stay tuned for 2015:Azure Bootcamp http://azure-camp.eu/

UXify Bulgaria http://uxify.org/

SQLSaturday https://www.sqlsaturday.com/

and more js.next();

Page 54: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

Thanks to our Sponsors:

Diamond Sponsor:

Gold Sponsors:

Swag Sponsors:

Media Partners:

Silver Sponsors:

Hosting partner:

Technological Partners:

Bronze Sponsors:

Page 55: DOM Performance (JSNext Bulgaria)

Nov 23, 2014

• Hristo Chakarov • Front-end architect • SiteKreator

• JavaScript trainer • IT Academy

• Photographer • @github • +HristoChakarov

Thanks for listening!