How fast is JavaScript

Faster JavaScript

JavaScript itself is actually a relatively quick language. However, when it comes to processing large amounts of commands, small tricks can help to make the script faster.

Points are time

Points are time! Every point and every (square) bracket within an object means that the corresponding sub-object must first be found. That costs computing time. Avoid long object names such as e.g. and shorten the whole thing as often as possible by assigning the object to a variable that will be used instead. In addition, the instruction can also be used to save time. A pleasant side effect: the source text is also shorter due to the saved object spelling. Example:

//instead of:
var a = '';
for (i = 0; i {a + = document.myForm.mySelect [i] .value; }

//better:
var a = '';
var obj = document.myFormular.mySelect;
for (i = 0; i {a + = obj [i] .value; }

//or:
var a = '';
with (document.myForm)
{
for (i = 0; i {a + = mySelect [i] .value; }

Define variables

In contrast to other programming languages, JavaScript is known not to have to declare variables. This sometimes simplifies programming, but it can lead to losses in the speed of a script. The advantage of defining variables is basically that the browser can set the memory in advance and this does not have to be done while the script is running. It is also advisable to rely more on global variables. Example:

//instead of:
function do something ()
{a = 1; /*...*/ return (a);}

//better:
function do something ()
{var a; a = 1; /*...*/ return (a);}

//or:
var a;
function do something ()
{a = 1; /*...*/ return (a);}

Avoid calls

Functions generally assign a new value to objects or variables. For the browser this means that it has to access the memory space of the object or the variable in order to be able to rewrite it. In order to save valuable time here, too, it is advisable to avoid calls as often as possible and instead to access the result of a function directly (). Likewise, for example, a string does not have to be saved within a variable in order to work with it. Example:

//instead of:
a = 'my text';
b = a.replace ('m', 'd');
c = b.substr (0.4);

//better:
a = 'my text'.replace (' m ',' d '). substr (0,4);

End grinding early

- Loops are easy to implement, but often make more passes than necessary. If, for example, a certain value is to be found from an array, the loop does not necessarily have to run through to the end of the array, but can rather be canceled after the find. It is often advisable to use or loops instead of a variant if this can reduce the number of passes. Example:

var a = new Array ('ab', 'cd', 'ef', 'gh', 'ij', 'kl', 'mn');

//instead of:
function getIndex ()
{b = -1;
for (i = 0; i {if (a [i] .search ('e')! = -1) {b = i;}}
return (b);
}

//better:
...
{if (a [i] .search ('e')! = -1) {b = i; break;}}
...

//or:
function getIndex ()
{b = -1; i = 0; found = false;
while (found == false)
 {
if (a [i] .search ('e')! = -1) {b = i; found = true;}
i ++;
 }
return (b);

Use Switch and Else

In many cases, series of -conditions are clear, but not the quickest way to solve the query. In the case of double values ​​(such as / or /), it is generally advisable to use a -condition for the second case (since the condition cannot assume any other value anyway). In the case of variables that can contain several values, it is usually better to use one statement instead of countless 's and' s. It is not quite as nice to write, but it is interpreted much faster by the browser. Example:

//instead of:
if (a == 1) {do it ()}
else if {a == 2} {machdies ()}
else if {a == 3} {machjenes ()}
else {wasanderes ()}

//better:
switch (a)
{
case 1: do that (); break;
case 2: machdies (); break;
case 3: machjenes (); break;
default: wasanderes ();

Avoid Eval ()

The method is often used by many programmers because it allows new code to be inserted out of nowhere, but it has certain disadvantages for the speed of the script. Because the browser has to process the code again, the operation of the script is slowed down. In the case of relatively secure procedures (e.g. when it is only decided whether one or the other object is used), it is often better to include one more query. In the case of nested (sub-) objects that are only defined by the method, it is often more intelligent (and faster) to call the sub-object using the name in square brackets instead of the method. Example:

//instead of:
function setValue (a, b)
{eval ('document.all.' + a + '. style.color =' + b); }

//better:
function setValue (a, b)
{document.all [a] .style.color = b; }

Internal and external

There is a certain difference whether or not you have to intervene in the rendering (display of the browser). In principle, it is advisable to do this as little as possible. Avoid, for example, generating a page within a loop and rather save the generated content and have it output after the loop. Example:

//instead of:
document.open ();
for (i = 0; i <1000; i ++) {document.writeln ('pass:' + i + '
');}
document.close ();

//better:
var s = '';
document.open ();
for (i = 0; i <1000; i ++) {s + = 'pass:' + i + '
';}
document.write (s);
document.close ();

about us