SlideShare a Scribd company logo
JavaScript Performance
Tips
SHAKTI SHRESTHA
shakti.shrestha@gmail.com
1. Define local variables
When a variable is referenced, JavaScript hunts it down by looping
through the different members of the scope chain (nesting of
object).
Simply, the deeper the engine has to dig into this scope chain, the
longer the operation will take.
Since local variables are first in this chain, they’re always faster than
globals. So anytime you use a global variable more than once you
should redefine it locally
1. Define local variables
Difference
2. Use closures as less as possible
Also can be called as inline function.

JavaScript treats a function as an object.
It causes a huge performance hit.
2. Use closures as less as possible
What we do mostly What is good
Code using closure
Refer to a function
Common event handler
2. Use closures as less as possible
http://jsperf.com/javascript-performance-02
3. Object properties and array items are
slower
 When you reference an object property or array item multiple
times
You can get a performance boost by defining a variable.
This applies to both reading and writing data.
Similarly don't dig too deep into array.
It cause a performance hit.
If you constantly reference foo.bar you can get a performance
boost by defining var bar = foo.bar;
3. Object properties and array items are
slower
What we do mostly What is good
Referring through deep nest
Improved method
3. Object properties and array items are
slower
http://jsperf.com/javascript-performance-03
4. Avoid function based iteration
 To allow function based looping, the iteration setups up a function
for each item in the loop which causes a performance issue.
 In javascript a function = an object.
4. Avoid function based iteration
Function based iteration Normal For loop
Function based iteration. Classic For loop.
4. Avoid function based iteration
http://jsperf.com/javascript-performance-04
5. Always Descend From an #id
 The fastest selector in jQuery is the ID selector ($('#someId')).
 This is because it maps directly to a native JavaScript method,
getElementById().
 Selecting multiple elements means:
DOM traversal and looping
something that is slow.
To minimize the performance hit, always descend from the closest
parent ID.
5. Always Descend From an #id
Not good Good
NG
OK
5. Always Descend From an #id
http://jsperf.com/javascript-performance-05
5. Always Descend From an #id
http://jsperf.com/javascript-performance-05-01
6. Cache jQuery Objects
 Set a reference of the jQuery object to a variable.
 var $active_light = $('#traffic_light input.on');
 As a standard set $ as prefix of jQuery object
(can be quickly identified as jQuery object)
var $active_light
6. Cache jQuery Objects
jQuery object NOT cached jQuery object cached
Object Not Cached Object Cached
6. Cache jQuery Objects
http://jsperf.com/javascript-performance-06
7. Harness the Power of Chaining
 Less code
 Better organized
7. Harness the Power of Chaining
jQuery object cached only jQuery object cached & chaining
Using cached object Chaining
8. Use Sub-queries
 JavaScript allows us to run additional selector operations on a
wrapped set.
 This reduces performance overhead on subsequent selections
 Reason we already grabbed and stored the parent object in a local variable
(aka object caching).
8. Use Sub-queries
Using query selector Object caching & sub-queries
jQuery & query selector Sub-queries
JavaScript & query selector JavaScript & Sub-queries
8. Use Sub-queries
http://jsperf.com/javascript-performance-08
9. Limit Direct DOM Manipulation
DOM operations are resource-heavy because of reflow.
Reflow is basically the process by which the browser re-renders the DOM elements
on the screen.
For instance, if you change the width of a div with JavaScript, the browser has to
refresh the rendered page to account for this change.
The basic idea here is to create exactly what you need in memory, and then
update the DOM.
This is not a jQuery best practice, but a must for efficient JavaScript.
9. Limit Direct DOM Manipulation
9. Limit Direct DOM Manipulation
http://jsperf.com/javascript-performance-09
9. Limit Direct DOM Manipulation
Another good option is using documentFragment
For detail on use of documentFragment please check below link
http://ejohn.org/blog/dom-documentfragments/
10. Leverage Event Delegation
Also known as Bubbling
 When an event is triggered on an element,
for example, a mouse click on a button,
the same event is also triggered on all of that element’s ancestors.
This process is known as event bubbling
10. Leverage Event Delegation
10. Leverage Event Delegation
What we generally do Event Delegation
Query selector
Event delegate
10. Leverage Event Delegation
http://jsperf.com/javascript-performance-10
References:
http://jonraasch.com/blog/10-javascript-performance-boosting-tips-from-
nicholas-zakas
http://www.artzstudio.com/2009/04/jquery-performance-rules/#descend-
from-id
 http://ejohn.org/blog/dom-documentfragments/
 http://www.yuiblog.com/blog/2006/04/11/with-statement-considered-
harmful/
http://www.developer.nokia.com/Community/Wiki/JavaScript_Performance_B
est_Practices
Thank You

More Related Content

What's hot (20)

PDF
State of jQuery June 2013 - Portland
dmethvin
 
PDF
jQuery Conference San Diego 2014 - Web Performance
dmethvin
 
PPTX
Choosing a JavaScript Framework
Tim Rayburn
 
PPTX
Mvvm knockout vs angular
Basarat Syed
 
PDF
Principles of MVC for PHP Developers
Edureka!
 
PPTX
Single Page Applications: Your Browser is the OS!
Jeremy Likness
 
PDF
Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece
Dan Gribbin
 
PPT
Getting started with angular js
Maurice De Beijer [MVP]
 
PPTX
Angular js tutorial slides
samhelman
 
PPTX
Backbone.js
898RakeshWaghmare
 
PDF
On Selecting JavaScript Frameworks (Women Who Code 10/15)
Zoe Landon
 
PPTX
My XML is Alive! An Intro to XAML
Jeremy Likness
 
PPTX
Single Page Application (SPA) using AngularJS
M R Rony
 
PPT
Selenium
Manjyot Singh
 
PDF
Introduction to Using PHP & MVC Frameworks
Gerald Krishnan
 
PPTX
Selenium_For_Beginners_VodQA_Final
Manjyot Singh
 
PPT
Unobtrusive javascript
Lee Jordan
 
PDF
jQuery Chicago 2014 - Next-generation JavaScript Testing
Vlad Filippov
 
POT
Beginning In J2EE
Suthat Rongraung
 
PDF
Modularize JavaScript with RequireJS
Minh Hoang
 
State of jQuery June 2013 - Portland
dmethvin
 
jQuery Conference San Diego 2014 - Web Performance
dmethvin
 
Choosing a JavaScript Framework
Tim Rayburn
 
Mvvm knockout vs angular
Basarat Syed
 
Principles of MVC for PHP Developers
Edureka!
 
Single Page Applications: Your Browser is the OS!
Jeremy Likness
 
Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece
Dan Gribbin
 
Getting started with angular js
Maurice De Beijer [MVP]
 
Angular js tutorial slides
samhelman
 
Backbone.js
898RakeshWaghmare
 
On Selecting JavaScript Frameworks (Women Who Code 10/15)
Zoe Landon
 
My XML is Alive! An Intro to XAML
Jeremy Likness
 
Single Page Application (SPA) using AngularJS
M R Rony
 
Selenium
Manjyot Singh
 
Introduction to Using PHP & MVC Frameworks
Gerald Krishnan
 
Selenium_For_Beginners_VodQA_Final
Manjyot Singh
 
Unobtrusive javascript
Lee Jordan
 
jQuery Chicago 2014 - Next-generation JavaScript Testing
Vlad Filippov
 
Beginning In J2EE
Suthat Rongraung
 
Modularize JavaScript with RequireJS
Minh Hoang
 

Similar to Java script performance tips (20)

PPTX
30 JavaScript optimization tips
Hovhannes Avoyan
 
PPT
Performance optimization - JavaScript
Filip Mares
 
PDF
Performance Optimization and JavaScript Best Practices
Doris Chen
 
KEY
jQuery Performance Tips and Tricks (2011)
Addy Osmani
 
PPTX
Javascript best practices
Jayanga V. Liyanage
 
PDF
5 Quick JavaScript Performance Improvement Tips
Troy Miles
 
PPT
Javascript and Jquery Best practices
Sultan Khan
 
PPT
Web Performance Tips
Ravi Raj
 
PDF
Front-end optimisation & jQuery Internals (Pycon)
Artur Cistov
 
PPT
High Performance Ajax Applications 1197671494632682 2
Niti Chotkaew
 
PPT
High Performance Ajax Applications
Julien Lecomte
 
KEY
jQuery: Tips, tricks and hints for better development and Performance
Jonas De Smet
 
PPT
Web performance essentials - Goodies
Jerry Emmanuel
 
PDF
How DRY impacts JavaScript performance // Faster JavaScript execution for the...
Mathias Bynens
 
PPT
Ajax Performance
kaven yan
 
PDF
Client Side Performance In Web Applications
vladungureanu
 
PDF
Ajax Performance Tuning and Best Practices
Doris Chen
 
KEY
Developing High Performance Web Apps - CodeMash 2011
Timothy Fisher
 
KEY
A rough guide to JavaScript Performance
allmarkedup
 
PPT
jQuery Performance Rules
nagarajhubli
 
30 JavaScript optimization tips
Hovhannes Avoyan
 
Performance optimization - JavaScript
Filip Mares
 
Performance Optimization and JavaScript Best Practices
Doris Chen
 
jQuery Performance Tips and Tricks (2011)
Addy Osmani
 
Javascript best practices
Jayanga V. Liyanage
 
5 Quick JavaScript Performance Improvement Tips
Troy Miles
 
Javascript and Jquery Best practices
Sultan Khan
 
Web Performance Tips
Ravi Raj
 
Front-end optimisation & jQuery Internals (Pycon)
Artur Cistov
 
High Performance Ajax Applications 1197671494632682 2
Niti Chotkaew
 
High Performance Ajax Applications
Julien Lecomte
 
jQuery: Tips, tricks and hints for better development and Performance
Jonas De Smet
 
Web performance essentials - Goodies
Jerry Emmanuel
 
How DRY impacts JavaScript performance // Faster JavaScript execution for the...
Mathias Bynens
 
Ajax Performance
kaven yan
 
Client Side Performance In Web Applications
vladungureanu
 
Ajax Performance Tuning and Best Practices
Doris Chen
 
Developing High Performance Web Apps - CodeMash 2011
Timothy Fisher
 
A rough guide to JavaScript Performance
allmarkedup
 
jQuery Performance Rules
nagarajhubli
 
Ad

Recently uploaded (20)

PDF
Modern Decentralized Application Architectures.pdf
Kalema Edgar
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PPTX
Essential Content-centric Plugins for your Website
Laura Byrne
 
PDF
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pdf
ghjghvhjgc
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
NASA A Researcher’s Guide to International Space Station : Fundamental Physics
Dr. PANKAJ DHUSSA
 
PDF
“ONNX and Python to C++: State-of-the-art Graph Compilation,” a Presentation ...
Edge AI and Vision Alliance
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PPTX
Manual Testing for Accessibility Enhancement
Julia Undeutsch
 
PDF
Survival Models: Proper Scoring Rule and Stochastic Optimization with Competi...
Paris Women in Machine Learning and Data Science
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Modern Decentralized Application Architectures.pdf
Kalema Edgar
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
Essential Content-centric Plugins for your Website
Laura Byrne
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pdf
ghjghvhjgc
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
NASA A Researcher’s Guide to International Space Station : Fundamental Physics
Dr. PANKAJ DHUSSA
 
“ONNX and Python to C++: State-of-the-art Graph Compilation,” a Presentation ...
Edge AI and Vision Alliance
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
Digital Circuits, important subject in CS
contactparinay1
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Manual Testing for Accessibility Enhancement
Julia Undeutsch
 
Survival Models: Proper Scoring Rule and Stochastic Optimization with Competi...
Paris Women in Machine Learning and Data Science
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Ad

Java script performance tips