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.
If we call 5 sub-functions synchronously, it creates a long task that can make browsers laggy and unresponsive.
To improve performance, we will do critical works first and defer non-critical works (not visible to user) by using setTimeout.
Another way to defer non-critical works called yield.
Here is the result of two optimized ways mentioned above.
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.
⚡Database Administrator | SQL, Oracle Database ⚡
1yVery helpful!
Software Engineer | .Net | .Net core | Microservices | Azure | Angular | React
1yThanks for sharing Phạm Đình Thiện
⚡️Lead Software Engineer @ MISA JSC | Youtube “Phạm Đình Thiện - Tối ưu Frontend”
1yMy previous article: https://www.linkedin.com/posts/pdthien_wecommit100xshare-frontend-javascript-activity-7221755011473588224-rinW
⚡️Lead Software Engineer @ MISA JSC | Youtube “Phạm Đình Thiện - Tối ưu Frontend”
1yReference: https://web.dev/articles/optimize-long-tasks