javascript - Why does throttle set timeout, context and args to null? -


So I'm just trying to understand the code of throttle in Underscore.js.

  _ Throttle = function (funk, wait, options) {great context, args, result; var timeout = null; Var previous = 0; Option || (Option = {}); Var after = function () {false = options.leading === false? 0: _.now (); Timeout = null; Result = Funny Aply (references, args); Reference = args = faucet; }; Return work () {var now = _.now (); If (past & options.leading === wrong) last = now; Var balance = wait - (now - last); Context = this; Args = arguments; If (left & lt; = 0) {explicit timeout (timeout); Timeout = null; Last = now; Result = Funny Aply (references, args); Reference = args = faucet; } And if (! Timeout & amp; options.trailing! == wrong) {timeout = setTimeout (later, remaining); } Return results; }; };   

I was wondering why context, logic and timetable are redundant. I originally thought that they were being set up for garbage collection assistance. To test that I made a big string, passed it in a function and messed up that function. I used to take two snapshots using the Chrome Dev tool - with the first lines which were set to clear the three variables and no comments have been made. Console.log ("hello"); }; Var dummyFunc2 = function (testStr) {console.log ("boom"); } Var dummyFunc3 = function (testStr) {console.log ("STOOP"); } Var testStr = ''; (Var i = 0; i & lt; 1000000; i ++) {testStr + = i; } Var largeThrottled1 = _.throttle (Dummy funk 1, 1000); Var bigger 2 = _.throttle (dummy funk 2, 1000); Var large Thought 3 = _.throttle (dummiefank 3,1000); LargeThrottled1 (testStr); LargeThrottled1 (testStr);

However, it does not seem that there is a lot of difference.

Enter image details here

so why are those lines there ?

I think it is to do with stopping memory leaks, as you've already done . Suppose you have a large number of disturbing jobs, call them with lots of data and then never call them again. Then the function created by _ throttle will still be within the scope of closing the context of that data. Those functions are still accessible in theory, so GC will not be able to clear that data unless you dispose of throttle work. So to set those variables to blank, it ensures that no reference is placed in it that is no longer in use, the memory leak is being stopped.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -