Memory Consumption (HTML Element x jQuery Objects)

2

I'm creating a system where I need to write many DOM elements into variables.

Is there any difference in memory consumption in writing HTML elements vs. jQuery objects?

HTML elements:

var elemento = jQuery('#elemento')[0]; //mesmo que document.getElementById('elemento')

jQuery object:

var elemento = jQuery('#elemento');

In my lay vision, I think the first option would consume less memory by just writing the DOM element, and in the second is generated a new jQuery object that carries inside that element.

Proceed?

    
asked by anonymous 05.11.2015 / 13:36

1 answer

1

In general this is correct. The first one only records one element and the second all elements.

But this difference may not be that great.

And this can be stored for a short time and then collected, so it makes little difference if you are taking up too much memory.

Depending on the case, you can save memory and spend processing to keep getting things that might already be available.

Only a test with the actual situation can indicate which is the best use. And you should only worry about this if you notice that there are problems in real situations.

    
05.11.2015 / 13:44