SlideShare ist ein Scribd-Unternehmen logo
1 von 33
 
Javascript Performance -Optimierung -Optimierung Johannes Weber TM09
Agenda ,[object Object],[object Object],[object Object],[object Object],[object Object]
Was beeinflusst die Performance-   Optimierung? Optimierung? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Performance um jeden Preis? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],<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 ,[object Object],[object Object],[object Object],[object Object]
#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 ,[object Object],[object Object]
#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 ,[object Object],[object Object],[object Object],[object Object]
#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 ,[object Object],[object Object],[object Object],[object Object]
Q & A
 

Weitere ähnliche Inhalte

Mehr von Johannes Weber

AngularJS Munich Meetup #7 - Intro
AngularJS Munich Meetup #7 - IntroAngularJS Munich Meetup #7 - Intro
AngularJS Munich Meetup #7 - IntroJohannes Weber
 
#perfmatters - Optimizing the Critical Rendering Path
#perfmatters - Optimizing the Critical Rendering Path#perfmatters - Optimizing the Critical Rendering Path
#perfmatters - Optimizing the Critical Rendering PathJohannes Weber
 
LeanJS - Lean startup with JavaScript
LeanJS - Lean startup with JavaScriptLeanJS - Lean startup with JavaScript
LeanJS - Lean startup with JavaScriptJohannes 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 #5Johannes Weber
 
A Story about AngularJS modularization development
A Story about AngularJS modularization developmentA Story about AngularJS modularization development
A Story about AngularJS modularization developmentJohannes 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 2014Johannes Weber
 
Updated: Fiese Fallstricke, sexy Strategien
Updated: Fiese Fallstricke, sexy StrategienUpdated: Fiese Fallstricke, sexy Strategien
Updated: Fiese Fallstricke, sexy StrategienJohannes Weber
 
AngularJS with RequireJS
AngularJS with RequireJSAngularJS with RequireJS
AngularJS with RequireJSJohannes 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 StrategienJohannes Weber
 
Facebook, Google, Youtube & co
Facebook, Google, Youtube & coFacebook, Google, Youtube & co
Facebook, Google, Youtube & coJohannes Weber
 
User centered design - Personas
User centered design - PersonasUser centered design - Personas
User centered design - PersonasJohannes Weber
 
Usability Test Inlandsüberweisung
Usability Test InlandsüberweisungUsability Test Inlandsüberweisung
Usability Test InlandsüberweisungJohannes Weber
 
Paper: Steuerung öffentlicher Screens
Paper: Steuerung öffentlicher ScreensPaper: Steuerung öffentlicher Screens
Paper: Steuerung öffentlicher ScreensJohannes Weber
 
Steuerung öffentlicher Screens
Steuerung öffentlicher ScreensSteuerung öffentlicher Screens
Steuerung öffentlicher ScreensJohannes Weber
 
Customer Centered Design
Customer Centered DesignCustomer Centered Design
Customer Centered DesignJohannes Weber
 
Hardware Usability Testing
Hardware Usability TestingHardware Usability Testing
Hardware Usability TestingJohannes Weber
 
Projektmanagement & Innovation
Projektmanagement & InnovationProjektmanagement & Innovation
Projektmanagement & InnovationJohannes Weber
 
Kontinuierliche Integration
Kontinuierliche IntegrationKontinuierliche Integration
Kontinuierliche IntegrationJohannes Weber
 
jQuery - Write less, do more!
jQuery - Write less, do more!jQuery - Write less, do more!
jQuery - Write less, do more!Johannes Weber
 

Mehr von Johannes Weber (20)

AngularJS Munich Meetup #7 - Intro
AngularJS Munich Meetup #7 - IntroAngularJS Munich Meetup #7 - Intro
AngularJS Munich Meetup #7 - Intro
 
#perfmatters - Optimizing the Critical Rendering Path
#perfmatters - Optimizing the Critical Rendering Path#perfmatters - Optimizing the Critical Rendering Path
#perfmatters - Optimizing the Critical Rendering Path
 
LeanJS - Lean startup with JavaScript
LeanJS - Lean startup with JavaScriptLeanJS - Lean startup with JavaScript
LeanJS - Lean startup with JavaScript
 
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
 
A Story about AngularJS modularization development
A Story about AngularJS modularization developmentA Story about AngularJS modularization development
A Story about AngularJS modularization development
 
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
 
Updated: Fiese Fallstricke, sexy Strategien
Updated: Fiese Fallstricke, sexy StrategienUpdated: Fiese Fallstricke, sexy Strategien
Updated: Fiese Fallstricke, sexy Strategien
 
AngularJS with RequireJS
AngularJS with RequireJSAngularJS with RequireJS
AngularJS with RequireJS
 
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
 
Facebook, Google, Youtube & co
Facebook, Google, Youtube & coFacebook, Google, Youtube & co
Facebook, Google, Youtube & co
 
User centered design - Personas
User centered design - PersonasUser centered design - Personas
User centered design - Personas
 
Usability Test Inlandsüberweisung
Usability Test InlandsüberweisungUsability Test Inlandsüberweisung
Usability Test Inlandsüberweisung
 
Paper: Steuerung öffentlicher Screens
Paper: Steuerung öffentlicher ScreensPaper: Steuerung öffentlicher Screens
Paper: Steuerung öffentlicher Screens
 
Steuerung öffentlicher Screens
Steuerung öffentlicher ScreensSteuerung öffentlicher Screens
Steuerung öffentlicher Screens
 
Customer Centered Design
Customer Centered DesignCustomer Centered Design
Customer Centered Design
 
Hardware Usability Testing
Hardware Usability TestingHardware Usability Testing
Hardware Usability Testing
 
Projektmanagement & Innovation
Projektmanagement & InnovationProjektmanagement & Innovation
Projektmanagement & Innovation
 
Kontinuierliche Integration
Kontinuierliche IntegrationKontinuierliche Integration
Kontinuierliche Integration
 
DA praesentation
DA praesentationDA praesentation
DA praesentation
 
jQuery - Write less, do more!
jQuery - Write less, do more!jQuery - Write less, do more!
jQuery - Write less, do more!
 

jQuery Performance

  • 1.  
  • 2. Javascript Performance -Optimierung -Optimierung Johannes Weber TM09
  • 3.
  • 4.
  • 5.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 32. Q & A
  • 33.