One tag, several attrs - faster to process requests using a filter or use multiple selectors?

I am wondering which of the following jQuery queries will be faster. I'm looking for matching any span tags that have either the src attribute or the data-src attribute.

var a = $('span[src],span[data-src]');


var b = $('span').filter('[src],[data-src]');

My gut feeling is (b), but there may be optimizations that I don't know about.

Thank.

UPDATE:

Based on a quick test with the elements 100 x span[src], 100 x span[data-src]and 100 x span: (a) came out 4-8 times faster, depending on the browser. On IE8, it was much faster (about 50 times) and on IE6 / 7 about the same.

What I wonder why is it faster?

(a) jquery querySelectorAll, . , , .

+3
3

http://jsfiddle.net/B9Vmy/1/ ( firebug )

6000 (2000 , 2000 src, 2000, 2000 data-src), 8 , . , jQuery (jQuery (element)).

A , B , .

, , sizzle ... : -)

+1

, , .

span src.

span, span attr src.

, .

,

+1

DOM; - .

The real answer requires profiling your particular case, that is, depending on how complex your DOM is. In addition, different versions of jQuery may produce different results.

+1
source

Source: https://habr.com/ru/post/1796017/


All Articles