Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why .data() function of jQuery is better to prevent memory leaks?

Tags:

People also ask

Which of the following can be done to reduce memory leakage?

Use reference objects to avoid memory leaks Using the java. lang. ref package, you can work with the garbage collector in your program. This allows you to avoid directly referencing objects and use special reference objects that the garbage collector easily clears.

Why should memory leaks be avoided?

Memory leak occurs when programmers create a memory in heap and forget to delete it. The consequences of memory leak is that it reduces the performance of the computer by reducing the amount of available memory.

How do we avoid memory leaks in closures?

Only capture variables as unowned when you can be sure they will be in memory whenever the closure is run, not just because you don't want to work with an optional self . This will help you prevent memory leaks in Swift closures, leading to better app performance.

Is memory leak possible in JavaScript?

In simple words, a memory leak is an allocated piece of memory that the JavaScript engine is unable to reclaim. The JavaScript engine allocates memory when you create objects and variables in your application, and it is smart enough to clear out the memory when you no longer need the objects.


Regarding to jQuery utility function jQuery.data() the online documentation says:

"The jQuery.data() method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks. "

Why to use:

document.body.foo = 52; 

can result a memory leak -or in what conditions- so that I should use

jQuery.data(document.body, 'foo', 52);

Should I ALWAYS prefer .data() instead of using expandos in any case?

(I would appreciate if you can provide an example to compare the differences)

Thanks,

burak ozdogan