Tips for Improving JavaScript Performance

By Joydip Kanjilal


Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

JavaScript has for long been the de-facto scripting language for the web. For performance critical applications, you may want to follow the best practices in the presentation layer of your application to boost your application's performance and scalability. This article presents a discussion on the best practices that can be followed to improve JavaScript performance so as to ensure that your web application is running as optimally as possible.

Let's now take a look at the important tips that can be adopted to speed up JavaScript performance.


Take advantage of caching to improve performance. You can have scripts that access a particular object multiple times. You can leverage caching by storing a repeatedly accessed object inside a user defined variable. Then you can use this variable instead of subsequent references to the object. The following code snippet shows how you can achieve this.

<script type = "text/javascript>
var cachedObj = document.images
for (var ctr = 0; ctr < cachedObj.length; ctr++)
cachedObj[ctr].src = "someImage.gif"

Minimize DOM access

Note that accessing the DOM in HTML is slow, i.e., it is a performance intensive operation. You should ensure that DOM access is at the minimum. There are many ways you can do this to reduce DOM traversal like, you can store references to the browser objects at the time of instantiation so that they can be reused at a later point of time.

Asynchronous API calls

Asynchronous programming facilitates fast and responsive user interfaces. You should take advantage of asynchrony to make asynchronous calls to the APIs. This would reduce the need of using too many worker threads and would boost the application's performance and scalability. Asynchronous programming is complex but there are many resources available these days.

Use local variables

The time needed to access a global variable is more than the time that is needed to access a variable that is defined inside a local scope. When you write functions in JavaScript, try to avoid global variables as much as you can. Local variables are those that have local scope - you can have variables defined in your functions rather than define them outside of the functions. It takes the runtime quite some time to search for variables that exist in the global scope, so, it is a good practice to avoid global variables if you don't need them.

Ensure that your code is lean and clean

When working in applications, it is imperative that you build solutions that are maintainable and the code is easy to understand and is readable. You should organize your JavaScript code in such a way that it is lean, clean and manageable. You should use the right frameworks for your presentation layer.

Also, you should follow the best practices when writing your JavaScript code. Typical examples include, avoiding unwanted loops, avoiding calls to the length property in the loops by storing the value of the length property of an object or etc. in a variable, and many more. You should try to avoid assignment statements outside of the loop as much as you can. This will help the loop run faster.


You can take advantage of Gzip compression to compress the rendered content. Note that most servers and modern-day browsers support Gzip compression. When you have a Gzip compatible browser requesting a resource from the server, the response is rendered in a compressed format thus saving on the bandwidth, time needed to render the response, etc. This greatly improves the performance of the application.


This article presented a glimpse at the best practices and tips that can be followed to improve JavaScript performance to build applications that are fast and efficient. Happy reading!

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date