Use local variables


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);


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