1. Avoid double judgment
var num1 = 5,num2 = 6, result = eval ("NUM1 + num2"), sum = new Function ("Arg1", "arg2", "return arg1 + arg2"); SetTimeout ("sum = num1 + num2", 100); SetInterval ("sum = num1 + num2", 100); First the above code evaluates the evaluation like normal code, but when executed, the code inside the string causes another evaluation operation to be evaluated. Some JavaScript engines are optimized to cache the same eval () execution results.2. Using objects, array literalsvar myObject = {
Name: "Nicholas",
Count:50
};
var myArray = ["Nicholas", "N", "true", "null"), so the speed is the fastest.3, do not repeat the workfunction AddHandler (target, EventType, handler) {
if (target.addeventlistener) {//dom2 Events
Target.addeventlistener (EventType, Handler, false);
} else {//ie
Target.attachevent ("on" + EventType, handler);
}
}
function RemoveHandler (target, EventType, handler) {
if (target.removeeventlistener) {//dom2 Events
Target.removeeventlistener (EventType, Handler, false);
} else {//ie
Target.detachevent ("on" + EventType, handler);
}
} The above function, each call will do the same detection, each test will need to find in the object, in fact, the detection once, to avoid duplication of work there are many ways, the next is ↓4. Lazy load function delay loading functionsfunction
AddHandler(Target, EventType, handler) {
if (Target.addeventlistener) {
AddHandler= function (target, EventType, handler) {//Overwrite the original method, run the function detection for the first time, the next call will not be detected again
Target.addeventlistener (EventType, Handler, false);
};
} else {
AddHandler = function (target, EventType, handler) {
Target.attachevent ("on" + EventType, handler);
};
}
AddHandler (target, EventType, handler); In order to be able to execute the first call, the subsequent call will run directly after the overwritten new function.
}
function
RemoveHandler(Target, EventType, handler) {
if (Target.removeeventlistener) {
RemoveHandler= function (target, EventType, handler) {
Target.addeventlistener (EventType, Handler, false);
};
} else {
RemoveHandler = function (target, EventType, handler) {
Target.detachevent ("on" + EventType, handler);
};
}
RemoveHandler (target, EventType, handler);
It can also be detected at the time of function definition rather than the first run: Var
AddHandler= (function (target, EventType, handler) {if (Target.addeventlistener) {
AddHandler= function (target, EventType, handler) {
Target.addeventlistener (EventType, Handler, false);
};
} else {
AddHandler = function (target, EventType, handler) {
Target.attachevent ("on" + EventType, handler);
};
}
AddHandler (target, EventType, handler);
})();5. Pre-loading conditionallyvar AddHandler = Document.body.addEventListener? Need to be put in front, when the code is parsed in advance of the judgment, without waiting for function call functions (target, EventType, handler) {Target.addeventlistener (EventType, Handler, FALSE);}:function (target, EventType, handler) {target.attachevent ("on" + EventType, handler);}; var RemoveHandler = Document.body.removeEventListener function (target, EventType, handler) { Target.removeeventlistener (EventType, Handler, false)}:function (target, EventType, handler) {target.detachevent (" On "+ EventType, handler);};6. Using bit arithmetic 7, native method, no matter how to optimize the code you write, will not be faster than the original drop. The reason is that the native methods that existed in the browser were written in the lower-level languages (such as C + +), which were compiled into a browser component
Recommended practices for high performance