SlideShare a Scribd company logo
 
Javascript Performance -Optimierung -Optimierung Johannes Weber TM09
Agenda Was beeinflusst die Performance-Optimierung? Lookups einsparen Loop optimierung Event delegation Performance Tipps
Was beeinflusst die Performance-   Optimierung? Optimierung? Performance-Optimierungen sind stets fallspezifisch MacBook Pro Leopard 2.5GHz Intel Core2 Duo 2GB RAM Firefox 3.5.5 Firebug Für jede Optimierung sind Verbesserungen im Millisekundenbereich möglich
Performance um jeden Preis? Performance vs. Lesbarkeit Datenintegrität Wartbarkeit Wiederverwendbarkeit Entwicklungsgeschwindigkeit
Lookups einsparen
Lookups einsparen (Nativ) document.getElementById('example').innerHTML = 'HTML INHALT';   document.getElementById('example').style.color = '#123456';   document.getElementById('example').style.height = '20px';   var exampleElement = document.getElementById('example');   exampleElement.style.height = '20px';   exampleElement.style.color = '#123456';   exampleElement.innerHTML = 'HTML INHALT';
Lookups einsparen (Nativ)
Lookups einsparen (jQuery) $('.example').css('height', '20px');   $('.example').css('color', '#123456');   $('.example').html('HTML INHALT');   var $exampleElement = $('.example');   $exampleElement.css({   'color': '#123456',   'height': '20px'   })   $exampleElement.html('HTML INHALT');
Lookups einsparen (jQuery)
Loop Optimierung
Loop-Optimierung 1 var anArray = ['Foo', 'Bar', 'Moo', 'Cow'];   for (var i = 0; i < anArray.length; i++) {   var currentElement = anArray[i];   }   var anArray = ['Foo', 'Bar', 'Moo', 'Cow'];   var anArrayLength = anArray.length;   for (var i = 0; i <  anArrayLength ; i++) {   var currentElement = anArray[i]; }
Loop-Optimierung 1
Loop-Optimierung 2 var anArray = ['Foo', 'Bar', 'Moo', 'Cow'];   var anArrayLength = anArray.length;   for (var i = 0; i < anArrayLength; i++) {   var currentElement = anArray[i];   } var anArray = ['Foo', 'Bar', 'Moo', 'Cow'];   var anArrayLength = anArray.length;   var currentElement;   for (var i = 0; i < anArrayLength; i++) {   currentElement  = anArray[i];   }
Loop-Optimierung 2
Loop-Optimierung 3 var anArray = ['Foo', 'Bar', 'Moo', 'Cow'];   var anArrayLength = anArray.length;   var currentElement;   for (var i = 0; i < anArrayLength; i++) {   currentElement = anArray[i];   } var anArray = ['Foo', 'Bar', 'Moo', 'Cow'];   var anArrayLength = anArray.length;   var currentElement; for (var i = 0; i < anArrayLength;  ++i ) {   currentElement = anArray[i];   }
Post- und Pre-Inkrementierung // Post-Inkrementierung   var i = 1;   var z = i++; // z = 1, i = 2   // Pre-Inkrementierung   var i = 1;   var z = ++i; // z = 2, i = 2
Loop-Optimierung 3 var anArray = ['Foo', 'Bar', 'Moo', 'Cow'];   var anArrayLength = anArray.length;   var currentElement;   for (var i = 0; i < anArrayLength; i++) {   currentElement = anArray[i];   } var anArray = ['Foo', 'Bar', 'Moo', 'Cow'];   var anArrayLength = anArray.length;   var currentElement;   for (var i = 0; i < anArrayLength;  ++i ) {   currentElement = anArray[i];   }
Loop-Optimierung 3
Event Delegation
Event Delegation Binden von Events an Elternelemente der konkreten Event-Ziele Einsatzgebiet: Tabellarische Darstellungen wie Excel, Kalender, ... <ul>   <li>Element 1</li>   <li>Element 2</li>   <li>Element 3</li>   ...   </ul>
Event Delegation $('li').bind('click', function (event) {   var $this = $(this);   $('div').html($this.html());   });   $('ul').bind('click', function (event) {   var $target = $(event.originalTarget);   $('div').html($target.html());   });
Event Delegation Bindungsdauer
Performance Tipps
#id vs .class CSS 1 - 3 Selektoren - Sizzle CSS Selector Engine verlockend durch kurze Schreibweise $('#id') -> document.getElementById() $('.class') -> durchsuchen jedes DOM Elementes
#id vs .class var d = new Date();console.info(&quot;Class Test:&quot;);console.info(&quot;-- Start: &quot; + d.getSeconds() + &quot; &quot; + d.getMilliseconds());var testBody = &quot;&quot;;for (var i=0; i<1000; i++){ testBody += &quot;<div class='testable&quot;+i+&quot;'>&quot;;}$(&quot;body&quot;).append(testBody);for (var i=0; i<1000; i++){ $(&quot;.testable&quot;+i);}// ----------------------------------var d = new Date();console.info(&quot;-- End: &quot; + d.getSeconds() + &quot; &quot; + d.getMilliseconds());console.info(&quot;-- ID Test&quot;);console.info(&quot;-- Start: &quot; + d.getSeconds() + &quot; &quot; + d.getMilliseconds());testBody = &quot;&quot;;for (var i=0; i<1000; i++){ testBody += &quot;<div id='testable&quot;+i+&quot;'>&quot;;}$(&quot;body&quot;).append(testBody);for (var i=0; i<1000; i++){ $(&quot;#testable&quot;+i);}var d = new Date();console.info(&quot;-- End: &quot; + d.getSeconds() + &quot; &quot; + d.getMilliseconds());console.info(&quot;End Test&quot;);
#id vs .class nur soviele Suchinformationen wie nötig! Beispiel: Formular validierung
#id vs .class // 50 INPUT Elemente müssen vor Absenden des Formulares validiert werden // <input type=text class=&quot;notBlank&quot;> // der „ schlechte “ Weg $(&quot; .notBlank &quot;).each(function(){ if ($(this).val()==&quot;&quot;) $(this).addClass(&quot;error&quot;); }); // der „ gute “ Weg $(&quot; input.notBlank &quot;).each(function(){ if ($(this).val()==&quot;&quot;) $(this).addClass(&quot;error&quot;); }); // der „ beste “ weg $(&quot; input:text.notBlank &quot;).each(function(){ if ($(this).val()==&quot;&quot;) $(this).addClass(&quot;error&quot;); });
Selectoren Cachen $() -> jQuery Object 1 Aufruf -> kein caching > 1 Aufrufe -> caching! Beispiel: Formular validierung
#id vs .class $(&quot;#ourHideButton&quot;).click(function(){ $(&quot;.hideable&quot;).hide(); }); var  hideable ; $(&quot;#ourHideButton&quot;).click(function(){ if ( hideable ) hideable .hide(); else hideable  = $(&quot;.hideable&quot;).hide(); });
Tipps & Tricks Google Page Speed YSlow JS compressor Google closure-compiler
Q & A
 

More Related Content

More from Johannes Weber (20)

AngularJS Munich Meetup #7 - Intro
AngularJS Munich Meetup #7 - IntroAngularJS Munich Meetup #7 - Intro
AngularJS Munich Meetup #7 - Intro
Johannes Weber
 
#perfmatters - Optimizing the Critical Rendering Path
#perfmatters - Optimizing the Critical Rendering Path#perfmatters - Optimizing the Critical Rendering Path
#perfmatters - Optimizing the Critical Rendering Path
Johannes Weber
 
LeanJS - Lean startup with JavaScript
LeanJS - Lean startup with JavaScriptLeanJS - Lean startup with JavaScript
LeanJS - Lean startup with JavaScript
Johannes Weber
 
The evolution of Angular 2 @ AngularJS Munich Meetup #5
The evolution of Angular 2 @ AngularJS Munich Meetup #5The evolution of Angular 2 @ AngularJS Munich Meetup #5
The evolution of Angular 2 @ AngularJS Munich Meetup #5
Johannes Weber
 
A Story about AngularJS modularization development
A Story about AngularJS modularization developmentA Story about AngularJS modularization development
A Story about AngularJS modularization development
Johannes Weber
 
Debugging War Stories & Strategies to Survive on RejectJS 2014
Debugging War Stories & Strategies to Survive on RejectJS 2014Debugging War Stories & Strategies to Survive on RejectJS 2014
Debugging War Stories & Strategies to Survive on RejectJS 2014
Johannes Weber
 
Updated: Fiese Fallstricke, sexy Strategien
Updated: Fiese Fallstricke, sexy StrategienUpdated: Fiese Fallstricke, sexy Strategien
Updated: Fiese Fallstricke, sexy Strategien
Johannes Weber
 
AngularJS with RequireJS
AngularJS with RequireJSAngularJS with RequireJS
AngularJS with RequireJS
Johannes Weber
 
Responsive Webdesign: Fiese Fallstricke und sexy Strategien
Responsive Webdesign: Fiese Fallstricke und sexy StrategienResponsive Webdesign: Fiese Fallstricke und sexy Strategien
Responsive Webdesign: Fiese Fallstricke und sexy Strategien
Johannes Weber
 
Facebook, Google, Youtube & co
Facebook, Google, Youtube & coFacebook, Google, Youtube & co
Facebook, Google, Youtube & co
Johannes Weber
 
User centered design - Personas
User centered design - PersonasUser centered design - Personas
User centered design - Personas
Johannes Weber
 
Usability Test Inlandsüberweisung
Usability Test InlandsüberweisungUsability Test Inlandsüberweisung
Usability Test Inlandsüberweisung
Johannes Weber
 
Paper: Steuerung öffentlicher Screens
Paper: Steuerung öffentlicher ScreensPaper: Steuerung öffentlicher Screens
Paper: Steuerung öffentlicher Screens
Johannes Weber
 
Steuerung öffentlicher Screens
Steuerung öffentlicher ScreensSteuerung öffentlicher Screens
Steuerung öffentlicher Screens
Johannes Weber
 
Customer Centered Design
Customer Centered DesignCustomer Centered Design
Customer Centered Design
Johannes Weber
 
Hardware Usability Testing
Hardware Usability TestingHardware Usability Testing
Hardware Usability Testing
Johannes Weber
 
Projektmanagement & Innovation
Projektmanagement & InnovationProjektmanagement & Innovation
Projektmanagement & Innovation
Johannes Weber
 
Kontinuierliche Integration
Kontinuierliche IntegrationKontinuierliche Integration
Kontinuierliche Integration
Johannes Weber
 
jQuery - Write less, do more!
jQuery - Write less, do more!jQuery - Write less, do more!
jQuery - Write less, do more!
Johannes Weber
 
AngularJS Munich Meetup #7 - Intro
AngularJS Munich Meetup #7 - IntroAngularJS Munich Meetup #7 - Intro
AngularJS Munich Meetup #7 - Intro
Johannes Weber
 
#perfmatters - Optimizing the Critical Rendering Path
#perfmatters - Optimizing the Critical Rendering Path#perfmatters - Optimizing the Critical Rendering Path
#perfmatters - Optimizing the Critical Rendering Path
Johannes Weber
 
LeanJS - Lean startup with JavaScript
LeanJS - Lean startup with JavaScriptLeanJS - Lean startup with JavaScript
LeanJS - Lean startup with JavaScript
Johannes Weber
 
The evolution of Angular 2 @ AngularJS Munich Meetup #5
The evolution of Angular 2 @ AngularJS Munich Meetup #5The evolution of Angular 2 @ AngularJS Munich Meetup #5
The evolution of Angular 2 @ AngularJS Munich Meetup #5
Johannes Weber
 
A Story about AngularJS modularization development
A Story about AngularJS modularization developmentA Story about AngularJS modularization development
A Story about AngularJS modularization development
Johannes Weber
 
Debugging War Stories & Strategies to Survive on RejectJS 2014
Debugging War Stories & Strategies to Survive on RejectJS 2014Debugging War Stories & Strategies to Survive on RejectJS 2014
Debugging War Stories & Strategies to Survive on RejectJS 2014
Johannes Weber
 
Updated: Fiese Fallstricke, sexy Strategien
Updated: Fiese Fallstricke, sexy StrategienUpdated: Fiese Fallstricke, sexy Strategien
Updated: Fiese Fallstricke, sexy Strategien
Johannes Weber
 
AngularJS with RequireJS
AngularJS with RequireJSAngularJS with RequireJS
AngularJS with RequireJS
Johannes Weber
 
Responsive Webdesign: Fiese Fallstricke und sexy Strategien
Responsive Webdesign: Fiese Fallstricke und sexy StrategienResponsive Webdesign: Fiese Fallstricke und sexy Strategien
Responsive Webdesign: Fiese Fallstricke und sexy Strategien
Johannes Weber
 
Facebook, Google, Youtube & co
Facebook, Google, Youtube & coFacebook, Google, Youtube & co
Facebook, Google, Youtube & co
Johannes Weber
 
User centered design - Personas
User centered design - PersonasUser centered design - Personas
User centered design - Personas
Johannes Weber
 
Usability Test Inlandsüberweisung
Usability Test InlandsüberweisungUsability Test Inlandsüberweisung
Usability Test Inlandsüberweisung
Johannes Weber
 
Paper: Steuerung öffentlicher Screens
Paper: Steuerung öffentlicher ScreensPaper: Steuerung öffentlicher Screens
Paper: Steuerung öffentlicher Screens
Johannes Weber
 
Steuerung öffentlicher Screens
Steuerung öffentlicher ScreensSteuerung öffentlicher Screens
Steuerung öffentlicher Screens
Johannes Weber
 
Customer Centered Design
Customer Centered DesignCustomer Centered Design
Customer Centered Design
Johannes Weber
 
Hardware Usability Testing
Hardware Usability TestingHardware Usability Testing
Hardware Usability Testing
Johannes Weber
 
Projektmanagement & Innovation
Projektmanagement & InnovationProjektmanagement & Innovation
Projektmanagement & Innovation
Johannes Weber
 
Kontinuierliche Integration
Kontinuierliche IntegrationKontinuierliche Integration
Kontinuierliche Integration
Johannes Weber
 
jQuery - Write less, do more!
jQuery - Write less, do more!jQuery - Write less, do more!
jQuery - Write less, do more!
Johannes Weber
 

jQuery Performance

  • 1.  
  • 2. Javascript Performance -Optimierung -Optimierung Johannes Weber TM09
  • 3. Agenda Was beeinflusst die Performance-Optimierung? Lookups einsparen Loop optimierung Event delegation Performance Tipps
  • 4. Was beeinflusst die Performance- Optimierung? Optimierung? Performance-Optimierungen sind stets fallspezifisch MacBook Pro Leopard 2.5GHz Intel Core2 Duo 2GB RAM Firefox 3.5.5 Firebug Für jede Optimierung sind Verbesserungen im Millisekundenbereich möglich
  • 5. Performance um jeden Preis? Performance vs. Lesbarkeit Datenintegrität Wartbarkeit Wiederverwendbarkeit Entwicklungsgeschwindigkeit
  • 7. Lookups einsparen (Nativ) document.getElementById('example').innerHTML = 'HTML INHALT'; document.getElementById('example').style.color = '#123456'; document.getElementById('example').style.height = '20px'; var exampleElement = document.getElementById('example'); exampleElement.style.height = '20px'; exampleElement.style.color = '#123456'; exampleElement.innerHTML = 'HTML INHALT';
  • 9. Lookups einsparen (jQuery) $('.example').css('height', '20px'); $('.example').css('color', '#123456'); $('.example').html('HTML INHALT'); var $exampleElement = $('.example'); $exampleElement.css({ 'color': '#123456', 'height': '20px' }) $exampleElement.html('HTML INHALT');
  • 12. Loop-Optimierung 1 var anArray = ['Foo', 'Bar', 'Moo', 'Cow']; for (var i = 0; i < anArray.length; i++) { var currentElement = anArray[i]; } var anArray = ['Foo', 'Bar', 'Moo', 'Cow']; var anArrayLength = anArray.length; for (var i = 0; i < anArrayLength ; i++) { var currentElement = anArray[i]; }
  • 14. Loop-Optimierung 2 var anArray = ['Foo', 'Bar', 'Moo', 'Cow']; var anArrayLength = anArray.length; for (var i = 0; i < anArrayLength; i++) { var currentElement = anArray[i]; } var anArray = ['Foo', 'Bar', 'Moo', 'Cow']; var anArrayLength = anArray.length; var currentElement; for (var i = 0; i < anArrayLength; i++) { currentElement = anArray[i]; }
  • 16. Loop-Optimierung 3 var anArray = ['Foo', 'Bar', 'Moo', 'Cow']; var anArrayLength = anArray.length; var currentElement; for (var i = 0; i < anArrayLength; i++) { currentElement = anArray[i]; } var anArray = ['Foo', 'Bar', 'Moo', 'Cow']; var anArrayLength = anArray.length; var currentElement; for (var i = 0; i < anArrayLength; ++i ) { currentElement = anArray[i]; }
  • 17. Post- und Pre-Inkrementierung // Post-Inkrementierung var i = 1; var z = i++; // z = 1, i = 2 // Pre-Inkrementierung var i = 1; var z = ++i; // z = 2, i = 2
  • 18. Loop-Optimierung 3 var anArray = ['Foo', 'Bar', 'Moo', 'Cow']; var anArrayLength = anArray.length; var currentElement; for (var i = 0; i < anArrayLength; i++) { currentElement = anArray[i]; } var anArray = ['Foo', 'Bar', 'Moo', 'Cow']; var anArrayLength = anArray.length; var currentElement; for (var i = 0; i < anArrayLength; ++i ) { currentElement = anArray[i]; }
  • 21. Event Delegation Binden von Events an Elternelemente der konkreten Event-Ziele Einsatzgebiet: Tabellarische Darstellungen wie Excel, Kalender, ... <ul> <li>Element 1</li> <li>Element 2</li> <li>Element 3</li> ... </ul>
  • 22. Event Delegation $('li').bind('click', function (event) { var $this = $(this); $('div').html($this.html()); }); $('ul').bind('click', function (event) { var $target = $(event.originalTarget); $('div').html($target.html()); });
  • 25. #id vs .class CSS 1 - 3 Selektoren - Sizzle CSS Selector Engine verlockend durch kurze Schreibweise $('#id') -> document.getElementById() $('.class') -> durchsuchen jedes DOM Elementes
  • 26. #id vs .class var d = new Date();console.info(&quot;Class Test:&quot;);console.info(&quot;-- Start: &quot; + d.getSeconds() + &quot; &quot; + d.getMilliseconds());var testBody = &quot;&quot;;for (var i=0; i<1000; i++){ testBody += &quot;<div class='testable&quot;+i+&quot;'>&quot;;}$(&quot;body&quot;).append(testBody);for (var i=0; i<1000; i++){ $(&quot;.testable&quot;+i);}// ----------------------------------var d = new Date();console.info(&quot;-- End: &quot; + d.getSeconds() + &quot; &quot; + d.getMilliseconds());console.info(&quot;-- ID Test&quot;);console.info(&quot;-- Start: &quot; + d.getSeconds() + &quot; &quot; + d.getMilliseconds());testBody = &quot;&quot;;for (var i=0; i<1000; i++){ testBody += &quot;<div id='testable&quot;+i+&quot;'>&quot;;}$(&quot;body&quot;).append(testBody);for (var i=0; i<1000; i++){ $(&quot;#testable&quot;+i);}var d = new Date();console.info(&quot;-- End: &quot; + d.getSeconds() + &quot; &quot; + d.getMilliseconds());console.info(&quot;End Test&quot;);
  • 27. #id vs .class nur soviele Suchinformationen wie nötig! Beispiel: Formular validierung
  • 28. #id vs .class // 50 INPUT Elemente müssen vor Absenden des Formulares validiert werden // <input type=text class=&quot;notBlank&quot;> // der „ schlechte “ Weg $(&quot; .notBlank &quot;).each(function(){ if ($(this).val()==&quot;&quot;) $(this).addClass(&quot;error&quot;); }); // der „ gute “ Weg $(&quot; input.notBlank &quot;).each(function(){ if ($(this).val()==&quot;&quot;) $(this).addClass(&quot;error&quot;); }); // der „ beste “ weg $(&quot; input:text.notBlank &quot;).each(function(){ if ($(this).val()==&quot;&quot;) $(this).addClass(&quot;error&quot;); });
  • 29. Selectoren Cachen $() -> jQuery Object 1 Aufruf -> kein caching > 1 Aufrufe -> caching! Beispiel: Formular validierung
  • 30. #id vs .class $(&quot;#ourHideButton&quot;).click(function(){ $(&quot;.hideable&quot;).hide(); }); var hideable ; $(&quot;#ourHideButton&quot;).click(function(){ if ( hideable ) hideable .hide(); else hideable = $(&quot;.hideable&quot;).hide(); });
  • 31. Tipps & Tricks Google Page Speed YSlow JS compressor Google closure-compiler
  • 32. Q & A
  • 33.  
  翻译: