1

Let's say for instance that I have the following XML doc, and I need to parse it using jQuery.

    <books>
        <book>
            <title>Gone with the Wind!</title>
        </book
        <book>
            <title>Lord of the Rings</title>
        </book>
        <book>
            <title>4 hour work week</title>
        </book
    </books>

In order to get the 'title' of the first book. I could use the following jQuery function.

    // let's assume xmldoc contains the above document.
    $(xmldoc).find("title").first().text();

This will give me the 'title' of the first book i.e. "Gone with the Wind!". Now, my question here is: Is this the efficient method to obtain the 'title' of the first book?

I am afraid, internally jQuery might be parsing the titles of all the books and then returning me the first(). I wonder, if am I wasting the CPU-cycles in parsing the books that I am not interested in?

Please suggest an alternative, if you can think of any. Thanks in advance!

PS: Please note that, I can't use browser's native javascript APIs, since the rest of the project uses jQuery for XML-parsing already.

5
  • I would assume that jQuery would use the browser's xml parsing api if it is available. Why not check the jQuery source? Commented Apr 9, 2012 at 21:04
  • @VincentMcNabb, Thanks. Your comment made me go look into the jQuery source. I always thought, I couldn't read and understand jQuery :). True, jQuery does use the browser's native function if available. I had set up a break-point and step-through to find that jQuery calls Sizzle.find(). Sizzle.find() in turn uses browser's getElementsByTagName(). So, it does obtain the whole set of names and returned as an Array. The first() call simply returns the first element in that array. Commented Apr 9, 2012 at 21:56
  • no worries :-) Btw jQuery wasn't written by magical gods (I used to feel that way about a lot of things), it just takes a long time to get your head around it at first, because of its size, and also the way the jQuery object is constructed. Actually I find it a little messy. But generally, once you get over the fact that it's so large, you should be able to search for the function you're calling, understand that, and then look at understanding the functions that that calls in turn. et cetera. Commented Apr 10, 2012 at 5:48
  • To that end I often refer to the jQuery Annotated Source when trying to grok jQuery internals. Take a look. Commented Apr 10, 2012 at 6:43
  • But although jQuery is very nice (and useful) for working with HTML, I personally like using XPath for working with XML - and I use it for HTML too. I suggest having a look. Commented Apr 10, 2012 at 10:38

1 Answer 1

4

I can't speak to its efficiency (why not benchmark it?), but here is an alternate expression that uses just a single selector to achieve the same effect:

$( "book:first-child title", xmldoc ).text()

As @VincentMcNabb points out, jQuery tries to use browsers' built-in capabilities wherever possible.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. But, While stepping through I noticed that, jQuery still creates a set of all matched elements and returns the first element. So I think, efficiency-wise it will probably be the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.