DEV Community

Cover image for Optimizing JavaScript for Performance
Kafeel Ahmad (kaf shekh)
Kafeel Ahmad (kaf shekh)

Posted on • Updated on

Optimizing JavaScript for Performance

Optimizing JavaScript for Performance

JavaScript is a versatile and powerful language, but it can also be a source of performance bottlenecks if not optimized correctly. In this article, we will explore several key techniques to optimize JavaScript code, accompanied by examples to illustrate each point.

1. Minimize DOM Access

Manipulating the Document Object Model (DOM) is one of the most time-consuming operations in JavaScript. To minimize DOM access, you should:

  • Cache DOM elements
  • Reduce the number of DOM manipulations
  • Use Document Fragments for bulk updates

Example:

// Inefficient DOM manipulation
for (let i = 0; i < 1000; i++) {
    let div = document.createElement('div');
    div.textContent = i;
    document.body.appendChild(div);
}

// Optimized with Document Fragment
let fragment = document.createDocumentFragment();
for (let i = 0; i < 1000; i++) {
    let div = document.createElement('div');
    div.textContent = i;
    fragment.appendChild(div);
}
document.body.appendChild(fragment);
Enter fullscreen mode Exit fullscreen mode

2. Debounce and Throttle Event Handlers

Event handlers can fire frequently, especially for events like scrolling or resizing. To optimize, use debounce or throttle techniques to limit the rate at which these handlers execute.

Example:

// Debounce function
function debounce(func, wait) {
    let timeout;
    return function(...args) {
        clearTimeout(timeout);
        timeout = setTimeout(() => func.apply(this, args), wait);
    };
}

// Throttle function
function throttle(func, limit) {
    let inThrottle;
    return function(...args) {
        if (!inThrottle) {
            func.apply(this, args);
            inThrottle = true;
            setTimeout(() => inThrottle = false, limit);
        }
    };
}

// Usage
window.addEventListener('resize', debounce(() => {
    console.log('Resized');
}, 200));

window.addEventListener('scroll', throttle(() => {
    console.log('Scrolled');
}, 100));
Enter fullscreen mode Exit fullscreen mode

3. Use Asynchronous Programming

Blocking the main thread with synchronous operations can lead to a poor user experience. Utilize asynchronous techniques such as async/await, Promises, or setTimeout to keep the main thread responsive.

Example:

// Synchronous operation (blocking)
function fetchData() {
    let response = fetch('https://api.example.com/data'); // Blocking
    console.log(response);
}

// Asynchronous operation (non-blocking)
async function fetchDataAsync() {
    let response = await fetch('https://api.example.com/data'); // Non-blocking
    console.log(response);
}

fetchDataAsync();
Enter fullscreen mode Exit fullscreen mode

4. Optimize Loops and Iterations

Loops can be optimized by minimizing the work done inside them. For example, cache the length of an array or use more efficient loop constructs.

Example:

// Inefficient loop
let arr = [1, 2, 3, 4, 5];
for (let i = 0; i < arr.length; i++) {
    console.log(arr[i]);
}

// Optimized loop
for (let i = 0, len = arr.length; i < len; i++) {
    console.log(arr[i]);
}

// Using forEach
arr.forEach(item => console.log(item));
Enter fullscreen mode Exit fullscreen mode

5. Minimize Reflows and Repaints

Reflows and repaints are expensive operations in the browser rendering process. Minimize these by:

  • Batching DOM updates
  • Using CSS classes instead of inline styles
  • Avoiding layout thrashing

Example:

// Layout thrashing (inefficient)
const element = document.getElementById('myElement');
element.style.width = '100px';
console.log(element.offsetWidth);
element.style.height = '100px';
console.log(element.offsetHeight);

// Optimized
const element = document.getElementById('myElement');
element.style.cssText = 'width: 100px; height: 100px;';
const width = element.offsetWidth;
const height = element.offsetHeight;
console.log(width, height);
Enter fullscreen mode Exit fullscreen mode

6. Use Efficient Data Structures

Choosing the right data structure can significantly improve performance. For example, use sets for unique values and maps for key-value pairs with frequent lookups.

Example:

// Inefficient array search for unique values
let arr = [1, 2, 3, 4, 5, 1, 2];
let uniqueArr = arr.filter((item, index) => arr.indexOf(item) === index);

// Optimized using Set
let uniqueSet = new Set(arr);
let uniqueArrOptimized = [...uniqueSet];
Enter fullscreen mode Exit fullscreen mode

Conclusion

Optimizing JavaScript is crucial for improving the performance and responsiveness of web applications. By minimizing DOM access, debouncing and throttling event handlers, using asynchronous programming, optimizing loops, minimizing reflows and repaints, and choosing efficient data structures, you can create faster and more efficient JavaScript code. Implement these techniques in your projects to see a noticeable improvement in performance.

Top comments (9)

Collapse
 
htho profile image
Hauke T.

Hi - Thanks for your Article.

When I read this article, I get a strong AI vibe. If AI was used to write that article, it must stated in the article.

There are severe errors in the code:

(3) Use Asynchronous Programming

The "blocking" example is not blocking at all.
fetch returns a Promise. If you await the Promise, you (asynchronously) get the data received.
If you don't, then console.log(result) will just log something like PromiseΒ {<pending>}.

(4) Optimize Loops and Iterations

I wonder if there is any measurable difference between the first and the second loop, where the length is cached. It depends on the data-structure. For arrays the length is a property of the array. Access should be as fast as caching it upfront.

(5) Minimize Reflows and Repaints

The problem is not setting width and height in two lines of code. The problem is reading them right after writing them. The first example can simply be optimized by moving console.log(element.offsetWidth); one line down.

Conclusion

I am tired to see so many low quality articles like this here on DEV. They degrade the value of our community. This is just one of the many.

I beg you: Please only write about stuff you actually used and understand. Chose appropriate examples. Don't use AI, except for grammar checks. Don't write for the writings sake. Publish articles of which you think they add value to the common knowledge of developers world wide.

That would make DEV a better place.

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

There are actually guidelines for AI generated/assisted posts that should be followed by the author if indeed this is such content:

Collapse
 
nakul-047 profile image
Nakul

I also got feel of AI content. Thanks for pointing out.

Collapse
 
webjose profile image
JosΓ© Pablo RamΓ­rez Vargas

Array.forEach() and similar (filter, etc.) are usually one order of magnitude slower than a FOR loop.

Collapse
 
kafeel_ahmad profile image
Kafeel Ahmad (kaf shekh)
Collapse
 
martinbaun profile image
Martin Baun

Great article. Props!

Collapse
 
ashishsimplecoder profile image
Ashish Prajapati

layout trashing is new for me.

Collapse
 
htho profile image
Hauke T.

This is the mother of all articles about that topic:
web.dev/articles/avoid-large-compl...

Collapse
 
rt1616103 profile image
Ryan Tyler

Great article πŸ‘ŠπŸ‘ŠπŸ‘ŒπŸ‘Œ