Forgotten Callbacks, Getting a Closure
Tags: showdev, webdev, beginners, programming
Published: 2022-07-08 12:23:00



Reason why JS is adored all over the internet is because of the powerful libraries and tools that empower us in making efficient web pages in very less time. Closure is one of the perks of JS which is used quite a lot. Now, every once while these commonly used phenomenons double cross the developers and fall prey to the dark side. In this blog we will understand how closures and callbacks result in memory leakage.

***Have you heard about [Minify JSON](https://www.lambdatest.com/free-online-tools/json-minify?utm_source=devto&utm_medium=organic&utm_campaign=jul08_sd&utm_term=sd&utm_content=free_tools)? It is a JavaScript library that removes whitespace and comments from blocks of JSON-like content, while preserving its syntax***.

## Closure and Callbacks

When you define any function in javascript, all the data in that parent function can be accessed by any other function that you define within the parent function’s boundaries. Closures provide you with associating the data and referencing to another function within.

    function strongestavenger() {
        var name = "HULK";                     // name is a local variable created by 
                                               // strongestavenger
        function displayName() {               // displayName() is the inner function, a closure
                                alert (name);  // displayName() uses variable declared in 
                                               // the parent function       
                               }
        displayName();    
    }
    strongestavenger();

The strongestavenger would display HULK, when you initiated the function strongestavenger. DisplayName() is the closure that has access to the name which stores HULK in it.

Callbacks as the name itself indicates are nothing but when a function and its components are called.

    var shield_data = getData();
    setInterval(function() {
        var node = document.getElementById('Node');
        if(node) {
            // Do stuff with node and shield_data
            node.innerHTML = JSON.stringify(someResource));
        }
    }, 1000);

Callbacks and closures are basically work on the principle of referencing the data nodes for usage. So what will happen if these nodes are removed, or the data itself is deleted. Both of them will result in Memory leakage and will cripple the browser’s efficiency. DOM node referencing also works in the same manner and their effect is also the same.
Closures and with recursion if not properly used may easily handicap the browsers, mix it with a redundant variables and you have got a recipe for disaster.

***Do you know? [JSON Prettify](https://www.lambdatest.com/free-online-tools/json-prettify?utm_source=devto&utm_medium=organic&utm_campaign=jul08_sd&utm_term=sd&utm_content=free_tools) is a common format for storing data, but it can be hard to read. Prettify JSON will indent the JSON file, make sure stuff is lined up in a similar manner, and will put closing parenthesis in the same space every time.***

## Solution

Old versions of internet explorers had bugs due to which memory leakage due to forgotten callbacks and closures resulted in crashing. The problem then was that they couldn’t differentiate between javascript and DOM nodes cyclic differences. Even in the latest versions of the browsers today are not that efficient that they can reduce the memory leakage to zero. Till the time the node can be reached within the parent (window) function they can’t be swept by the garbage collection.

It is impossible to not use closures while scripting, what can be done is keeping in check where the leakage is happening. Using browser profiling (mentioned in the memory debugging blog) time lapse we can easily detect the increase in memory. Find the root cause, what event or function is causing this surge and then explicitly delete them.

Especially if you are using jQuery, it is imperative that you don’t use eventlisteners too much. If you are writing script which requires events to be made, make events on list rather than making a list of events.


***Check this out: [Base64 decoding](https://www.lambdatest.com/free-online-tools/base64-decode?utm_source=devto&utm_medium=organic&utm_campaign=jul08_sd&utm_term=sd&utm_content=free_tools)- A way of translating text form into binary data so that it can be transmitted more easily via e-mail and HTML form data.***