SlideShare a Scribd company logo
Performance Checklist: Front End
Side
Ravi Raj
Discuss some development tips thats helpful to
improve performance
 Don't Put CSS in body tag. CSS in the body
can block rendering, especially in IE
 Put only js needed to render the page in the
HEAD section. Everything else can go at the
end of the BODY.
 Scripts block parallel downloads , While a
script is downloading, the browser won't start
any other downloads, even on different
hostnames.
 Avoid Redirect (really harmful as
performance point of view)
 Preload Components :- take advantage of
the time the browser is idle and request
components (like images, styles and scripts)
you'll need in the future.
 Based on a user action you make an
educated guess where the user is headed
next and preload accordingly
 Cookie:- Eliminate unnecessary cookies
 Keep cookie sizes as low as possible to
minimize the impact on the user response
time
 Be mindful of setting cookies at the
appropriate domain level so other sub-
domains are not affected
 Set an Expires date appropriately. An earlier
Expires date or none removes the cookie
sooner, improving the user response time
 DOM manipulations are the slowest
 Never update the DOM
 if that's not possible, at least do it as infrequently as possible. Bunch up
your updates to the DOM and save them for a later time. Realize that it is
not the size of the update but the high frequency of updates that's slow.
Doing appendChild in a loop is updating the DOM frequently. Caching the
markup in a string, and then setting the innerHTML in the end is batching
and updating infrequently. The second is much faster.
 What if you are updating existing elements on the DOM? How
do you keep updates to a minimum when you want to change
style, class names, content and children of a node that already
exists? Simple. Clone the node you want to work with. Now
you will be working with a clone of the real node, and the
cloned node doesn't exist in the DOM. Updating the cloned
node doesn't affect the DOM. When you are done with your
manipulations, replace the original node with the cloned node.
However, note that the performance problems here are
because of the content and rendering reflow that the browser
has to do. You might get similar benefits by simply hiding the
element first, making the changes, and then showing it.
Though I haven't tried this, it should work in theory.
 Keep track of events. For me, this is the worst part of working
with the DOM. This is important because when your
application (or any DOM nodes) are being unloaded or
destroyed, you will have to manually unregister the events
from the nodes BEFORE you destroy the elements. Yes, this is
the garbage collector's job, and that's supposed to be the job
of the environment your code runs in, but guess which browser
is the offender here. Internet Explorer doesn't free all the
references even when the user leaves your web page. Unless
you want your web app to earn the reputation of being
responsible for many a crashed browser, and a horrid
browsing experience for other websites too, count your
references.
 If you are going to iterate through a node list to
attach event handlers, you are probably
wasting processor time. Instead, simply attach
the event handler to some parent of the node
list and read from the event object to know
what was clicked on. You save the cycles
required to iterate over the nodes this way.
 Avoid calls to functions like getElementsBySelector, where there's lot
of DOM walking involved. If you cannot, then make sure you work on
as small an area of the DOM as possible. If your favourite version of
getElementsBySelector lets you send in a root node under which to
search, do that. Otherwise, provide a very high specificity, starting
with a "#someId" so that the function can narrow down the search.
Also, understand how these functions work internally. For example,
you could use a getElementsByClassName to find divs with the
class "foo", and the implementation of getElementsByClassName
will probably be just three lines, However, using
getElementsBySelector("div.foo") will be faster in almost all
frameworks, even though it might have a hundred lines of code in it's
implementation, since it has less DOM walking to do.
 WRONG ( it touches the live DOM each time
through the loop)
 for (var i=0; i < items.length; i++){
 var item = document.createElement("li");

item.appendChild(document.createTextNode("
Option " + i);
 list.appendChild(item);
 }
 RIGHT (create a document fragment as an
intermediate placeholder for the created li
elements and then use that to add all of the
elements to their parent )
 var fragment = document.createDocumentFragment();
 for (var i=0; i < items.length; i++){
 var item = document.createElement("li");
 item.appendChild(document.createTextNode("Option " + i);
 fragment.appendChild(item);
 }
 list.appendChild(fragment);
 IT touches the live DOM only once, on the last
line. Prior to that, the document fragment is
used to hold the intermediate results. Since a
document fragment has no visual
representation, it doesn’t cause reflow when
modified. Document fragments also can’t be
added into the live DOM, so passing it into
appendChild() actually adds all of the
fragment’s children to list rather than the
fragment itself.
 WRONG(code has three style changes…and also three reflows. A
reflow happens with every change in style to this element. If you’re going to
be making a number of changes to an element’s style, it’s best to group
those in a CSS class and then change the class using JavaScript rather
than applying individual style changes manually)
 element.style.backgroundColor = "blue";
 element.style.color = "red";
 element.style.fontSize = "12em";
 RIGHT
 .newStyle {
 background-color: blue;
 color: red;
 font-size: 12em;
 }
 element.className = "newStyle";
 very important to cache results that you
retrieve from the DOM
 document.getElementById("myDiv").style.left
=document.getElementById("myDiv").offsetLeft+document.getElementById("myDiv"
).offsetWidth + "px";
 IT IS WRONG ...
 The three calls to getElementById() here are the problem. Accessing the DOM is
expensive, and this is three DOM calls to access the exact same element. The code
would better be written as such:-
 var myDiv = document.getElementById("myDiv");
 myDiv.style.left = myDiv.offsetLeft + myDiv.offsetWidth + "px";
 HTMLCollection type …
 This is the type of object that is returned from the DOM anytime a
collection of nodes must be represented, and so is the type of the
childNodes property and is the type returned from
getElementsByTagName(). An HTMLCollection may act like an array
in many ways, but it actually is a living, breathing entity that changes
as the DOM structure changes. Every time you access a property on
an HTMLCollection object, it actually queries the DOM for all nodes
matching the original criteria once again.
 WRONG
 var divs =
document.getElementsByTagName("div");
 for (var i=0; i < divs.length; i++){ //infinite loop

document.body.appendChild(document.create
Element("div"));
 }
 This code is an infinite loop because every time a new div
element is added to the document, the divs collection is
updated with that new information. That means that i will
never reach divs.length because divs.length increases by
one every time through the loop. Every time divs.length is
accessed, it collection is updated, making it far more
expensive than accessing a regular array’s length
property. When dealing with HTMLCollection objects, it’s
best to minimize the number of times you access their
properties. You can speed up a loop tremendously by
simply caching the length in a local variable
 RIGHT
 var divs =
document.getElementsByTagName("div");
 for (var i=0, len=divs.length; i < len; i++){ //not
an infinite loop

document.body.appendChild(document.create
Element("div"));
 }
 THANKS
 raviraj4u@in.com

More Related Content

What's hot (18)

PDF
Web development resources brackets
Laurence Svekis ✔
 
PDF
JavaScript - Chapter 11 - Events
WebStackAcademy
 
PDF
How to make Ajax work for you
Simon Willison
 
PDF
Fewd week4 slides
William Myers
 
PPTX
An introduction to Vue.js
TO THE NEW Pvt. Ltd.
 
PDF
JavaScript Best Pratices
ChengHui Weng
 
PDF
10 java script projects full source code
Laurence Svekis ✔
 
PPT
Grails Introduction - IJTC 2007
Guillaume Laforge
 
PDF
lect4
tutorialsruby
 
PPTX
Behat - Drupal South 2018
Berend de Boer
 
PDF
React JS and why it's awesome
Andrew Hull
 
PDF
tut0000021-hevery
tutorialsruby
 
PDF
&lt;img src="../i/r_14.png" />
tutorialsruby
 
PPT
Enhance Web Performance
Adam Lu
 
ODP
Wicket Next (1.4/1.5)
jcompagner
 
PDF
Create a meteor chat app in 30 minutes
Designveloper
 
PDF
Intro to jQuery @ Startup Institute
Rafael Gonzaque
 
PDF
Cutting the Fat
Codemotion
 
Web development resources brackets
Laurence Svekis ✔
 
JavaScript - Chapter 11 - Events
WebStackAcademy
 
How to make Ajax work for you
Simon Willison
 
Fewd week4 slides
William Myers
 
An introduction to Vue.js
TO THE NEW Pvt. Ltd.
 
JavaScript Best Pratices
ChengHui Weng
 
10 java script projects full source code
Laurence Svekis ✔
 
Grails Introduction - IJTC 2007
Guillaume Laforge
 
Behat - Drupal South 2018
Berend de Boer
 
React JS and why it's awesome
Andrew Hull
 
tut0000021-hevery
tutorialsruby
 
&lt;img src="../i/r_14.png" />
tutorialsruby
 
Enhance Web Performance
Adam Lu
 
Wicket Next (1.4/1.5)
jcompagner
 
Create a meteor chat app in 30 minutes
Designveloper
 
Intro to jQuery @ Startup Institute
Rafael Gonzaque
 
Cutting the Fat
Codemotion
 

Similar to Web Performance Tips (20)

PDF
Performance Optimization and JavaScript Best Practices
Doris Chen
 
PDF
How DRY impacts JavaScript performance // Faster JavaScript execution for the...
Mathias Bynens
 
PPTX
Java script performance tips
Shakti Shrestha
 
PDF
DOM Performance (JSNext Bulgaria)
Hristo Chakarov
 
PPTX
Building High Perf Web Apps - IE8 Firestarter
Mithun T. Dhar
 
PPTX
Building High Performance Web Applications and Sites
goodfriday
 
PPTX
jQuery performance best practices by Sameera Thilakasiri
Sameera Thilakasiri
 
PPT
Javascript and Jquery Best practices
Sultan Khan
 
PDF
DOM Enlightenment Exploring JavaScript and the Modern DOM 1st Edition Lindley...
shiehgovan7e
 
PDF
jQuery quick tips
Rochester Oliveira
 
PDF
bawawjspdx082117
Thinkful
 
PPTX
JavaScript front end performance optimizations
Chris Love
 
PDF
Fast mobile web apps
Ivano Malavolta
 
PDF
Build an App with JavaScript & jQuery
Aaron Lamphere
 
KEY
Bcblackpool jquery tips
Jack Franklin
 
PPTX
The Real Story Behind JavaScript Performance on Mobile... Because Science!
Ryan J. Salva
 
PPT
Javascript Experiment
wgamboa
 
PDF
Ajax Performance Tuning and Best Practices
Doris Chen
 
KEY
jQuery: Tips, tricks and hints for better development and Performance
Jonas De Smet
 
PDF
fuser interface-development-using-jquery
Kostas Mavridis
 
Performance Optimization and JavaScript Best Practices
Doris Chen
 
How DRY impacts JavaScript performance // Faster JavaScript execution for the...
Mathias Bynens
 
Java script performance tips
Shakti Shrestha
 
DOM Performance (JSNext Bulgaria)
Hristo Chakarov
 
Building High Perf Web Apps - IE8 Firestarter
Mithun T. Dhar
 
Building High Performance Web Applications and Sites
goodfriday
 
jQuery performance best practices by Sameera Thilakasiri
Sameera Thilakasiri
 
Javascript and Jquery Best practices
Sultan Khan
 
DOM Enlightenment Exploring JavaScript and the Modern DOM 1st Edition Lindley...
shiehgovan7e
 
jQuery quick tips
Rochester Oliveira
 
bawawjspdx082117
Thinkful
 
JavaScript front end performance optimizations
Chris Love
 
Fast mobile web apps
Ivano Malavolta
 
Build an App with JavaScript & jQuery
Aaron Lamphere
 
Bcblackpool jquery tips
Jack Franklin
 
The Real Story Behind JavaScript Performance on Mobile... Because Science!
Ryan J. Salva
 
Javascript Experiment
wgamboa
 
Ajax Performance Tuning and Best Practices
Doris Chen
 
jQuery: Tips, tricks and hints for better development and Performance
Jonas De Smet
 
fuser interface-development-using-jquery
Kostas Mavridis
 
Ad

More from Ravi Raj (8)

PDF
Time series data monitoring at 99acres.com
Ravi Raj
 
PPT
Web application security
Ravi Raj
 
PPT
Code Review
Ravi Raj
 
PPT
PHP Exception handler
Ravi Raj
 
PPT
Is it time to start using HTML 5
Ravi Raj
 
PPT
Character Encoding issue with PHP
Ravi Raj
 
PPT
How PHP Works ?
Ravi Raj
 
ODP
High Performance Web Sites
Ravi Raj
 
Time series data monitoring at 99acres.com
Ravi Raj
 
Web application security
Ravi Raj
 
Code Review
Ravi Raj
 
PHP Exception handler
Ravi Raj
 
Is it time to start using HTML 5
Ravi Raj
 
Character Encoding issue with PHP
Ravi Raj
 
How PHP Works ?
Ravi Raj
 
High Performance Web Sites
Ravi Raj
 
Ad

Recently uploaded (20)

PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Digital Circuits, important subject in CS
contactparinay1
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 

Web Performance Tips

  • 1. Performance Checklist: Front End Side Ravi Raj Discuss some development tips thats helpful to improve performance
  • 2.  Don't Put CSS in body tag. CSS in the body can block rendering, especially in IE
  • 3.  Put only js needed to render the page in the HEAD section. Everything else can go at the end of the BODY.
  • 4.  Scripts block parallel downloads , While a script is downloading, the browser won't start any other downloads, even on different hostnames.
  • 5.  Avoid Redirect (really harmful as performance point of view)
  • 6.  Preload Components :- take advantage of the time the browser is idle and request components (like images, styles and scripts) you'll need in the future.  Based on a user action you make an educated guess where the user is headed next and preload accordingly
  • 7.  Cookie:- Eliminate unnecessary cookies  Keep cookie sizes as low as possible to minimize the impact on the user response time  Be mindful of setting cookies at the appropriate domain level so other sub- domains are not affected  Set an Expires date appropriately. An earlier Expires date or none removes the cookie sooner, improving the user response time
  • 8.  DOM manipulations are the slowest  Never update the DOM  if that's not possible, at least do it as infrequently as possible. Bunch up your updates to the DOM and save them for a later time. Realize that it is not the size of the update but the high frequency of updates that's slow. Doing appendChild in a loop is updating the DOM frequently. Caching the markup in a string, and then setting the innerHTML in the end is batching and updating infrequently. The second is much faster.
  • 9.  What if you are updating existing elements on the DOM? How do you keep updates to a minimum when you want to change style, class names, content and children of a node that already exists? Simple. Clone the node you want to work with. Now you will be working with a clone of the real node, and the cloned node doesn't exist in the DOM. Updating the cloned node doesn't affect the DOM. When you are done with your manipulations, replace the original node with the cloned node. However, note that the performance problems here are because of the content and rendering reflow that the browser has to do. You might get similar benefits by simply hiding the element first, making the changes, and then showing it. Though I haven't tried this, it should work in theory.
  • 10.  Keep track of events. For me, this is the worst part of working with the DOM. This is important because when your application (or any DOM nodes) are being unloaded or destroyed, you will have to manually unregister the events from the nodes BEFORE you destroy the elements. Yes, this is the garbage collector's job, and that's supposed to be the job of the environment your code runs in, but guess which browser is the offender here. Internet Explorer doesn't free all the references even when the user leaves your web page. Unless you want your web app to earn the reputation of being responsible for many a crashed browser, and a horrid browsing experience for other websites too, count your references.
  • 11.  If you are going to iterate through a node list to attach event handlers, you are probably wasting processor time. Instead, simply attach the event handler to some parent of the node list and read from the event object to know what was clicked on. You save the cycles required to iterate over the nodes this way.
  • 12.  Avoid calls to functions like getElementsBySelector, where there's lot of DOM walking involved. If you cannot, then make sure you work on as small an area of the DOM as possible. If your favourite version of getElementsBySelector lets you send in a root node under which to search, do that. Otherwise, provide a very high specificity, starting with a "#someId" so that the function can narrow down the search. Also, understand how these functions work internally. For example, you could use a getElementsByClassName to find divs with the class "foo", and the implementation of getElementsByClassName will probably be just three lines, However, using getElementsBySelector("div.foo") will be faster in almost all frameworks, even though it might have a hundred lines of code in it's implementation, since it has less DOM walking to do.
  • 13.  WRONG ( it touches the live DOM each time through the loop)  for (var i=0; i < items.length; i++){  var item = document.createElement("li");  item.appendChild(document.createTextNode(" Option " + i);  list.appendChild(item);  }
  • 14.  RIGHT (create a document fragment as an intermediate placeholder for the created li elements and then use that to add all of the elements to their parent )  var fragment = document.createDocumentFragment();  for (var i=0; i < items.length; i++){  var item = document.createElement("li");  item.appendChild(document.createTextNode("Option " + i);  fragment.appendChild(item);  }  list.appendChild(fragment);
  • 15.  IT touches the live DOM only once, on the last line. Prior to that, the document fragment is used to hold the intermediate results. Since a document fragment has no visual representation, it doesn’t cause reflow when modified. Document fragments also can’t be added into the live DOM, so passing it into appendChild() actually adds all of the fragment’s children to list rather than the fragment itself.
  • 16.  WRONG(code has three style changes…and also three reflows. A reflow happens with every change in style to this element. If you’re going to be making a number of changes to an element’s style, it’s best to group those in a CSS class and then change the class using JavaScript rather than applying individual style changes manually)  element.style.backgroundColor = "blue";  element.style.color = "red";  element.style.fontSize = "12em";
  • 17.  RIGHT  .newStyle {  background-color: blue;  color: red;  font-size: 12em;  }  element.className = "newStyle";
  • 18.  very important to cache results that you retrieve from the DOM  document.getElementById("myDiv").style.left =document.getElementById("myDiv").offsetLeft+document.getElementById("myDiv" ).offsetWidth + "px";  IT IS WRONG ...  The three calls to getElementById() here are the problem. Accessing the DOM is expensive, and this is three DOM calls to access the exact same element. The code would better be written as such:-  var myDiv = document.getElementById("myDiv");  myDiv.style.left = myDiv.offsetLeft + myDiv.offsetWidth + "px";
  • 19.  HTMLCollection type …  This is the type of object that is returned from the DOM anytime a collection of nodes must be represented, and so is the type of the childNodes property and is the type returned from getElementsByTagName(). An HTMLCollection may act like an array in many ways, but it actually is a living, breathing entity that changes as the DOM structure changes. Every time you access a property on an HTMLCollection object, it actually queries the DOM for all nodes matching the original criteria once again.
  • 20.  WRONG  var divs = document.getElementsByTagName("div");  for (var i=0; i < divs.length; i++){ //infinite loop  document.body.appendChild(document.create Element("div"));  }
  • 21.  This code is an infinite loop because every time a new div element is added to the document, the divs collection is updated with that new information. That means that i will never reach divs.length because divs.length increases by one every time through the loop. Every time divs.length is accessed, it collection is updated, making it far more expensive than accessing a regular array’s length property. When dealing with HTMLCollection objects, it’s best to minimize the number of times you access their properties. You can speed up a loop tremendously by simply caching the length in a local variable
  • 22.  RIGHT  var divs = document.getElementsByTagName("div");  for (var i=0, len=divs.length; i < len; i++){ //not an infinite loop  document.body.appendChild(document.create Element("div"));  }