Performance issue with HTML selector
Hi all,
I've come across a performance issue when using the HTML selector in
jQuery 1.2.6. We are doing an AJAX call that returns a fairly large
chunk of HTML. To insert this output into the page, we are taking
that HTML and making it into a jQuery object ...
$.get(someURL, function(data) {
// Convert the HTML string into jquery so that we can do stuff with
it later
var newrows = $(data);
...
}
The problem is with the $(data) function. In IE7, an 80kb data string
takes 20 seconds to process.
The good news is that we tracked this down to the following line in
the jquery selector code (around line 47 in v1.2.6), where a regexp is
run on the selector to detect whether it is HTML...
var match = quickExpr.exec( selector );
To get around this, we added a quick hack to assume that any string >
1000 chars was HTML and skip the regexp check. This hasn't been
thoroughly tested by any means...
var match;
if (selector.length > 1000) {
match=new Array( "", selector );
}
else {
quickExpr.exec( selector );
}
This solved the performance problem for our application.
I suspect there's a better alternative, or at least a better way to do
something like this properly, so I thought I'd put it out there for
the experts to consider. Any thoughts?
Cheers,
- Roger Barnes