Does DOM size affect $ ("# id") performance?

I was wondering if the DOM size affects the performance for selecting items by id.

I thought to keep the content loaded via ajax in the DOM and hide it when it is no longer needed. That way, I could just show it if necessary. This, of course, would inflate the DOM, so if it had a negative effect when choosing by identifier, I would not want to do this. (Because performance is what I strive for first).

+3
source share
1 answer

When using $ ("# id"), jquery will still need to scroll through the elements to find the correct one, so adding to the DOM will make it slower, but it will only be slow when the DOM is massive.

It depends on how much you want to add to the DOM. A few hundred lines will not matter much in new browsers, problems with IE8 and lower may occur.

I suggest making a test in the oldest browser that you are going to support, if it works fine, then it should be fine on others.

-2
source

All Articles