> I want to improve performance for following snipet when run for 1000 times.
>
> jQuery(".Grid_Item,.Grid_AltItem").each( function() { //begin for-each TR
>         id = jQuery(this).find(".IdCell").text();
>         foo = jQuery(this).find("#foo").val();
>         bar = jQuery(this).find("#bar").val();
>     });

Since there is no document.getElementsByClassName, that selector is
going to take two complete passes through the entire document to get
the two classes, then join the two result sets together. Since you are
dropping into an .each() anyway, you might as well just get all TR
elements and filter them yourself.

The other uses of the jQuery selectors inside the .each() can be
replaced by using the childNodes array of the TR directly.

jQuery("#myTable tr").each( function() {
   if ( !/Grid_Item|Grid_AltItem/.test(this.className) )
      return;
   var kids = this.childNodes;
   var Id = jQuery(kids[2]).text();
   var foo = kids[8].firstChild.value;
   var bar = kids[9].firstChild.value;
   ....
});

For fastest operation, you can substitute this for the Id line:

   var Id = kids[2].textContent || kids[2].innerText;

All of these optimizations only work if you guarantee that the table
is laid out strictly as you specified. Otherwise you'll need to insert
a bunch of error checks to make sure you don't follow empty firstChild
objects etc.

Reply via email to