jquery context selector vs .find()

2019-06-19 08:11发布

问题:

What is more efficient?

var container = $("#container");

// 1
var links1 = container.find("a");

// 2
var links2 = $("a", container);

I personally prefer $("a", container) because it looks better, but are they different in performance?

回答1:

The context selector $("a", container) is converted to find. find() will be faster but in most cases this could be ignored. I would go for find() as its syntax is quite stright forward for me. This post has performance comparison that would help you deciding which one you would use.