There are serveral guidelines to aid the javascript programmers in writing fast code. Some of the guidelines are based on well-known static compliler optimization techniques, while others, focus on Javascript Language specialities.

 

1. Use the local Variables.

Every times, when JS code is running, the engin should scope all code to find the variable we are using, this is a long-time excution. So in our codes, we  must avoid to use global variables. If a variable can be defined in a function, or a class, then stop to define it as a global variables.

 

2. Caching object Members.

In JS code, when need to access object members for serveral times, it would be better to cache it into a local variable. Because accessing a local variable will be more efficient compared to access to object member.

Here, I have another point, when access to DOM element in JS code, I prefer to cache the element into a local variable, to avoid to find the elelment each times when we use it.

 

3. Avoiding "With"

With using of "With", will help us to write code faster, but it will increase the execution time. A good suggestion would be caching the object into a local variable, and access it with the local variable.

 

4. Creating Object,

The most important sugggestion of guidelines about object creation is to avoid creating objects like in Object-Oriented languages, since the kind to object creation has to be solved throught a function call.It is proposed to use the JS Object Notation(JSON) form. For example:

Var obj = {name:"test", gender:"f"}

 

5. Avoid eval

Evan function evaluates a string and executes it as if were script code. This language feature can help hiding the script code, but the function has its own cose. Each string that is passed to Eval has to be parsed and executed it on-the-fly. This cost has to be paid every time the execution reaches an eval function call.

 

6. Function inlining.

Steps for performing functions:

a. Allocating space for parameters.

b. Copying the parameters.

c. Resolving the functions name.

d. Execute it.

It would be better to replace the functions call with the body of the function to be called. This is a doubt point, because in writing code, we always say that we need to devide each function into small part, to reuse it in future functions. So, try to analyze it when apply this guildeline.

7.  Optimizing Loop indices.

a. POST-decrement and increment shold be replaced with Pre-decrement and increment.POST de/increment will take more steps to execute it.

b. Decrement should be prefered over incrementing it. Becuase compared to zero is much faster on alomost every architecture than compare to any other number.

 

Here I am listing part of guidelines in JS coding, I will write down the remaining guidelines when I get time.