1

When I ran the profiler on a JQuery intensive page while tuning for performance, I noticed that these JQuery functions take significant amount of time (in that order)

dir (9.42%)
Sizzle (3.79%)
filter (3.79%)

My jquery functions are too large to paste here. Can I get pointers on which JQuery functions internally call these functions?

Also, is there a way to get a tree of JS functions using the IE8 profiler (or any other way) that tells me which JS function called which one?

Thanks

Nick
  • 7,475
  • 18
  • 77
  • 128

1 Answers1

2

Most of the internals of jQuery are private, anonymous functions. I don't see how you can profile this. (I've tried as well, with no luck).

Sizzle is the selector-engine powering jQuery. And I suspect that Sizzle uses filter() a lot when querying the DOM, using more complex selectors.

So, at least those function-calls are "ok" and works as intended, and are optimized as much as possible, speed-wise.


Update:

This is the implementation of the dir() function:

jQuery.dir = function( elem, dir ){
    var matched = [], cur = elem[dir];
    while ( cur && cur != document ) {
        if ( cur.nodeType == 1 )
            matched.push( cur );
        cur = cur[dir];
    }
    return matched;
};

nodeType 1 is an ELEMENT_NODE. The dir() function filters (or "sanitizes" is you will) the result-set of a given query, to make sure that only DOM elements are returned, and not attributes, text-nodes and so on. (Weather dir() gets executed, is most likely determined by Sizzle).


To conclude:

You can't skip any of these functions, since they are the core of actually being able to get a hold of anything in the DOM. (Doing a query).


Every time you query the DOM ( $("#something") ) these functions will get executed. On simple queries, perhaps only one or two of the three functions will get executed. Thats probably the reason why there are variations in the number of calls.


As Chetan Sastry points out: You can gain a lot of performance caching your queries and so forth. Have a look at this post for some neat jQuery tricks.

Community
  • 1
  • 1
cllpse
  • 21,396
  • 37
  • 131
  • 170
  • I am trying to see if I can ignore calling the JQuery function that internally calls these JQuery functions. I am unable to identify which JQuery function in my script is triggering these calls – Nick Sep 09 '09 at 18:50
  • 1
    @unknown: Virtually all the jQuery selectors make use of one or all of these functions. Eliminate selectors as much as possible (cache results) and you should see a performance gain. – Chetan S Sep 09 '09 at 19:15