DEV Community

Cover image for Optimise performance in Javascript: Part 1
shelly agarwal
shelly agarwal

Posted on • Edited on

2 2 2 3 2

Optimise performance in Javascript: Part 1

Hello devs, I'm sure you've heard it before— "We need to optimize this code or flow; it's taking too long and hurting the user experience!" Sounds familiar? You’ve got this! 😎🚀

What does optimization actually means..

If your pages are loading slowly, tasks are taking too long to return results, or your landing page is sluggish—chances are users won’t stick around to see what your website has to offer. That’s a clear sign it’s time for optimization!

How?
1. Don't block the main thread
2. Break up your long tasks

You need to understand what tasks are and how browser handles them

What tasks are?

  1. Task is a piece of work that the browser does.
  2. That task includes rendering, parsing HTML and CSS, running javascript and other type of work that you might not have direct control over. Of all of this, javascript you write is perhaps the largest source of tasks.

What is a main thread?

1. It is a thread where almost all javascript code you write executes and most tasks are run in the browser.
2. It process tasks 1 by 1 in a queue

Tasks taking more than 50 ms is considered as long task
Task's blocking time: Total time taken by a task - 50 ms

Should we start with running Long task?

function longTask() {
  for (let index = 0; index < 30_000; index++) {
    // Emulate CPU load, as using console.log
    // with open devtools is a very CPU-intensive operation.
    console.log(index);
  }
}
Enter fullscreen mode Exit fullscreen mode

Let's try this out..
You will see performance like this 👇 (Go to performance tab by inspecting the element)

long_task

It says, long task took 2.55 s. in RED color, it means, the task took more than what was expected as an ideal time.

Any task exceeding 50ms is a long task
Example 1

function task(name, num = 5_000) {
  console.log(`Starting task: ${name}`);

  for (let index = 0; index < num; index++) {
    console.log({index});
  }
}

function longTask() {
    task('1');
    task('2');
    task('3');
    task('4');
    task('5');
    task('6');
  }

  useEffect(() => { 
    longTask()
  },[])
Enter fullscreen mode Exit fullscreen mode

example_1

Example 2: After breaking long tasks into smaller tasks.

useEffect(() => {
  function longTask() {
  // Critical tasks.
  task('1');
  task('3');

  // Deferring set of tasks.
  setTimeout(() => {
    task('2');
    task('4');
    task('5');
    task('6');
  });
}

longTask()
  },[])
Enter fullscreen mode Exit fullscreen mode

Now as you can see below, one long Task is divided into 4 sub tasks (63.46 ms + 836.75 ms + 520.72 ms + 510.94 ms)

after_img

  • This technique is known as yielding, works best with sequential tasks.
    (In simple terms - setting a setTimeOut in a series of functions is known as yielding.)

  • It helps dividing the tasks into subtasks, so that critical tasks can be executed first then in next task non-critical ones.

Example 3:

async function saveSettings () {
  // Do critical work that is user-visible:
  task('1');
  task('3');

  // Yield to the main thread:
  await scheduler.yield()

  // Work that isn't user-visible, continued in a separate task:
    task('2');
    task('4');
    task('5');
    task('6');
}
Enter fullscreen mode Exit fullscreen mode

Alternative of setTimeOut -

  1. scheduler.yield() is an API specifically designed for yielding to the main thread in the browser.
  2. Use scheduler library link

To summarize in short:

  1. Look what are the priority tasks that will affect user experience.
  2. Yield to the main thread for crutial tasks.
  3. Minimize work per function upto 10ms and below 50ms to avoid blocking the thread.

Using these approaches you can improve your web performance effectively.

Go check out part 2 of my exciting blog series on JavaScript performance! 🚀 🤯

If you enjoyed this blog (short and sweet), check out my other blogs too! -

👍 Like and drop a comment if you have any questions or if you find it useful!

Image_description

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (1)

Collapse
 
shelly_agarwal_19 profile image
shelly agarwal • Edited

Checkout 2nd part of this article
dev.to/shelly_agarwal_19/performan...

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

If you found this post helpful, please leave a ❤️ or a friendly comment below!

Okay