News for hackers. Stuff that matters.
Re:Node.js Best Practices by donbronson on Friday September 19, 2014 @04:29PM (#8342332)
Anyone have more insight into the statement "Avoid closures"? Or rather, an alternative to having private functions?
Re:Node.js Best Practices by kansface on Friday September 19, 2014 @04:50PM (#8342418)
Closures are fine, but avoid seagulls:

function() {

  function() {

     function() {

        function() {

        }

      }

   } 

}
Re:Node.js Best Practices by prezjordan on Friday September 19, 2014 @06:58PM (#8342940)
"Seagulls" amazing! I'm used to calling them pyramids of doom, or laser guns.
Re:Node.js Best Practices by ufo on Friday September 19, 2014 @11:27PM (#8343588)
Looks a bit less like seagulls if you do "});" instead of plain "}" though
Re:Node.js Best Practices by manyxcxi on Friday September 19, 2014 @07:10PM (#8342988)
We call it arrow code. The arrow points down a path of deep despair and frustration.
Re:Node.js Best Practices by possibilistic on Friday September 19, 2014 @10:13PM (#8343459)
I think this is going to catch on.
Re:Node.js Best Practices by comrh on Friday September 19, 2014 @05:24PM (#8342572)
Seagulls?
Re:Node.js Best Practices by bhauer on Friday September 19, 2014 @05:44PM (#8342651)
First I've heard the word used in this context, but I assume it refers to the appearance of the braces forming a 'V' pattern of what look like seagulls.
Re:Node.js Best Practices by ufo on Friday September 19, 2014 @11:07PM (#8343550)
They mentioned memory leaks as their reasoning. Run the following program in any browser and you will see the memory usage balloon out of control:

    var theThing = null;

    function replaceThing(){
      var oldThing = theThing;
      function unused(){ return oldThing }
      theThing = {
        longStr: new Array(1000000).join('*'),
        someMethod: function(){ }
      };
    }

    setInterval(replaceThing, 1000);
V8 (and all other engines I tested) will save the oldThing variable in someMethod's lexical environment record, causing each Thing to keep a reference to the previous Thing, preventing it from being garbage collected. This is despite the fact that the old thing is actually unreachable - someMethod never uses the oldThing variable.

For a more detailed explanation, check out the post that I lifted this example from: http://point.davidglasser.net/2013/06/27/surprising-javascri...

Re:Node.js Best Practices by danneu on Sunday September 21, 2014 @04:06AM (#8346629)
Example of Google's Closure Compiler eliminating the memory leak:

    var theThing = null; 

    function replaceThing() {
      theThing = {
        longStr: Array(1E6).join("*"),
        someMethod: function() { }
      }; 
    } 

    setInterval(replaceThing, 1E3);
http://closure-compiler.appspot.com/home
Re:Node.js Best Practices by manyxcxi on Friday September 19, 2014 @04:38PM (#8342371)
I interpret 'Avoid closures' as just don't define a function within your function. Private functions are simply functions that haven't been exported and aren't really closures (if we're talking about using a module pattern, at least).

Excessive closures can lead to memory leaks. Anonymous closures are a PITA when debugging.

Re:Node.js Best Practices by dkarapetyan on Friday September 19, 2014 @06:21PM (#8342796)
Deeply nested closures are harder to inline and optimize. If you want fast code then the easiest way to accomplish that is with explicitly spelling out everything in the prototype instead of generating closures.