Four basic data access locations in 1.JavaScript: literal, local variable, array element, object member.
In general: [literal, local variable] run speed >[Array, object member]
2. The Internal property contains a collection of objects in the scope in which the function is created. This set is called the scope chain.
3. Execute function-> Create an execution environment-> create the active object (that is, the function run-time variable object).
So calling the same function multiple times can cause multiple execution environments to be created.
4. Function Execution procedure
Each time a variable is encountered, it undergoes an identifier resolution process, where the data is obtained or stored. This procedure searches the scope chain of the execution environment. It is this search process that affects performance.
5. Identifier Resolution performance
Global variables always exist at the very end of the execution environment scope. Local variables are the first cis-bit parsing.
Rule of thumb: if a value across a scope is referenced more than once in a function, store it in a local variable.
Such as:
function Initui () {
var bd=document.body;
There are several times after doucument this global object's call
}
//->
function Initui () {
var doc=document;
Bd=doc.body;
Store a reference to this global object in the local variable doc doucument
6. Changing the scope chain
In general, the scope chain of an execution environment does not change.
<1>with can temporarily change the scope chain
Width is used to create a variable for all properties of an object
function Initui () {with
(document) {
var bd=body
}
}
When the code executes to with, the scope chain of the execution environment is temporarily changed . A new variable object is created, which contains all the properties of the parameter-specified object. This object is pushed into the scope chain first , so all the local variables are in the second scope chain object of the cab, so the access costs are higher.
<2>try-catch
When an error occurs in a try statement, the execution automatically jumps to the catch. the exception object is then pushed into a variable object and placed at the top of the scope.
Note : Once the Catch child statement has been executed, the scope chain returns to its previous state.
7. Closure-triggered performance problems
Closures are one of the most powerful features of JavaScript.
Because closures contain references that perform the same objects as the scope chain of the environment, the active object of the function is not destroyed, resulting in more memory overhead.
focus on performance points: When you access a cross scoped identifier frequently, each visit can cause a performance penalty.
Start:19:41:45 2015-11-21 references from by aaron:http://www.cnblogs.com/aaronjs/p/3370176.html
8. Memory leaks
A memory leak is a piece of allocated memory that is neither available nor recyclable until the browser process finishes. In C + +, memory leaks are a frequent occurrence because of the manual memory management. And now popular in C # and Java and other languages using automatic garbage collection method to manage memory, normal use of the case almost no memory leaks. Memory is also managed by the automatic garbage collection method in the browser, but a memory leak can occur because of a bug in the browser garbage collection method.
Several cases of memory leaks
- Circular Reference
- JavaScript closures
- Dom Insertion Order
A DOM object that is referenced by a JavaScript object, while referencing the same or other JavaScript object, may cause a memory leak. The reference to this DOM object will not be reclaimed by the garbage collector when the script stops. To break a circular reference, a reference to an object or DOM object referencing a DOM element needs to be assigned null.
The concrete is discussed in depth, the summary here
- JS memory leaks, no wonder is removed from the DOM element, but there are still variables or objects referencing the DOM object. Then it cannot be deleted in memory. Makes the browser's memory footprint high. This memory footprint is automatically released as the browser refreshes.
- In another case, is circular reference, a DOM object and JS object to each other, which caused a more serious situation, even if the refresh, memory will not be reduced. This is the strict sense of the memory leak.
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!