JavaScript Programming best practices: Memory efficiency with Closures

Memory Leaks leading to undesired memory growth and exhaustion are most common production problems in enterprise Node applications. Closures, a powerful semantic feature in functional programs, used pervasively in Node programs are prominent sources of unanticipated leaks.

I will explain three most common use cases where Closures are used in Node. For each, a) sample code, b) the life-span of the Closure context and which variables the closure keeps live, iii) what memory is retained during the Closure life-span. The intention is to help audience understand Closure's execution environment, their implications to memory footprint, and help them develop efficient code.

Many recent problem determination exercises revealed that memory leaks are the most prevalent production issues, and debugging of them reveals that Closures play a major role in it. Disambiguating Closures and programming best practices around it, I believe the Node community will be able to apply it instantly in their day-to-day job. People with strong procedural and object oriented language background will find this information useful, in making the key differentiation between Closures and normal functions.

Server side JavaScript with Node has gained unprecedented popularity as a powerful platform for Cloud / Web development, though the development tools and frameworks are still to mature to complement the productivity. There exists eclipse plugins for Node, but they need improved awareness among the developer community. Brining Node topics to this session are expected to improve this awareness - through implicit indications as well as explicit communication to the wider Eclipse consumers.


Outline/Structure of the Experience Report

An introduction to Closures, how they support the Node.js applications in its asynchronous and event driven programming model, what subtle difference it brings to the perception of live variables and their life span in the context of functions and blocks. The three use cases through which Closure is introduced are: i) Completion handlers where named or anonymous function is passed as a argument to an asynchronous routine, ii) Intermediary functions where an inner function is created in the lexical scope of an outer function, and is returned to the caller for future invocation, iii) Listener functions which gets invoked asynchronously upon certain events occurring in the program. The presentation shows how the memory is retained in each cases, and what are the best practices to follow in each case. It will also use sample code and snapshots taken from heapdump and analysed in Google Chrome developer tools to visually aid the concepts in more impactful manner

Learning Outcome

Post-mortem investigation of production issues is important for businesses running production Node.js applications. It is also important that all developers are aware of the needs/issues as the choices they make during development will affect how effectively production issues can be investigated. This talk will increase the awareness of this important aspect of Node.js applications allowing developers to make choices that enable debugging of production applications. In turn this will result in Node .js increasingly been seen as a platform that is good for production use.

Target Audience

Developers, Architects

schedule Submitted 3 years ago

Public Feedback

comment Suggest improvements to the Speaker
  • Pradeep Balachandran
    By Pradeep Balachandran  ~  3 years ago
    reply Reply

    Hi Gireesh, thanks for the proposal and touching upon a common pitfall in JavaScript programming. Could you please update the abstract on the role of Eclipse IDE or Eclipse Platform in this talk?

    • Gireesh Punathil
      By Gireesh Punathil  ~  3 years ago
      reply Reply

      Pradeep, thanks. I have updated the abstract to articulate the role of Eclipse IDE in this talk: the statement which starts with "Server side Javascript...". Hope this helps.