We know that this anonymous function calls the returned function (the function with the identifier g), and then assigns the value to the external f
After learning about these non-conforming code parsing bugs, if we use them, we will find the memory is actually faulty. Let's look at an example:
var f = (function(){ if (true) { return function g(){}; } return function g(){}; })();
We know that this anonymous function calls the returned function (a function with the identifier g) and then assigns the value to the external f. We also know that naming a function expression will lead to the generation of redundant function objects, and this object is not the same as the returned function object. Therefore, this excess g function will die in the closure of the return function, so the memory problem will occur. This is because the function and g in the if statement are declared in the same scope. In this case, unless we explicitly disconnect the reference to the g function, it keeps occupying the memory.
Var f = (function () {var f, g; if (true) {f = function g () {};} else {f = function g (){};} // After g is set to null, it will no longer occupy the memory. g = null; return f ;})();
By setting g to null, the garbage collector recycles the implicit function referenced by g. to verify our code, we will perform some tests, to ensure that our memory is recycled.
Test
The test is simple, that is, the namefunction expression creates 10000 functions and saves them in an array. Wait a while and then check the memory occupied by these functions. Then, disconnect the references and repeat the process. The following is the test code:
function createFn(){ return (function(){ var f; if (true) { f = function F(){ return 'standard'; }; } else if (false) { f = function F(){ return 'alternative'; }; } else { f = function F(){ return 'fallback'; }; } // var F = null; return f; })(); } var arr = [ ]; for (var i=0; i < 10000; i++) { arr[i] = createFn(); }
You can see the following results through the Task Manager running in Windows XP SP2:
IE6: without `null`: 7.6K -> 20.3K with `null`: 7.6K -> 18K IE7: without `null`: 14K -> 29.7K with `null`: 14K -> 27K
As expected, it is shown that disconnecting the reference can release the memory, but not a lot of memory is released. Only 10000 function objects can release about 3 MB of memory, which is nothing for some small scripts, however, it is necessary for large programs or devices that run in low memory for a long time.
About Safari 2. JS parsing in x also has some bugs, but the version is relatively low, so we will not introduce it here. If you want to see it, Please carefully check the English documents.
Additional reading
The topic list of this article is as follows:
- How should we understand the working principle of the JavaScript engine?
- JavaScript exploration: the importance of writing maintainable code
- JavaScript exploration: exercise caution when using global variables
- JavaScript exploration: var pre-parsing and side effects
- JavaScript exploration: for Loop (for Loops)
- JavaScript exploration: for-in loop (for-in Loops)
- Exploring JavaScript: Prototypes is too powerful
- JavaScript: eval () is the devil"
- JavaScript exploration: Using parseInt () for Numerical Conversion
- Exploring JavaScript: Basic coding specifications
- JavaScript exploration: function declaration and function expression
- JavaScript exploration: Name function expressions
- JavaScript: function name in the debugger
- JavaScript: JScript Bug
- JavaScript exploration: Memory Management of JScript
- Exploring JavaScript: SpiderMonkey's quirks
- JavaScript exploration: an alternative solution to naming function expressions
- JavaScript exploration: Object
- JavaScript exploration: Prototype chain
- JavaScript exploration: Constructor
- JavaScript probing: executable context Stack
- Execution context 1: Variable object and activity object
- Execution context 2: Scope chain Scope Chains
- Execution context 3: Closure Closures
- Execution context 4: This pointer
- Exploring JavaScript: Powerful prototype and prototype chain
- JavaScript Functions 1: function declaration
- JavaScript function 2: function expressions
- JavaScript function 3: function expressions in a group
- JavaScript function 4: function Constructor
- JavaScript variable object 1: VO Declaration
- JavaScript variable object 2: VO in different execution contexts
- JavaScript variable object 3: two stages of execution Context
- JavaScript variable object IV: Variables
- Property of the JavaScript variable object __parent _
- JavaScript scope chain 1: Scope chain Definition
- JavaScript scope chain 2: function Lifecycle
- JavaScript scope chain 3: Scope chain features
- JavaScript closure 1: Introduction to closures
- JavaScript closure 2: Implementation of closure
- JavaScript closure 3: Closure usage
This article is available at http://www.nowamagic.net/librarys/veda/detail/1637.