Computer >> Computer tutorials >  >> Programming >> Javascript

How to improve the performance of JavaScript?


To improve the performance, try the following techniques −

Create and use Variables

In JavaScript, only create variables, which you will use and save values. Use the following and avoid unnecessary lines of code and variable declaration −

document.write(num.copyWithin(2,0));

Avoid the usage of with

The with keywords do not have a good impact on JavaScript speed. Avoid using it in your code.

Faster Looping

While using a loop, keep the assignment outside of the loop. This would tun the loop faster −

var i;
// assignment outside the loop
var j = arr.length;

for (i = 0; i < j; i++) {
   // code
}