SlideShare a Scribd company logo
Performance optimization 2
ID meeting
13 th July 2010

Filip Mares
Agenda
•Why does it matter?

•Browsers and JavaScript
•Loading and execution
•Write efficient JavaScript code
•Responsive interfaces
Why does it matter?
• Fast Internet connections - people have come to expect speed
• More JavaScript code than ever before - download and execute
even more code as you interact
• Rich Web 2.0 applications based on AJAX - Applications stay
open for a long time (Gmail, Facebook…)
Browsers and JavaScript
Browsers and Optimizing Engines
• Chrome
V8

• Safari 4+
Nitro
• Firefox 3.5+
TraceMonkey

• Opera 10.5+
Carakan
• IE9
Chakra

V8 Benchmark Suite v5 score
6000
5000
4000
3000

2000
1000
0
Browser limits
• JavaScript is still single-threaded
• Call stack controls how many functions can be executed in a single
process
• Runaway Script timer limits the amount of time a script is allowed to
execute
• Errors occur when call stack size or Execution time or Number of
statements are exceeded
• Behaviour and limits are browser specific
• Avoid too much DOM interaction, too much recursion, long-running
loops
Loading and execution
Optimize loading and execution
• Put Scripts at the bottom of the page
• Combine, Minify and Gzip Scripts
• Download JavaScript in a nonblocking fashion
• Dynamically create <script> elements to download and execute the code
• Download the JavaScript code using an XHR object, and then inject the code
into the page

• If you need inline script at the top of the page,
put it before the stylesheets
Write efficient JavaScript
Data access & scope management
• Accessing data from a literal or a local variable is fastest
• The deeper the object property, the longer it takes to retrieve
• The further into the scope chain, the slower the resolution
• Use closures sparingly - calling the closure means three objects
in the scope chain (minimum)
• Avoid the with statement, be careful with try-catch
• Avoid using eval()
Never pass a string to the setTimeout and setInterval functions.
Instead, pass an anonymous function
DOM
• Minimize DOM access, perform DOM manipulations off the document
• Use local variables to store DOM references you’ll access repeatedly
• Be careful using HTMLCollection objects, make a copy into an array
for heavy work on collections
• Batch CSS changes to minimize repaint/reflow
• Use classNames instead of inline styles except for animations
• Position absolutely during animations
• Use event delegation to minimize the number of event handlers
Flow control
• Ensure that the most common conditions are first
• For a large number of discrete values to test use a Lookup table
• The for, while and do-while loops have the same performance impact
• Avoid the for-in loop unless you need to iterate over a number of
unknown object properties
• To improve loop performance decrease the amount of work done per
iteration and decrease number of iterations
• Use recursion carefully to avoid stack overflow error
Loops easy fixes for speed
//for loop

var len = items.length;

for (var i=0; i < items.length; i++) {
process(items[i]);
}

//for loop
for (var i=len; i--;) {
process(items[i]);

//do-while loop

}

var j=0;
do {

//do-while loop
process(items[j]);

} while (j < items.length);

var j=len - 1;
do {
process(items[j]);

//while loop

} while (j--);

var k=0;
while (k < items.length) {
process(items[k++]);
}

//while loop
var k=len;
while (k--) {
process(items[k]);
}
Strings and regular expressions
• Optimize string concatenation – use array joining when concatenating
numerous or large strings

• Don’t use the regex constructor, use regular expression literals instead
• Backtracking is a frequent source of regex inefficiency – test regex
on strings of varying lengths that match, don’t mach and nearly match
• Split complex regexes into simpler pieces
• Regexes are not always the best tool for the job
Ajax
• Avoid using synchronous XMLHttpRequest
• Make Ajax Cacheable
• Use GET for Ajax Requests
• Favour JSON over XML as your data exchange format
Responsive interfaces
Responsible User Interface
• No JavaScript task should take longer than 100ms to execute
• Browsers behave differently in response to user interaction during
JavaScript execution
• Timers can be used to schedule code for later execution
• Web workers are a feature in newer browsers that allow you to
execute JavaScript code outside the UI thread
Questions?
Thanks
for listening
Links
•Best Practices for Speeding Up Your Web Site
http://developer.yahoo.com/performance/rules.html

•NCZOnline blog
http://www.nczonline.net/blog/

•High Performance Web Sites blog
http://www.stevesouders.com/blog/

•Web Performance Best Practices
http://code.google.com/speed/page-speed/docs/rules_intro.html

More Related Content

PPT
Performance optimization - Advanced techniques
PPT
Performance optimization - Basics
PPTX
Performance Web
PDF
Client Side Optimization
PPT
Web performance Talk
KEY
Web Optimization Level: Paranoid
PDF
Performance Testing w/ WebPage Test Private Instance (DrupalCamp Ohio)
PPTX
Website performance optimization QA
Performance optimization - Advanced techniques
Performance optimization - Basics
Performance Web
Client Side Optimization
Web performance Talk
Web Optimization Level: Paranoid
Performance Testing w/ WebPage Test Private Instance (DrupalCamp Ohio)
Website performance optimization QA

What's hot (20)

PPTX
NGINX for Application Delivery & Acceleration
PPTX
Advance java session 15
PDF
Web performance mercadolibre - ECI 2013
PPTX
The server side story: Parallel and Asynchronous programming in .NET - ITPro...
PPT
MongoDB at community engine
PPT
Node js
PPTX
Silverstripe at scale - design & architecture for silverstripe applications
PPTX
Benchmarking NGINX for Accuracy and Results
PPTX
Advance java session 20
PPTX
Getting Started with Web Services
PDF
ASP.NET Scalability - WebDD
PPTX
Windows Azure Service Bus
PPTX
Level Up: 5 Expert Tips for Optimizing WordPress Performance
PPTX
Why Wordnik went non-relational
PDF
Supporting large scale React applications
PDF
Building a better web
PDF
ASP.NET Scalability - VBUG London
PDF
CFWheels - Pragmatic, Beautiful Code
PPTX
Building large scalable mission critical business applications on the web
KEY
MongoDB London PHP
NGINX for Application Delivery & Acceleration
Advance java session 15
Web performance mercadolibre - ECI 2013
The server side story: Parallel and Asynchronous programming in .NET - ITPro...
MongoDB at community engine
Node js
Silverstripe at scale - design & architecture for silverstripe applications
Benchmarking NGINX for Accuracy and Results
Advance java session 20
Getting Started with Web Services
ASP.NET Scalability - WebDD
Windows Azure Service Bus
Level Up: 5 Expert Tips for Optimizing WordPress Performance
Why Wordnik went non-relational
Supporting large scale React applications
Building a better web
ASP.NET Scalability - VBUG London
CFWheels - Pragmatic, Beautiful Code
Building large scalable mission critical business applications on the web
MongoDB London PHP
Ad

Viewers also liked (6)

PPTX
Let’s practice critical thinking
PDF
Proofread symbols
PPT
Anatomie mobilního webu
PPTX
Komponenty v kaskákdě
PPT
HTML5 Geolocation API
PDF
77094122 report-on-aarong-part-two
Let’s practice critical thinking
Proofread symbols
Anatomie mobilního webu
Komponenty v kaskákdě
HTML5 Geolocation API
77094122 report-on-aarong-part-two
Ad

Similar to Performance optimization - JavaScript (20)

PPTX
Java script performance tips
PPTX
Javascript best practices
PDF
High Performance JavaScript Build Faster Web Application Interfaces 1st Editi...
PPT
Ajax Performance
PDF
Performance Optimization and JavaScript Best Practices
PPT
High Performance Ajax Applications 1197671494632682 2
PPT
High Performance Ajax Applications
PPTX
30 JavaScript optimization tips
KEY
Developing High Performance Web Apps - CodeMash 2011
PDF
Front-end optimisation & jQuery Internals (Pycon)
PDF
Client Side Performance In Web Applications
PDF
Ajax Performance Tuning and Best Practices
PPT
Web performance essentials - Goodies
PDF
Performance Improvements In Browsers
PDF
Performance Improvements in Browsers
PDF
Developing High Performance Web Apps
PDF
Ajaxworld07
PDF
Ajaxworld07
KEY
A rough guide to JavaScript Performance
PPT
Javascript and Jquery Best practices
Java script performance tips
Javascript best practices
High Performance JavaScript Build Faster Web Application Interfaces 1st Editi...
Ajax Performance
Performance Optimization and JavaScript Best Practices
High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications
30 JavaScript optimization tips
Developing High Performance Web Apps - CodeMash 2011
Front-end optimisation & jQuery Internals (Pycon)
Client Side Performance In Web Applications
Ajax Performance Tuning and Best Practices
Web performance essentials - Goodies
Performance Improvements In Browsers
Performance Improvements in Browsers
Developing High Performance Web Apps
Ajaxworld07
Ajaxworld07
A rough guide to JavaScript Performance
Javascript and Jquery Best practices

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
KodekX | Application Modernization Development
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPT
Teaching material agriculture food technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
cuic standard and advanced reporting.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
MYSQL Presentation for SQL database connectivity
Machine learning based COVID-19 study performance prediction
Building Integrated photovoltaic BIPV_UPV.pdf
Unlocking AI with Model Context Protocol (MCP)
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The AUB Centre for AI in Media Proposal.docx
Reach Out and Touch Someone: Haptics and Empathic Computing
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Big Data Technologies - Introduction.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
KodekX | Application Modernization Development
NewMind AI Weekly Chronicles - August'25 Week I
Teaching material agriculture food technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
Network Security Unit 5.pdf for BCA BBA.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
cuic standard and advanced reporting.pdf
Empathic Computing: Creating Shared Understanding
MYSQL Presentation for SQL database connectivity

Performance optimization - JavaScript

  • 1. Performance optimization 2 ID meeting 13 th July 2010 Filip Mares
  • 2. Agenda •Why does it matter? •Browsers and JavaScript •Loading and execution •Write efficient JavaScript code •Responsive interfaces
  • 3. Why does it matter? • Fast Internet connections - people have come to expect speed • More JavaScript code than ever before - download and execute even more code as you interact • Rich Web 2.0 applications based on AJAX - Applications stay open for a long time (Gmail, Facebook…)
  • 5. Browsers and Optimizing Engines • Chrome V8 • Safari 4+ Nitro • Firefox 3.5+ TraceMonkey • Opera 10.5+ Carakan • IE9 Chakra V8 Benchmark Suite v5 score 6000 5000 4000 3000 2000 1000 0
  • 6. Browser limits • JavaScript is still single-threaded • Call stack controls how many functions can be executed in a single process • Runaway Script timer limits the amount of time a script is allowed to execute • Errors occur when call stack size or Execution time or Number of statements are exceeded • Behaviour and limits are browser specific • Avoid too much DOM interaction, too much recursion, long-running loops
  • 8. Optimize loading and execution • Put Scripts at the bottom of the page • Combine, Minify and Gzip Scripts • Download JavaScript in a nonblocking fashion • Dynamically create <script> elements to download and execute the code • Download the JavaScript code using an XHR object, and then inject the code into the page • If you need inline script at the top of the page, put it before the stylesheets
  • 10. Data access & scope management • Accessing data from a literal or a local variable is fastest • The deeper the object property, the longer it takes to retrieve • The further into the scope chain, the slower the resolution • Use closures sparingly - calling the closure means three objects in the scope chain (minimum) • Avoid the with statement, be careful with try-catch • Avoid using eval() Never pass a string to the setTimeout and setInterval functions. Instead, pass an anonymous function
  • 11. DOM • Minimize DOM access, perform DOM manipulations off the document • Use local variables to store DOM references you’ll access repeatedly • Be careful using HTMLCollection objects, make a copy into an array for heavy work on collections • Batch CSS changes to minimize repaint/reflow • Use classNames instead of inline styles except for animations • Position absolutely during animations • Use event delegation to minimize the number of event handlers
  • 12. Flow control • Ensure that the most common conditions are first • For a large number of discrete values to test use a Lookup table • The for, while and do-while loops have the same performance impact • Avoid the for-in loop unless you need to iterate over a number of unknown object properties • To improve loop performance decrease the amount of work done per iteration and decrease number of iterations • Use recursion carefully to avoid stack overflow error
  • 13. Loops easy fixes for speed //for loop var len = items.length; for (var i=0; i < items.length; i++) { process(items[i]); } //for loop for (var i=len; i--;) { process(items[i]); //do-while loop } var j=0; do { //do-while loop process(items[j]); } while (j < items.length); var j=len - 1; do { process(items[j]); //while loop } while (j--); var k=0; while (k < items.length) { process(items[k++]); } //while loop var k=len; while (k--) { process(items[k]); }
  • 14. Strings and regular expressions • Optimize string concatenation – use array joining when concatenating numerous or large strings • Don’t use the regex constructor, use regular expression literals instead • Backtracking is a frequent source of regex inefficiency – test regex on strings of varying lengths that match, don’t mach and nearly match • Split complex regexes into simpler pieces • Regexes are not always the best tool for the job
  • 15. Ajax • Avoid using synchronous XMLHttpRequest • Make Ajax Cacheable • Use GET for Ajax Requests • Favour JSON over XML as your data exchange format
  • 17. Responsible User Interface • No JavaScript task should take longer than 100ms to execute • Browsers behave differently in response to user interaction during JavaScript execution • Timers can be used to schedule code for later execution • Web workers are a feature in newer browsers that allow you to execute JavaScript code outside the UI thread
  • 20. Links •Best Practices for Speeding Up Your Web Site http://developer.yahoo.com/performance/rules.html •NCZOnline blog http://www.nczonline.net/blog/ •High Performance Web Sites blog http://www.stevesouders.com/blog/ •Web Performance Best Practices http://code.google.com/speed/page-speed/docs/rules_intro.html