SlideShare a Scribd company logo
Memory efficiency with Closures
Gireesh Punathil, Software Engineer, IBM
gpunathi@in.ibm.com | @gireeshpunam | https://github.com/gireeshpunathil
Agenda
 Introduction to Closures
 Different Closure Semantics
 Closure use cases
 Real world memory leak example
 Summary
Example of a Closure
Ways to use Closures
Closure
Function passed
as an argument
Function defined in-line in the
asynchronous call
Function defined in the lexical
scope, and passed as argument
to asynchronous call
Function defined in-line as a
listener to an event
Function defined in the lexical
scope, and name passed as
listener to events
Function
retrieved as a
return value
Function defined in the lexical
scope of an outer function, and
returned to the caller
#1
#3
#2
Use case #1: Completion handler
Memory allocation: Closure context is
created when completion handler is
defined
Memory retention: Closure and its
associated data is retained until:
 The completion handler returns
 Timer is cleared
Use case #1: Completion handler
Before the timer fires
After the timer fires
Use case #2: Intermediary function
Memory allocation: Closure context is
created when the inner function is defined
Memory retention: Closure and its
associated data is retained until:
The reference is manually cleared
Use case #2: Intermediary function
Even after the Closure is used up
After manual cleanup: data = null;
Use case #3: Listener functions
Memory allocation: Closure context is
created when the inner function is defined
Memory retention: Closure and its
associated data is retained until:
The listener is manually removed
Use case #3: Listener functions
Long after the run() returns
After removing listener: ev.removeAllListeners();
Real-world leak scenario
Summary
 Closure is a powerful semantic feature in functional programs
 Closure is pervasively used in Node / JavaScript
 Closure ‘remembers’ the environment in which it was created
 This remembrance can cost an inefficient program a lot of memory
 Closures in Node cover five discrete semantics
 In some cases the context is cleared automatically, in some, manually
 Understanding Closures closely is vital to developing efficient node code
Disclaimer
 The information contained in this presentation is provided for informational purposes only.
 While efforts were made to verify the completeness and accuracy of the information contained in this presentation, it is
provided “As Is”, without warranty of any kind, express or implied.
 In addition, this information is based on IBM’s current product plans and strategy, which are subject to change by IBM
without notice.
 IBM shall not be responsible for any damages arising out of the use of, or otherwise related to, this presentation or any
other documentation.
 Nothing contained in this presentation is intended to, or shall have the effect of:
 Creating any warranty or representation from IBM (or its affiliates or its or their supplies and/or licensors); or
 Altering the terms and conditions of the applicable license agreement governing the use of IBM software.
THANK YOU!Gireesh Punathil | gpunathi@in.ibm.com | @gireeshpunam | https://github.com/gireeshpunathil

More Related Content

Similar to Programming best practices: Memory efficiency with Closures

IBM ProtecTIER Deduplication for Symantec OpenStorage
IBM ProtecTIER Deduplication for Symantec OpenStorageIBM ProtecTIER Deduplication for Symantec OpenStorage
IBM ProtecTIER Deduplication for Symantec OpenStorage
IBM India Smarter Computing
 
Reproducibility in artificial intelligence
Reproducibility in artificial intelligenceReproducibility in artificial intelligence
Reproducibility in artificial intelligence
Carlos Toxtli
 
MQ Guide France - IBM MQ and Containers
MQ Guide France - IBM MQ and ContainersMQ Guide France - IBM MQ and Containers
MQ Guide France - IBM MQ and Containers
Robert Parker
 
MongoDB World 2019: Modern Data Backup and Recovery from On-premises to the P...
MongoDB World 2019: Modern Data Backup and Recovery from On-premises to the P...MongoDB World 2019: Modern Data Backup and Recovery from On-premises to the P...
MongoDB World 2019: Modern Data Backup and Recovery from On-premises to the P...
MongoDB
 
Flutter Forward EXTENDED - Ashimi0x .pdf
Flutter Forward EXTENDED - Ashimi0x .pdfFlutter Forward EXTENDED - Ashimi0x .pdf
Flutter Forward EXTENDED - Ashimi0x .pdf
Utibe5
 
SpringCloudConfig.pptx.pdf
SpringCloudConfig.pptx.pdfSpringCloudConfig.pptx.pdf
SpringCloudConfig.pptx.pdf
Knoldus Inc.
 
Many-to-One Replication for IBM ProtecTIER Deduplication Solutions
Many-to-One Replication for IBM ProtecTIER Deduplication SolutionsMany-to-One Replication for IBM ProtecTIER Deduplication Solutions
Many-to-One Replication for IBM ProtecTIER Deduplication Solutions
IBM India Smarter Computing
 
IRJET- An Efficient Hardware-Oriented Runtime Approach for Stack-Based Softwa...
IRJET- An Efficient Hardware-Oriented Runtime Approach for Stack-Based Softwa...IRJET- An Efficient Hardware-Oriented Runtime Approach for Stack-Based Softwa...
IRJET- An Efficient Hardware-Oriented Runtime Approach for Stack-Based Softwa...
IRJET Journal
 
How to Containerize WebSphere Application Server Traditional, and Why You Mig...
How to Containerize WebSphere Application Server Traditional, and Why You Mig...How to Containerize WebSphere Application Server Traditional, and Why You Mig...
How to Containerize WebSphere Application Server Traditional, and Why You Mig...
David Currie
 
Become an IBM Cloud Architect in 40 Minutes
Become an IBM Cloud Architect in 40 MinutesBecome an IBM Cloud Architect in 40 Minutes
Become an IBM Cloud Architect in 40 Minutes
Andrew Ferrier
 
Presentation BP205 "Custom Controls: Powerful, But Not Rocket Science!" Conne...
Presentation BP205 "Custom Controls: Powerful, But Not Rocket Science!" Conne...Presentation BP205 "Custom Controls: Powerful, But Not Rocket Science!" Conne...
Presentation BP205 "Custom Controls: Powerful, But Not Rocket Science!" Conne...
Martin Meijer
 
IBM Message Hub: Cloud-Native Messaging
IBM Message Hub: Cloud-Native MessagingIBM Message Hub: Cloud-Native Messaging
IBM Message Hub: Cloud-Native Messaging
Andrew Schofield
 
InterConnect 2017 : Git for COBOL and PL/I?—Yes, It Can Make Sense
InterConnect 2017 : Git for COBOL and PL/I?—Yes, It Can Make SenseInterConnect 2017 : Git for COBOL and PL/I?—Yes, It Can Make Sense
InterConnect 2017 : Git for COBOL and PL/I?—Yes, It Can Make Sense
DevOps for Enterprise Systems
 
Cloud Economics
Cloud EconomicsCloud Economics
Cloud Economics
Chris Bailey
 
Developing Integrations for IBM Integration Bus on Cloud
Developing Integrations for IBM Integration Bus on CloudDeveloping Integrations for IBM Integration Bus on Cloud
Developing Integrations for IBM Integration Bus on Cloud
Geza Geleji
 
Webinar: Application Explosion - Rediscovering the Lost Art of Protection Ser...
Webinar: Application Explosion - Rediscovering the Lost Art of Protection Ser...Webinar: Application Explosion - Rediscovering the Lost Art of Protection Ser...
Webinar: Application Explosion - Rediscovering the Lost Art of Protection Ser...
Storage Switzerland
 
Building out a Microservices Architecture with WebSphere Liberty Profile and ...
Building out a Microservices Architecture with WebSphere Liberty Profile and ...Building out a Microservices Architecture with WebSphere Liberty Profile and ...
Building out a Microservices Architecture with WebSphere Liberty Profile and ...
David Currie
 
OpenWhisk Deep Dive: the action container model
OpenWhisk Deep Dive: the action container modelOpenWhisk Deep Dive: the action container model
OpenWhisk Deep Dive: the action container model
Philippe Suter
 
Containerize, PaaS, or Go Serverless!?
Containerize, PaaS, or Go Serverless!?Containerize, PaaS, or Go Serverless!?
Containerize, PaaS, or Go Serverless!?
Phil Estes
 
WebSphere sMash June Product Review
WebSphere sMash June Product ReviewWebSphere sMash June Product Review
WebSphere sMash June Product Review
Project Zero
 

Similar to Programming best practices: Memory efficiency with Closures (20)

IBM ProtecTIER Deduplication for Symantec OpenStorage
IBM ProtecTIER Deduplication for Symantec OpenStorageIBM ProtecTIER Deduplication for Symantec OpenStorage
IBM ProtecTIER Deduplication for Symantec OpenStorage
 
Reproducibility in artificial intelligence
Reproducibility in artificial intelligenceReproducibility in artificial intelligence
Reproducibility in artificial intelligence
 
MQ Guide France - IBM MQ and Containers
MQ Guide France - IBM MQ and ContainersMQ Guide France - IBM MQ and Containers
MQ Guide France - IBM MQ and Containers
 
MongoDB World 2019: Modern Data Backup and Recovery from On-premises to the P...
MongoDB World 2019: Modern Data Backup and Recovery from On-premises to the P...MongoDB World 2019: Modern Data Backup and Recovery from On-premises to the P...
MongoDB World 2019: Modern Data Backup and Recovery from On-premises to the P...
 
Flutter Forward EXTENDED - Ashimi0x .pdf
Flutter Forward EXTENDED - Ashimi0x .pdfFlutter Forward EXTENDED - Ashimi0x .pdf
Flutter Forward EXTENDED - Ashimi0x .pdf
 
SpringCloudConfig.pptx.pdf
SpringCloudConfig.pptx.pdfSpringCloudConfig.pptx.pdf
SpringCloudConfig.pptx.pdf
 
Many-to-One Replication for IBM ProtecTIER Deduplication Solutions
Many-to-One Replication for IBM ProtecTIER Deduplication SolutionsMany-to-One Replication for IBM ProtecTIER Deduplication Solutions
Many-to-One Replication for IBM ProtecTIER Deduplication Solutions
 
IRJET- An Efficient Hardware-Oriented Runtime Approach for Stack-Based Softwa...
IRJET- An Efficient Hardware-Oriented Runtime Approach for Stack-Based Softwa...IRJET- An Efficient Hardware-Oriented Runtime Approach for Stack-Based Softwa...
IRJET- An Efficient Hardware-Oriented Runtime Approach for Stack-Based Softwa...
 
How to Containerize WebSphere Application Server Traditional, and Why You Mig...
How to Containerize WebSphere Application Server Traditional, and Why You Mig...How to Containerize WebSphere Application Server Traditional, and Why You Mig...
How to Containerize WebSphere Application Server Traditional, and Why You Mig...
 
Become an IBM Cloud Architect in 40 Minutes
Become an IBM Cloud Architect in 40 MinutesBecome an IBM Cloud Architect in 40 Minutes
Become an IBM Cloud Architect in 40 Minutes
 
Presentation BP205 "Custom Controls: Powerful, But Not Rocket Science!" Conne...
Presentation BP205 "Custom Controls: Powerful, But Not Rocket Science!" Conne...Presentation BP205 "Custom Controls: Powerful, But Not Rocket Science!" Conne...
Presentation BP205 "Custom Controls: Powerful, But Not Rocket Science!" Conne...
 
IBM Message Hub: Cloud-Native Messaging
IBM Message Hub: Cloud-Native MessagingIBM Message Hub: Cloud-Native Messaging
IBM Message Hub: Cloud-Native Messaging
 
InterConnect 2017 : Git for COBOL and PL/I?—Yes, It Can Make Sense
InterConnect 2017 : Git for COBOL and PL/I?—Yes, It Can Make SenseInterConnect 2017 : Git for COBOL and PL/I?—Yes, It Can Make Sense
InterConnect 2017 : Git for COBOL and PL/I?—Yes, It Can Make Sense
 
Cloud Economics
Cloud EconomicsCloud Economics
Cloud Economics
 
Developing Integrations for IBM Integration Bus on Cloud
Developing Integrations for IBM Integration Bus on CloudDeveloping Integrations for IBM Integration Bus on Cloud
Developing Integrations for IBM Integration Bus on Cloud
 
Webinar: Application Explosion - Rediscovering the Lost Art of Protection Ser...
Webinar: Application Explosion - Rediscovering the Lost Art of Protection Ser...Webinar: Application Explosion - Rediscovering the Lost Art of Protection Ser...
Webinar: Application Explosion - Rediscovering the Lost Art of Protection Ser...
 
Building out a Microservices Architecture with WebSphere Liberty Profile and ...
Building out a Microservices Architecture with WebSphere Liberty Profile and ...Building out a Microservices Architecture with WebSphere Liberty Profile and ...
Building out a Microservices Architecture with WebSphere Liberty Profile and ...
 
OpenWhisk Deep Dive: the action container model
OpenWhisk Deep Dive: the action container modelOpenWhisk Deep Dive: the action container model
OpenWhisk Deep Dive: the action container model
 
Containerize, PaaS, or Go Serverless!?
Containerize, PaaS, or Go Serverless!?Containerize, PaaS, or Go Serverless!?
Containerize, PaaS, or Go Serverless!?
 
WebSphere sMash June Product Review
WebSphere sMash June Product ReviewWebSphere sMash June Product Review
WebSphere sMash June Product Review
 

Recently uploaded

Secure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA ComplianceSecure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
ICS
 
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptxOperational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
sandeepmenon62
 
Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
Alina Yurenko
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
campbellclarkson
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
The Third Creative Media
 
Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.
KrishnaveniMohan1
 
Cost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App DevelopmentCost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App Development
Softradix Technologies
 
Upturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in NashikUpturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in Nashik
Upturn India Technologies
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
Tier1 app
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
Yara Milbes
 
Boost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management AppsBoost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management Apps
Jhone kinadey
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
kalichargn70th171
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
Microsoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptxMicrosoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptx
jrodriguezq3110
 

Recently uploaded (20)

Secure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA ComplianceSecure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
 
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptxOperational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
 
Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
 
Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.
 
Cost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App DevelopmentCost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App Development
 
Upturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in NashikUpturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in Nashik
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
bgiolcb
bgiolcbbgiolcb
bgiolcb
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
 
Boost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management AppsBoost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management Apps
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
Microsoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptxMicrosoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptx
 

Programming best practices: Memory efficiency with Closures

  • 1. Memory efficiency with Closures Gireesh Punathil, Software Engineer, IBM gpunathi@in.ibm.com | @gireeshpunam | https://github.com/gireeshpunathil
  • 2. Agenda  Introduction to Closures  Different Closure Semantics  Closure use cases  Real world memory leak example  Summary
  • 3. Example of a Closure
  • 4. Ways to use Closures Closure Function passed as an argument Function defined in-line in the asynchronous call Function defined in the lexical scope, and passed as argument to asynchronous call Function defined in-line as a listener to an event Function defined in the lexical scope, and name passed as listener to events Function retrieved as a return value Function defined in the lexical scope of an outer function, and returned to the caller #1 #3 #2
  • 5. Use case #1: Completion handler Memory allocation: Closure context is created when completion handler is defined Memory retention: Closure and its associated data is retained until:  The completion handler returns  Timer is cleared
  • 6. Use case #1: Completion handler Before the timer fires After the timer fires
  • 7. Use case #2: Intermediary function Memory allocation: Closure context is created when the inner function is defined Memory retention: Closure and its associated data is retained until: The reference is manually cleared
  • 8. Use case #2: Intermediary function Even after the Closure is used up After manual cleanup: data = null;
  • 9. Use case #3: Listener functions Memory allocation: Closure context is created when the inner function is defined Memory retention: Closure and its associated data is retained until: The listener is manually removed
  • 10. Use case #3: Listener functions Long after the run() returns After removing listener: ev.removeAllListeners();
  • 12. Summary  Closure is a powerful semantic feature in functional programs  Closure is pervasively used in Node / JavaScript  Closure ‘remembers’ the environment in which it was created  This remembrance can cost an inefficient program a lot of memory  Closures in Node cover five discrete semantics  In some cases the context is cleared automatically, in some, manually  Understanding Closures closely is vital to developing efficient node code
  • 13. Disclaimer  The information contained in this presentation is provided for informational purposes only.  While efforts were made to verify the completeness and accuracy of the information contained in this presentation, it is provided “As Is”, without warranty of any kind, express or implied.  In addition, this information is based on IBM’s current product plans and strategy, which are subject to change by IBM without notice.  IBM shall not be responsible for any damages arising out of the use of, or otherwise related to, this presentation or any other documentation.  Nothing contained in this presentation is intended to, or shall have the effect of:  Creating any warranty or representation from IBM (or its affiliates or its or their supplies and/or licensors); or  Altering the terms and conditions of the applicable license agreement governing the use of IBM software.
  • 14. THANK YOU!Gireesh Punathil | gpunathi@in.ibm.com | @gireeshpunam | https://github.com/gireeshpunathil