Memory consumption (HTML element x jQuery objects)

Asked

Viewed 223 times

2

I’m creating a system where I need to record many DOM elements in variables.

Is there any difference of memory consumption in writing HTML elements x jQuery objects?

HTML elements:

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

Object jQuery:

var elemento = jQuery('#elemento');

In my layman’s view, I think the first option would consume less memory by just recording the DOM element, and in the second is generated a new jQuery object that carries within it such an element.

Proceeding?

  • using native functions is always "light", but memory should only be a problem if the system is geared towards mobile phones

1 answer

1


In general this is correct. The first records only one element and the second all elements. But this difference may not be so great.

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

Depending on the case can save memory and spend processing to keep picking things that could already be available.

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

  • Well observed! In the first item I would keep only the element, however, whenever I need to use it I would use $( elemento ), where "element" is the variable. Then it would be more feasible to already leave the jQuery object in the variable than to keep creating a new jQuery object whenever using a pure HTML element?

  • 1

    I don’t know your real case, but if you’re going to be using various elements of the object, then it’s better to stay with the object than to keep taking the elements individually.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.