Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is finding Elements by a custom attribute efficient?

Tags:

jsoup

I'm wondering whether a select statement like this would be efficient:

elements = document.body().select("[data-custom-attr=blahblah]");

Does JSoup create a Map for all element attributes and values so that it can look them up efficiently, or would this involve a traversal of the entire document?

like image 934
sanity Avatar asked Nov 14 '22 19:11

sanity


1 Answers

Yes, attributes appear to be stored in a LinkedHashMap as of v1.7.2.

org.jsoup.nodes.Attributes:
line 20: private LinkedHashMap<String, Attribute> attributes = null;
line 21: // linked hash map to preserve insertion order.
line 22: // null be default as so many elements have no attributes -- saves a good chunk of memory

I'd be remiss if I didn't tell you a good API is supposed to abstract implementation details away from the programmer. A detail like this isn't supposed to be something most developers are concerned with. Of course, no harm done if it's just for curiosity's sake.

like image 181
Richard Krajunus Avatar answered Feb 16 '23 12:02

Richard Krajunus