Avoid using eval or Function constructor
Using eval or Function constructor is very expensive and requires the script engine to convert the source code every time to executable code.
In addition, using eval to process strings must be interpreted at runtime.
Code that runs slowly:
function addMethod(object, property, code) { object[property] = new Function(code); } addMethod(myObj, 'methodName', 'this.localVar=foo');
Code that runs faster:
function addMethod(object, property, func) { object[property] = func; } addMethod(myObj, 'methodName', function () { 'this.localVar=foo'; });
Avoid using with
Although it is convenient, with requires additional reference search time because it does not know the upper and lower scopes at compile time.
Code that runs slowly:
with (test.object) { foo = 'Value of foo property of object'; bar = 'Value of bar property of object'; }
Code that runs faster:
var myObj = test.object; myObj.foo = 'Value of foo property of object'; myObj.bar = 'Value of bar property of object';
Do not use try-catch-finally in functions with critical performance requirements
try-catch-finally will create a new variable in the current scope each time during runtime, use Exception in execution of allocation statement.
Exception handling should be done at a high level in the script, where exceptions do not occur very frequently, such as outside a loop.
If possible, try to avoid using try-catch-finally entirely.
Code that runs slowly:
var object = ['foo', 'bar'], i; for (i = 0; i < object.length; i++) { try { // do something that throws an exception } catch (e) { // handle exception } }
Code that runs faster:
var object = ['foo', 'bar'], i; try { for (i = 0; i < object.length; i++) { // do something } } catch (e) { // handle exception }
Avoid using global variables
If you are When global variables are used in a function or other scope, the script engine needs to traverse the entire scope to find them.
Variables in the global scope exist throughout the life cycle of the script, and those in the local scope will be destroyed when the local scope is lost.
Code that runs slowly:
var i, str = ''; function globalScope() { for (i=0; i < 100; i++) { str += i; // here we reference i and str in global scope which is slow } } globalScope();
Code that runs faster:
function localScope() { var i, str = ''; for (i=0; i < 100; i++) { str += i; // i and str in local scope which is faster } } localScope();
Avoid functions that are critical to performance requirements Using for-in
The for-in loop requires the script engine to build a list of all enumerable properties and check whether they are duplicates of the previous ones.
If the code in your for loop scope does not modify the array, you can pre-calculate the length of the array and use it to iterate the array in the for loop.
Code that runs slowly:
var sum = 0; for (var i in arr) { sum += arr[i]; }
Code that runs faster:
var sum = 0; for (var i = 0, len = arr.length; i < len; i++) { sum += arr[i]; }
The above is the detailed content of Summarize some functions and statements to avoid using in JavaScript. For more information, please follow other related articles on the PHP Chinese website!