5 quick javascript performance improvement tips

30
5 Quick JavaScript Performance Improvement Tips 30 January 2014

Upload: troy-miles

Post on 08-May-2015

3.284 views

Category:

Technology


0 download

DESCRIPTION

JavaScript is arguably the most important language in the world. It comes included in nearly every desktop and mobile browser. It powers the client-side of apps like Facebook and GMail. It is the language of choice for mobile development environments like Apccelerator's Titanium and Apache's Cordova (aka Adobe's PhoneGap). It is even on the server now in Node.js. Yet when programmer's run into performance issue with JavaScript their first inclination is to blame its interpreted nature, not realizing that simple changes in the structure of their code can result in sometimes significant improvements in performance. In this session I will show five quick changes you can make to your JavaScript code to improve its performance and explain why they work.

TRANSCRIPT

Page 1: 5 Quick JavaScript Performance Improvement Tips

5 Quick JavaScript Performance

Improvement Tips30 January 2014

Page 2: 5 Quick JavaScript Performance Improvement Tips

Twitter handle: @therockncoder

Page 3: 5 Quick JavaScript Performance Improvement Tips

The Rock n Coder

• http://therockncoder.blogspot.com

• http://www.youtube.com/user/rockncoder

• https://github.com/Rockncoder

• http://www.slideshare.net/rockncoder

Page 4: 5 Quick JavaScript Performance Improvement Tips

Our Agenda

• Why Performance Matters

• 5 Performance Tips

• Resources

• Summary

Page 5: 5 Quick JavaScript Performance Improvement Tips

Why Performance Matters?

Page 6: 5 Quick JavaScript Performance Improvement Tips

Why Performance Matters?

• 47% of consumers expect a 2 second page load

• 40% abandon a page that takes 3 seconds or more to load

• A 1 second delay in page response can result in a 7% reduction in conversions

Page 7: 5 Quick JavaScript Performance Improvement Tips

If an e-commerce site is making $100,000 a day in sales, a 1 second

page delay could potentially cost you $2.5 million in lost sales every year

Page 8: 5 Quick JavaScript Performance Improvement Tips

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up

our opportunities in that critical 3%. D. Knuth

Page 9: 5 Quick JavaScript Performance Improvement Tips

5 Performance Tips

Page 10: 5 Quick JavaScript Performance Improvement Tips

Tip #5 Use as few files as

possible

Page 11: 5 Quick JavaScript Performance Improvement Tips

Use as few files as possible

• Browser can load multiple files at a time

• But only one JS file at a time

• Concatenate multiple JS file into one

• Compress JS files

• Prefer JS at the bottom of the HTML file

Page 12: 5 Quick JavaScript Performance Improvement Tips

Tip #4 Prefer local variables

Page 13: 5 Quick JavaScript Performance Improvement Tips

Prefer local variables

• Variables in scope found quicker

• JS search local scope, then global

• with creates a new scope level ahead of local

• closures also create new scope level

Page 14: 5 Quick JavaScript Performance Improvement Tips

Var Scoping Demo

Page 15: 5 Quick JavaScript Performance Improvement Tips

Prefer local variables

• Property chains similar to var scoping

• Objects closer in the chain found quicker

Page 16: 5 Quick JavaScript Performance Improvement Tips

Property Chain Demo

Page 17: 5 Quick JavaScript Performance Improvement Tips

Tip #3 Reduce the work done

in loops

Page 18: 5 Quick JavaScript Performance Improvement Tips

Reduce the work done in loops

• No real speed difference between: for, while and do_while

• Avoid for_in

• (Watch library based for_each)

Page 19: 5 Quick JavaScript Performance Improvement Tips

Tip #2 Watch your plugins

Page 20: 5 Quick JavaScript Performance Improvement Tips

Watch your plugins

• Know what your plugins do

• Be sure to evaluate different plugins

• Example jQuery.js or Zepto.js

Page 21: 5 Quick JavaScript Performance Improvement Tips

jQuery Demo

Page 22: 5 Quick JavaScript Performance Improvement Tips

Tip #1 Avoid the DOM

Page 23: 5 Quick JavaScript Performance Improvement Tips

Avoid the DOM

• The DOM is REALLY Slow

• Avoid accessing it when possible

• Do work offline then update DOM

Page 24: 5 Quick JavaScript Performance Improvement Tips

DOM Access Demo

Page 25: 5 Quick JavaScript Performance Improvement Tips

Resources

Page 26: 5 Quick JavaScript Performance Improvement Tips

Resources

• Microsoft Ajax Minifierhttp://ajaxmin.codeplex.com/

• Google Closure Toolshttps://developers.google.com/closure/

• Yahoo YSlow http://developer.yahoo.com/yslow/

Page 27: 5 Quick JavaScript Performance Improvement Tips

Books

• High Performance JavaScriptNicholas C. Zakas

• JavaScript NinjaJohn Resig & Bear Bibeaults

• JavaScript: The Good PartsDouglas Crockford

Page 28: 5 Quick JavaScript Performance Improvement Tips

Summary

Page 29: 5 Quick JavaScript Performance Improvement Tips

Summary

• Avoid the DOM

• Watch your plugins

• Reduce the work done in loops

• Prefer local variables

• Use as few files as possible

Page 30: 5 Quick JavaScript Performance Improvement Tips

The Rock n Coder

• http://therockncoder.blogspot.com

• http://www.youtube.com/user/rockncoder

• https://github.com/Rockncoder

• http://www.slideshare.net/rockncoder