You can profile javascript with firebug and see total calls for
different approaches and how long they take.

That said, $('#id') is fast, since it is basically getElementById() a
native javascript method. However, if you call $('#id') 100x, you'll
see that it's faster to cache it in a variable. e.g

var id = $('#id');
for (i=0;i<100;i++) {
 id.append("<p>number "+i+"</p>");
}

what is the bottleneck in your code?

-j

On Feb 10, 6:12 pm, Michal Popielnicki <[EMAIL PROTECTED]>
wrote:
> Hi there!
>
> I've been dealing with some performance clean-up of a code of a web-
> app that I wrote and I was wondering if jQuery does caching of the DOM
> references. Lets say I have a function:
>
> function myFunction(){
> $('#foo').attr("bar");
>
> }
>
> I frequently refer to the function, so is it the case that each time I
> launch it, the DOM is scanned for element with "foo" id? Or maybe this
> reference is somehow cached?
>
> If its not, then maybe doing something like this would speed up the
> code execution:
>
> function DomLinker(){
> this.foo = $('#foo').attr("bar");
>
> function myFunction(){
> domMember.foo.attr("bar");}
>
> $(document).ready(
> domMember = new DomLinker();
>
> }
>
> Please keep in mind that we talk about case of many executions of
> myFunction
>
> Please share your thoughts on this one.
>
> Best regards

Reply via email to