Hacker News new | past | comments | ask | show | jobs | submit login

When they say 'avoiding closures', how does that relate to functions in your module? Your module is often exported as a function, so are they suggesting that every function be exported? I suspect I'm not understanding the logic behind 'stack based'. Why is it better to have your functions not contain other functions (or is it not be contained?)



It appears they are warning about using closures everywhere to avoid memory leaks.

> are they suggesting that every function be exported?

No. Just export the functions that are used outside of the module. Other functions can be private to the module.

> Why is it better to have your functions not contain other functions (or is it not be contained?)

1. It can make the code clearer

2. It eliminates one source of memory leaks

3. It allows the V8 runtime to optimize more of your code. There is a function length limit (including comments) before V8 will no longer optimize a function.

module.exports = function fOne() { function fTwo() {

    }

    do something...
    fTwo();
}

Can become:

function fTwo() {

}

module.exports = function fOne() { do something... fTwo(); }


v8 (and all other JS engines I tested) are kind of stupid when it comes to garbage collection. You can see a detailed explanation of the problem in this blog post:

http://point.davidglasser.net/2013/06/27/surprising-javascri...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: