Optimize long task in JavaScript [Part 2]

Optimize long task in JavaScript [Part 2]

Continuing from the previous article. In this part, I will provide a more practical example.

In this example, we have saveSettings function including 5 sub-functions inside to handle rendering UI, saving data to db, and analyzing data.

Article content
saveSettings function

If we call 5 sub-functions synchronously, it creates a long task that can make browsers laggy and unresponsive.

Article content
Performance tab

To improve performance, we will do critical works first and defer non-critical works (not visible to user) by using setTimeout.

Article content
Optimize long task - setTimeout

Another way to defer non-critical works called yield.

Article content
Optimize long task - Yield

Here is the result of two optimized ways mentioned above.

Article content
Performance tab

Zoom in the picture above, we can see that the browser responds to higher-priority work much sooner, such as user interactions or rendering. That's why the browser remains smooth.

Article content
User interactions task


Trần Đông Thạnh

⚡Database Administrator | SQL, Oracle Database ⚡

1y

Very helpful!

Fatima Azam

Software Engineer | .Net | .Net core | Microservices | Azure | Angular | React

1y

Thanks for sharing Phạm Đình Thiện

Phạm Đình Thiện

⚡️Lead Software Engineer @ MISA JSC | Youtube “Phạm Đình Thiện - Tối ưu Frontend”

1y
Like
Reply
Phạm Đình Thiện

⚡️Lead Software Engineer @ MISA JSC | Youtube “Phạm Đình Thiện - Tối ưu Frontend”

1y
Like
Reply

To view or add a comment, sign in

Others also viewed

Explore topics