SlideShare a Scribd company logo
1 of 7
Download to read offline
XYZ- Javascript Best Practices
For maximizing Front End performance
Avoid inefficient jQuery selectors.................................................................................................................2
Cache selected elements ..............................................................................................................................2
Use delegate() when possible.......................................................................................................................3
Use efficient event delegation......................................................................................................................3
Minimize DOM manipulation........................................................................................................................4
Avoid global variables...................................................................................................................................4
Use Object literals.........................................................................................................................................4
Do not pass strings to setTimeout() or setInterval().....................................................................................5
Optimize string concatenation......................................................................................................................5
De-reference unused objects........................................................................................................................5
Avoid loops if possible, but when needed ensure they are efficient ...........................................................6
Prohibited functions .....................................................................................................................................6
Do not mix CSS with JavaScript.....................................................................................................................6
Optimize Browser Layout Calculation...........................................................................................................7
Validating Code.............................................................................................................................................7
Avoid inefficient jQuery selectors
Too general
Use the closest persistent ancestor with an ID to look up an element instead of just a class, which
requires traversing the entire DOM.
$('.className') // Avoid this, it’s slow!
$(‘#someAncestor’).find(‘.className’); // Use this
cachedElement.find(‘.className’); // Or this
Pseudo selectors
These are the slowest selectors to use, there is usually a better way to do what you’re trying to. Avoid
them at all costs.
$('.overlay:hidden') // this takes 200ms in IE8
Attribute selectors
These are also very slow, use a class instead if it is available.
$('select[name=attribute]') // Avoid this
Finding elements with id's within other elements
When an element has an ID, it is always faster to simply select it than searching within another
container.
$('#element).find('#anotherElement') // Avoid this
self.cache.body.find(‘#anotherElement’) // Avoid this
$(‘#anotherElement’) // Use this
Looping elements
Use the good ol’ basic FOR instead of the $.each for looping. It has been proved that the Jquery loop is
slower in performance - http://jsperf.com/jquery-each-vs-for-loop/6
$.each(a, function() {
e = this;
}); // Avoid this
for (var i = 0, len = a.length; i < len; i++) {
e = a[i];
}; // Use this
Cache selected elements
Selecting the same element multiple times causes unnecessary DOM traversal, instead cache the
element in a variable and reuse it.
/* Avoid this */
$(‘#element’).addClass(‘foo’);
// do some stuff
$(‘#element’).removeClass(‘bar’);
/* Use this instead */
var myElement = $(‘#element’);
myElement.addClass(‘foo’);
// do some stuff
myElement.removeClass(‘bar’);
Use delegate() when possible
When multiple elements within a persistent container trigger the same action, use delegate() instead of
bind()
<ul id=”myList”>
<li><a href=””>Link 1</a></li>
<li><a href=””>Link 2</a></li>
<li><a href=””>Link 3</a></li>
<li><a href=””>Link 4</a></li>
</ul>
/* Slower */
var myList = $(‘#myList’);
myList.find(‘a’).bind(‘click’,function(){/* doSomething() */});
/* Faster */
var myList = $(‘#myList’);
myList.delegate(‘click’, ‘a’, function(){/* doSomething() */});
Use efficient event delegation
Do not delegate to the body or the document when there is another option. Instead, delegate to the
target’s closest persistent ancestor. Also, delegation is not necessary when you are binding to unique
elements with an id (with the exception of Overlays, or if the unique ID is added/removed from the page
dynamically).
/* Avoid this */
$(‘body’).delegate(‘.someClass’, ‘click’, function(){…});
/* This is better */
$(‘#Main’).delegate(‘.someClass’, ‘click’, function(){…});
/* This is best */
$(‘#closestAncestor’).delegate(‘.someClass’, ‘click’,
function(){…});
Minimize DOM manipulation
Accessing and modifying the DOM is very expensive and should be kept to a minimum.
/* Avoid this */
var container = $(‘#container’);
for(var i=0, length = someArray.length; i < length; i++) {
// Modifies the DOM on each iteration of the loop
container.append(‘<li>…</li>);
}
/* Use this instead */
var container = $(‘#container’);
var markupString = ‘’;
for(var i=0, length = someArray.length; i < length; i++) {
markupString += ‘<li>…</li>’;
}
// Only modifies the DOM once
container.append(markupString);
Avoid global variables
Global variables and function names are a very bad idea, and must be avoided. If one needs to be
available to the global scope, they must be a member of a relevant namespace using object literal
notation.
/* Avoid This */
var current = null;
function doSomething() {…};
/* Use this, if there is not already a namespace to use (in most
cases there will be) */
var myNameSpace = {
current: null,
doSomething: function(){};
}
Use Object literals
Objects should never be declared using constructors, doing so is overly verbose and causes a lot of
overhead. Object literal syntax should be used instead.
/* Slower */
var foo = new Object()
foo.bar = 'property';
foo.baz = 'property1';
var fooBar = new Array();
fooBar[0] = 'first slot';
fooBar[1] = 'second slot';
/* Faster */
var foo = {
bar: 'property',
baz: 'property2'
} ;
var fooBar = [
'first slot',
'second slot'
];
Do not pass strings to setTimeout() or setInterval()
Instead, pass function names or anonymous functions. A string passed to setTimeout will be executed
globally with eval, which is very slow.
// Slower
setTimeout(‘functionName()’,1000);
// Faster
setTimeout(functionName, 1000);
// Also good
setTimeout(function(){/* doSomething() */},1000);
Optimize string concatenation
Each time the + operator is used, a new string is created in memory and concatenated to the value
assigned to it. Instead, directly build upon the existing variable.
// Slower
a += ‘b’ + ‘c’;
// Faster
a += ‘b’;
a += ‘c’;
De-reference unused objects
Once a variable or element or event-binding is no longer needed, dereferencing it will get ahead of the
JS garbage collection and free up memory. This is especially important for IE
// Delete objects
namespace.foo = { bigData: 'foobar' };
// Do something with namespace.foo
delete namespace.foo;
// Detach listeners when no longer needed
someElement.removeEventListener(type, fn, false) // Native
$(someElement).unbind(type); // jQuery
// Remove DOM elements when no longer needed
someElement.parentNode.removeChild(someElement); // Native
$(someElement).remove(); //jQuery
Avoid loops if possible, but when needed ensure they
are efficient
In all program languages, loops should be avoided if at all possible. If they are necessary, keep as much
evaluation or variable creation outside of the loop as possible. Also, cache array lengths in a variable.
Avoid nested loops.
// Slower
for(var i=0; i < someArray.length; i++) {
var container = document.getElementById(‘containerId’);
doSomething();
}
// Faster
var container = document.getElementById(‘containerId’);
for(var i=0, length = someArray.length; i < length; i++) {
doSomething();
}
Prohibited functions
The following have been proven to be inefficient, and should be avoided.
 eval()
 document.write()
 for-in loops
 while loops
Do not mix CSS with JavaScript
Whenever possible styling should not be done with JavaScript, unless style needs to change based on
user-interaction. If JavaScript needs to adjust the style of an element, it must do so without
manipulating style attributes directly. Instead, add/remove a CSS class, and allow the CSS to style the
element.
// Slower
$(‘#foo’).attr(‘style’,’color:blue’);
// Faster
$(‘#foo’).addClass(‘someClass’);
Optimize Browser Layout Calculation
Setting styles on an element invalidates the browser's layout so that looking up certain style and layout
properties will cause the browser to compute the position of every element on the page. The more
elements on the page, the more costly the calculation. It is possible to minimize the amount of times
this happens.
// Slower
elementA.className = "a-style";
var heightA = elementA.offsetHeight; // layout calculated
elementB.className = "b-style"; // invalidates the layout
var heightB = elementB.offsetHeight; // layout calculated again
// Faster
elementA.className = "a-style";
elementB.className = "b-style";
var heightA = elementA.offsetHeight; // layout calculated
var heightB = elementB.offsetHeight; // layout is up-to-date
Validating Code
Syntactical quality of code must be ran though JSHint - a JavaScript validation tool that gives a detailed
report about syntax warnings and their meaning. Clean and valid code means less confusing bugs, easier
maintainability, and better code security.
http://www.jshint.com/

More Related Content

Recently uploaded

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 

Recently uploaded (20)

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Javascript (Jquery) best practices

  • 1. XYZ- Javascript Best Practices For maximizing Front End performance Avoid inefficient jQuery selectors.................................................................................................................2 Cache selected elements ..............................................................................................................................2 Use delegate() when possible.......................................................................................................................3 Use efficient event delegation......................................................................................................................3 Minimize DOM manipulation........................................................................................................................4 Avoid global variables...................................................................................................................................4 Use Object literals.........................................................................................................................................4 Do not pass strings to setTimeout() or setInterval().....................................................................................5 Optimize string concatenation......................................................................................................................5 De-reference unused objects........................................................................................................................5 Avoid loops if possible, but when needed ensure they are efficient ...........................................................6 Prohibited functions .....................................................................................................................................6 Do not mix CSS with JavaScript.....................................................................................................................6 Optimize Browser Layout Calculation...........................................................................................................7 Validating Code.............................................................................................................................................7
  • 2. Avoid inefficient jQuery selectors Too general Use the closest persistent ancestor with an ID to look up an element instead of just a class, which requires traversing the entire DOM. $('.className') // Avoid this, it’s slow! $(‘#someAncestor’).find(‘.className’); // Use this cachedElement.find(‘.className’); // Or this Pseudo selectors These are the slowest selectors to use, there is usually a better way to do what you’re trying to. Avoid them at all costs. $('.overlay:hidden') // this takes 200ms in IE8 Attribute selectors These are also very slow, use a class instead if it is available. $('select[name=attribute]') // Avoid this Finding elements with id's within other elements When an element has an ID, it is always faster to simply select it than searching within another container. $('#element).find('#anotherElement') // Avoid this self.cache.body.find(‘#anotherElement’) // Avoid this $(‘#anotherElement’) // Use this Looping elements Use the good ol’ basic FOR instead of the $.each for looping. It has been proved that the Jquery loop is slower in performance - http://jsperf.com/jquery-each-vs-for-loop/6 $.each(a, function() { e = this; }); // Avoid this for (var i = 0, len = a.length; i < len; i++) { e = a[i]; }; // Use this Cache selected elements Selecting the same element multiple times causes unnecessary DOM traversal, instead cache the element in a variable and reuse it. /* Avoid this */ $(‘#element’).addClass(‘foo’); // do some stuff $(‘#element’).removeClass(‘bar’);
  • 3. /* Use this instead */ var myElement = $(‘#element’); myElement.addClass(‘foo’); // do some stuff myElement.removeClass(‘bar’); Use delegate() when possible When multiple elements within a persistent container trigger the same action, use delegate() instead of bind() <ul id=”myList”> <li><a href=””>Link 1</a></li> <li><a href=””>Link 2</a></li> <li><a href=””>Link 3</a></li> <li><a href=””>Link 4</a></li> </ul> /* Slower */ var myList = $(‘#myList’); myList.find(‘a’).bind(‘click’,function(){/* doSomething() */}); /* Faster */ var myList = $(‘#myList’); myList.delegate(‘click’, ‘a’, function(){/* doSomething() */}); Use efficient event delegation Do not delegate to the body or the document when there is another option. Instead, delegate to the target’s closest persistent ancestor. Also, delegation is not necessary when you are binding to unique elements with an id (with the exception of Overlays, or if the unique ID is added/removed from the page dynamically). /* Avoid this */ $(‘body’).delegate(‘.someClass’, ‘click’, function(){…}); /* This is better */ $(‘#Main’).delegate(‘.someClass’, ‘click’, function(){…}); /* This is best */ $(‘#closestAncestor’).delegate(‘.someClass’, ‘click’, function(){…});
  • 4. Minimize DOM manipulation Accessing and modifying the DOM is very expensive and should be kept to a minimum. /* Avoid this */ var container = $(‘#container’); for(var i=0, length = someArray.length; i < length; i++) { // Modifies the DOM on each iteration of the loop container.append(‘<li>…</li>); } /* Use this instead */ var container = $(‘#container’); var markupString = ‘’; for(var i=0, length = someArray.length; i < length; i++) { markupString += ‘<li>…</li>’; } // Only modifies the DOM once container.append(markupString); Avoid global variables Global variables and function names are a very bad idea, and must be avoided. If one needs to be available to the global scope, they must be a member of a relevant namespace using object literal notation. /* Avoid This */ var current = null; function doSomething() {…}; /* Use this, if there is not already a namespace to use (in most cases there will be) */ var myNameSpace = { current: null, doSomething: function(){}; } Use Object literals Objects should never be declared using constructors, doing so is overly verbose and causes a lot of overhead. Object literal syntax should be used instead. /* Slower */ var foo = new Object() foo.bar = 'property'; foo.baz = 'property1'; var fooBar = new Array(); fooBar[0] = 'first slot';
  • 5. fooBar[1] = 'second slot'; /* Faster */ var foo = { bar: 'property', baz: 'property2' } ; var fooBar = [ 'first slot', 'second slot' ]; Do not pass strings to setTimeout() or setInterval() Instead, pass function names or anonymous functions. A string passed to setTimeout will be executed globally with eval, which is very slow. // Slower setTimeout(‘functionName()’,1000); // Faster setTimeout(functionName, 1000); // Also good setTimeout(function(){/* doSomething() */},1000); Optimize string concatenation Each time the + operator is used, a new string is created in memory and concatenated to the value assigned to it. Instead, directly build upon the existing variable. // Slower a += ‘b’ + ‘c’; // Faster a += ‘b’; a += ‘c’; De-reference unused objects Once a variable or element or event-binding is no longer needed, dereferencing it will get ahead of the JS garbage collection and free up memory. This is especially important for IE // Delete objects namespace.foo = { bigData: 'foobar' }; // Do something with namespace.foo delete namespace.foo;
  • 6. // Detach listeners when no longer needed someElement.removeEventListener(type, fn, false) // Native $(someElement).unbind(type); // jQuery // Remove DOM elements when no longer needed someElement.parentNode.removeChild(someElement); // Native $(someElement).remove(); //jQuery Avoid loops if possible, but when needed ensure they are efficient In all program languages, loops should be avoided if at all possible. If they are necessary, keep as much evaluation or variable creation outside of the loop as possible. Also, cache array lengths in a variable. Avoid nested loops. // Slower for(var i=0; i < someArray.length; i++) { var container = document.getElementById(‘containerId’); doSomething(); } // Faster var container = document.getElementById(‘containerId’); for(var i=0, length = someArray.length; i < length; i++) { doSomething(); } Prohibited functions The following have been proven to be inefficient, and should be avoided.  eval()  document.write()  for-in loops  while loops Do not mix CSS with JavaScript Whenever possible styling should not be done with JavaScript, unless style needs to change based on user-interaction. If JavaScript needs to adjust the style of an element, it must do so without manipulating style attributes directly. Instead, add/remove a CSS class, and allow the CSS to style the element. // Slower $(‘#foo’).attr(‘style’,’color:blue’);
  • 7. // Faster $(‘#foo’).addClass(‘someClass’); Optimize Browser Layout Calculation Setting styles on an element invalidates the browser's layout so that looking up certain style and layout properties will cause the browser to compute the position of every element on the page. The more elements on the page, the more costly the calculation. It is possible to minimize the amount of times this happens. // Slower elementA.className = "a-style"; var heightA = elementA.offsetHeight; // layout calculated elementB.className = "b-style"; // invalidates the layout var heightB = elementB.offsetHeight; // layout calculated again // Faster elementA.className = "a-style"; elementB.className = "b-style"; var heightA = elementA.offsetHeight; // layout calculated var heightB = elementB.offsetHeight; // layout is up-to-date Validating Code Syntactical quality of code must be ran though JSHint - a JavaScript validation tool that gives a detailed report about syntax warnings and their meaning. Clean and valid code means less confusing bugs, easier maintainability, and better code security. http://www.jshint.com/