One of the most important things, when you're building a website, is ensuring its good performance. When people visit your webpage, they don't want to wait for 10 minutes until the page (and all the images) load. In a survey, it was found that 47% of visitors expect a website to load in less than 2 seconds and 40% of visitors leave the website if it takes more than 3 seconds to load.
Whenever you call a certain function, the variables that are used to define that function are stored inside. Variables can be categorized into two types.
- Local variables- Variables that are defined only within themselves.
- Global variables - Variables that are used throughout the script.
At the time when you're calling a function, the browser does an activity that is termed as scope lookup. With the increase in the number of scopes in the scope chain, there's also an increase in the amount of time taken to access variables that are outside the current scope.
That's the reason why an engine takes a long time to access a global variable as compared to a local variable. This means that if you define most of the variables locally, then there will be a rapid decrease in the time required by the engine to search them. Hence, it will boost the overall speed of the application.
The second method to increase the performance of your site is to use cache in your browser. When your browser launches your code, it repeatedly opens the same script again. If you use your cache right, it will open the already saved script the next time and the performance will improve immediately.