info@toimi.pro

How to improve JavaScript performance: Practical tips

8 min

Remember the dial-up days when pages took half a minute to load? Most often, JavaScript is the culprit - or more specifically, how we use it. Today we'll explore proper code optimization techniques and share working solutions for common performance issues.

Artyom Dovgopol
Artyom Dovgopol

Code optimization is an art form. It's like racing: you can just hop in and drive, or you can tune every part of the engine. The difference is obvious right away 😉

Key takeaways 👌

Smart code optimization can speed up website loading by 60-70% without losing functionality

Asynchronous loading of scripts cuts the time to first render in half

Lazy loading of images and scripts significantly improves performance - loading time decreases by 30-40%

Introduction

Remember those old computer games that lagged even on powerful machines? Often it wasn't the hardware but the code. The same thing happens with modern websites – even a super-powerful server won't save you if your JavaScript is poorly optimized.

Today, the average JavaScript code size on websites reaches 350 KB, and it's only growing. Studies show that about 40% of this code is never used during page load. Every extra kilobyte means additional loading time, especially on mobile devices and slow internet connections.

Interesting fact 👀

Did you know that JavaScript was created in just 10 days? Brendan Eich was tasked with "making something like Java, but simpler." Now this 10-day project runs the internet. It's like writing a song in one evening, and then it becomes a global hit!

Practical optimization tips

Code optimization is like packing a large suitcase for travel. There are several proven ways to make your baggage lighter:

  1. Remove the unnecessary. Remember how mom taught you to pack for trips? "Only take what you need!" Same goes for JavaScript – every extra line of code means additional milliseconds of loading time.
    Code name example
    // Before:
    function loadUserData(user) {
        console.log('Checking data');
        console.log('Loading...');
        return user.name;
    }
    
    // After:
    function loadUserData(user) {
        return user.name;
    }
  2. Load smartly. Put essential items on top, the rest at the bottom. In code, we use async and defer:
    Code name example
    // Core scripts - immediately
    <script src="core.js"></script>
    
    // Secondary loads later
    <script async src="stats.js"></script>
    <script async src="stats.js"></script>
Think

The most complex performance issues often have simple solutions. Sometimes just properly placing async and defer on your scripts can make your site load twice as fast. It's not about complex optimizations, but understanding the basics

Lazy loading

Imagine a restaurant where the chef prepares all dishes in advance - some might go bad, and the kitchen is always chaotic. A smart chef cooks as orders come in.

Meme
Code name example
// Load heavy functionality 
// only when needed
button.addEventListener(
  'click', 
  async () => {
    const { heavyF } = await import(
      './heavy-f.js'
    );
    heavyF();
  }
);
JavaScript today is the foundation of the web. But like in building a house, it's not just about the material, but how you use it. Unoptimized code can turn a Ferrari into a rusty old junker.

Dan Abramov, creator of Redux and React developer

DOM and how to work with it

Working with the DOM often becomes a performance bottleneck. Here's a classic example of inefficient code you might find on many websites:

Code name example
// Common mistake when working with DOM
for (let i = 0; i < 100; i++) {
document.body.innerHTML += 'Block';
}

// Optimized version
const container = document.createDocumentFragment();
for (let i = 0; i < 100; i++) {
container.appendChild(document.createElement('div'));
}
document.body.appendChild(container);

And more about
And more about performance...

Check out our article What is Web 3.0 and what it means for Internet evolution, where we dive deep into how the Web 3.0 standard is changing approaches to web application performance

Recommended reading 🤓
"JavaScript: The Good Parts"

"JavaScript: The Good Parts", Douglas Crockford

If Douglas Crockford wrote a cookbook, this would be it. Only the tastiest JavaScript recipes.

On Amazon
"Eloquent JavaScript"

"Eloquent JavaScript", Marijn Haverbeke

Marijn Haverbeke wrote this book as if he's telling an interesting story rather than teaching programming.

On Amazon
"High Performance JavaScript"

"High Performance JavaScript", Nicholas C. Zakas

The go-to book for those who want their code to fly, not crawl.

On Amazon

Conclusion

JavaScript optimization isn't a one-time task – it's an ongoing process of improvements. Start with simple steps: remove unused code, set up proper script loading, implement lazy loading. Each small improvement adds up to a significant performance boost.

Read the comments and leave your own
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Top articles ⭐

SEO and Analytics
Cross-channel analytics: What it is and how to implement it
In this article, we'll explore how to build an effective end-to-end analytics system without unnecessary complications. You'll learn about real implementation cases, common mistakes and how to avoid them. Artyom Dovgopol Data without action is just numbers on a screen. Real value emerges when you start using it for decision-making…
January 24, 2025
8 min
257

Your application has been sent!

We will contact you soon to discuss the project

Close