ecometer

Use local variables

(Development)
#37

When you call a global variable in JavaScript, the interpreter must check that:
- it exists in the current scope, in the scope above, etc. - the variable has a value - and more

To avoid all these checks, it is often possible to make useful variables local by transforming them into routine arguments. This process saves CPU time.

Instead of:

var aGlobal = new String('Hello Dolly'); 
function globalLength() {
    length = aGlobal.length; console.log(length);
}
globalLength();

write:

var aGlobal = new String('Hello Dolly'); 
function someVarLength(str) {
    length = str.length; console.log(length);
}
someVarLength(aGlobal);
This best practice should only be applied if it is coherent with your project's specifications.
Under CC-By-NX-SA license