What is script execution ?
It is the time spent by the browser analyzing, compiling and executing JavaScript code. These steps are essential after loading the scripts so that the page can take advantage of the defined features and behaviors. The main execution is called "main thread" (it is possible to download some processing on secondary "threads"), it is popularly known as the main process in charge of the rendering and interactivity of the page.
Why is it important ?
When the page includes a lot of JavaScript code, it degrades the performance of the page in several ways :
- a loading delay slowed down by the weight of the scripts to download
- a loss of interactivity regarding users' actions due to too much solicitation of the "main thread" which cannot respond
- a higher cost of interpretation, especially if the page executes a lot of code before it is needed, which delays the interactivity time frame
- a high memory consumption which can give slow and jerky effects to users
How can it be improved ?
When a page suffers from slowness due to too much script execution, it is useful to :
- remove unused scripts from the page
- remove unused code from scripts
- split the JavaScript code into separate files and load only those useful for the page
- defer the execution of certain tasks in the JavaScript code
- decrease the overall use of scripts in the page