<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Performance Caveat with jQuery Selectors and Live Events</title>
	<atom:link href="http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/</link>
	<description></description>
	<lastBuildDate>Tue, 20 Jul 2010 23:06:58 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>By: Zach Leatherman</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-1985</link>
		<dc:creator>Zach Leatherman</dc:creator>
		<pubDate>Wed, 07 Jul 2010 02:25:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-1985</guid>
		<description>Actually, if you use $(document) or $(document.body), delegate should work very similar to the workaround included above.</description>
		<content:encoded><![CDATA[<p>Actually, if you use $(document) or $(document.body), delegate should work very similar to the workaround included above.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zach Leatherman</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-1982</link>
		<dc:creator>Zach Leatherman</dc:creator>
		<pubDate>Thu, 24 Jun 2010 00:04:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-1982</guid>
		<description>Hey Sam,
Not really, $(&#039;body&#039;) will still execute the selector query.  We want to delay that as long as possible.</description>
		<content:encoded><![CDATA[<p>Hey Sam,<br />
Not really, $(&#8216;body&#8217;) will still execute the selector query.  We want to delay that as long as possible.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam Blowes</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-1981</link>
		<dc:creator>Sam Blowes</dc:creator>
		<pubDate>Wed, 23 Jun 2010 11:27:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-1981</guid>
		<description>My syntax was slightly wrong but you get the idea.

Thanks..</description>
		<content:encoded><![CDATA[<p>My syntax was slightly wrong but you get the idea.</p>
<p>Thanks..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam Blowes</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-1980</link>
		<dc:creator>Sam Blowes</dc:creator>
		<pubDate>Wed, 23 Jun 2010 11:26:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-1980</guid>
		<description>Forgive me if I&#039;m missing something obvious here...
Isn&#039;t the delegate function the right function for this job?
 &lt;code&gt; 
$(&#039;body&#039;).delegate(&#039;&#039;span.myTooltip&#039;&#039;, &#039;mouseover&#039;, function(event) {
    // activate tooltip
}); &lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Forgive me if I&#8217;m missing something obvious here&#8230;<br />
Isn&#8217;t the delegate function the right function for this job?<br />
 <code><br />
$('body').delegate(''span.myTooltip'', 'mouseover', function(event) {<br />
    // activate tooltip<br />
}); </code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: On jQuery&#39;s live() &#171; Paul Irish</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-1839</link>
		<dc:creator>On jQuery&#39;s live() &#171; Paul Irish</dc:creator>
		<pubDate>Fri, 22 Jan 2010 18:00:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-1839</guid>
		<description>[...] uses the jQobj.selector property and context. It&#039;s a waste, I agree. But you can avoid it. Zachleat posted this technique first. Then I stole a code snippet from @janl to show off in my jQuery Performance talk for jQCon. [...]</description>
		<content:encoded><![CDATA[<p>[...] uses the jQobj.selector property and context. It&#39;s a waste, I agree. But you can avoid it. Zachleat posted this technique first. Then I stole a code snippet from @janl to show off in my jQuery Performance talk for jQCon. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Performant .live() in jQuery 1.4 at don&#8217;t explain</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-1832</link>
		<dc:creator>Performant .live() in jQuery 1.4 at don&#8217;t explain</dc:creator>
		<pubDate>Thu, 21 Jan 2010 09:00:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-1832</guid>
		<description>[...] significant optimization I&#8217;ve been using with jQuery 1.3.2 for some time now was documented by Zach Leatherman — when you use .live() (and if you have a dynamic number of elements you should always be using [...]</description>
		<content:encoded><![CDATA[<p>[...] significant optimization I&#8217;ve been using with jQuery 1.3.2 for some time now was documented by Zach Leatherman — when you use .live() (and if you have a dynamic number of elements you should always be using [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: links for 2010-01-20 &#171; BarelyBlogging</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-1830</link>
		<dc:creator>links for 2010-01-20 &#171; BarelyBlogging</dc:creator>
		<pubDate>Thu, 21 Jan 2010 00:33:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-1830</guid>
		<description>[...] Performance Caveat with jQuery Selectors and Live Events (tags: jquery live) [...]</description>
		<content:encoded><![CDATA[<p>[...] Performance Caveat with jQuery Selectors and Live Events (tags: jquery live) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Cardwell</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-1709</link>
		<dc:creator>Dave Cardwell</dc:creator>
		<pubDate>Tue, 10 Nov 2009 00:07:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-1709</guid>
		<description>After looking at how the .live() method is implemented in the jQuery source, I had been doing the following:

&lt;pre&gt;&lt;code&gt;$.extend({ handle: function(type, selector, fn) {
    return $.fn.live.apply({ selector:selector }, [type, fn]);
});&lt;/code&gt;&lt;/pre&gt;

This is called as &lt;code&gt;$.handle(&#039;click&#039;, &#039;blockquote&#039;, function(event) { … });&lt;/code&gt; (read as &lt;em&gt;handle&lt;/em&gt; the &lt;em&gt;click&lt;/em&gt; event for any &lt;em&gt;blockquote&lt;/em&gt;).

I came across your solution when I went to file a feature request (see &lt;a href=&quot;http://dev.jquery.com/ticket/5425&quot; rel=&quot;nofollow&quot;&gt;existing ticket&lt;/a&gt;) and now I’m not sure which I prefer :P

Hopefully an officially supported method will materialise in jQuery 1.4.</description>
		<content:encoded><![CDATA[<p>After looking at how the .live() method is implemented in the jQuery source, I had been doing the following:</p>
<pre><code>$.extend({ handle: function(type, selector, fn) {
    return $.fn.live.apply({ selector:selector }, [type, fn]);
});</code></pre>
<p>This is called as <code>$.handle('click', 'blockquote', function(event) { … });</code> (read as <em>handle</em> the <em>click</em> event for any <em>blockquote</em>).</p>
<p>I came across your solution when I went to file a feature request (see <a href="http://dev.jquery.com/ticket/5425" rel="nofollow">existing ticket</a>) and now I’m not sure which I prefer :P</p>
<p>Hopefully an officially supported method will materialise in jQuery 1.4.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zach Leatherman</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-1708</link>
		<dc:creator>Zach Leatherman</dc:creator>
		<pubDate>Mon, 09 Nov 2009 16:58:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-1708</guid>
		<description>Depends on what event you&#039;re trying to delegate.  I would look at the above comments, it seems like for the normal events supported by live, you can just as easily attach your own event handler to the document node and do it the least abstracted way.  Which in this case, probably would be preferable.</description>
		<content:encoded><![CDATA[<p>Depends on what event you&#8217;re trying to delegate.  I would look at the above comments, it seems like for the normal events supported by live, you can just as easily attach your own event handler to the document node and do it the least abstracted way.  Which in this case, probably would be preferable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jakob</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-1707</link>
		<dc:creator>Jakob</dc:creator>
		<pubDate>Mon, 09 Nov 2009 11:46:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-1707</guid>
		<description>This feels like a  hack

$.live = function(selector, type, fn)
{
    var r = $([]);
    r.selector = selector;
    if(type &amp;&amp; fn) {
        r.live(type, fn);
    }
    return r;
};

allthough it works and it improved performence on my page by reducing the number of calls with 60%.
But is this a preferable way of doing event delegation with jquery?</description>
		<content:encoded><![CDATA[<p>This feels like a  hack</p>
<p>$.live = function(selector, type, fn)<br />
{<br />
    var r = $([]);<br />
    r.selector = selector;<br />
    if(type &amp;&amp; fn) {<br />
        r.live(type, fn);<br />
    }<br />
    return r;<br />
};</p>
<p>allthough it works and it improved performence on my page by reducing the number of calls with 60%.<br />
But is this a preferable way of doing event delegation with jquery?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zach Leatherman</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-979</link>
		<dc:creator>Zach Leatherman</dc:creator>
		<pubDate>Sat, 23 May 2009 15:08:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-979</guid>
		<description>@Diego

In my stuff, there isn&#039;t too huge of a performance bottleneck on the callback execution.  I&#039;m a bigger stickler for onload performance. :)

I would agree that the &quot;hack&quot; presented above maybe isn&#039;t the most forward thinking approach.  Perhaps a hybrid approach with what redsquare presented is in order.

&lt;code&gt;
$(document).bind(&#039;click&#039;, function(event)
{
    if($(event.target).is(...)) {
        ...
    }
});
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>@Diego</p>
<p>In my stuff, there isn&#8217;t too huge of a performance bottleneck on the callback execution.  I&#8217;m a bigger stickler for onload performance. :)</p>
<p>I would agree that the &#8220;hack&#8221; presented above maybe isn&#8217;t the most forward thinking approach.  Perhaps a hybrid approach with what redsquare presented is in order.</p>
<p><code><br />
$(document).bind('click', function(event)<br />
{<br />
    if($(event.target).is(...)) {<br />
        ...<br />
    }<br />
});<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zach Leatherman</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-978</link>
		<dc:creator>Zach Leatherman</dc:creator>
		<pubDate>Sat, 23 May 2009 15:05:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-978</guid>
		<description>Great comments guys!

@redsquare

From my perspective, I want the code simplicity of being able to not perform a selector at all to improve both onload performance, and alleviate having to add event handlers later when I add new content using ajax.  The live method (in it&#039;s current form) addresses only the latter, not the former.

If I used &lt;code&gt;if($(event.target))&lt;/code&gt;, then I&#039;d still have to perform SOME sort of selector to bind the callback to an element, instead of just using the document object.

Maybe I&#039;m being super picky here, but in my tests, the performance difference is large enough to be picky about.</description>
		<content:encoded><![CDATA[<p>Great comments guys!</p>
<p>@redsquare</p>
<p>From my perspective, I want the code simplicity of being able to not perform a selector at all to improve both onload performance, and alleviate having to add event handlers later when I add new content using ajax.  The live method (in it&#8217;s current form) addresses only the latter, not the former.</p>
<p>If I used <code>if($(event.target))</code>, then I&#8217;d still have to perform SOME sort of selector to bind the callback to an element, instead of just using the document object.</p>
<p>Maybe I&#8217;m being super picky here, but in my tests, the performance difference is large enough to be picky about.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Diego Perini</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-977</link>
		<dc:creator>Diego Perini</dc:creator>
		<pubDate>Sun, 10 May 2009 23:47:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-977</guid>
		<description>Sure, removing the first selection can improve things during initialization, also I am not sure the way you are doing it is safe for all the tricks of jQuery internals.

Strictly speaking about the performances, you just scratched the surface of the problem and your cure is not enough (for performances, I repeat).

A &quot;match(element, selector)&quot; method is best suited than a &quot;select(selector, from)&quot; method for a fast event delegation. j

jQuery does not currently implement such a method, it does that by using a standard &quot;select()&quot; then look in the result set and see if the searched element is in there (slow/slow).

The event target should be passed to the &quot;match(element, selector)&quot; method. Only that element should be matched against the &quot;selector&quot;, not all the elements in the document as it currently happens in most implementations.

With event delegation these selection operations have to be repeated continuously for most events while users interact with the pages. You can easily see the selection method itself is the real bottleneck here.</description>
		<content:encoded><![CDATA[<p>Sure, removing the first selection can improve things during initialization, also I am not sure the way you are doing it is safe for all the tricks of jQuery internals.</p>
<p>Strictly speaking about the performances, you just scratched the surface of the problem and your cure is not enough (for performances, I repeat).</p>
<p>A &#8220;match(element, selector)&#8221; method is best suited than a &#8220;select(selector, from)&#8221; method for a fast event delegation. j</p>
<p>jQuery does not currently implement such a method, it does that by using a standard &#8220;select()&#8221; then look in the result set and see if the searched element is in there (slow/slow).</p>
<p>The event target should be passed to the &#8220;match(element, selector)&#8221; method. Only that element should be matched against the &#8220;selector&#8221;, not all the elements in the document as it currently happens in most implementations.</p>
<p>With event delegation these selection operations have to be repeated continuously for most events while users interact with the pages. You can easily see the selection method itself is the real bottleneck here.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: redsquare</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-976</link>
		<dc:creator>redsquare</dc:creator>
		<pubDate>Sun, 10 May 2009 23:40:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-976</guid>
		<description>I agree with your comments. If performance is critical I would advocate the use of the more traditional delegation method where you delegate an event to a closer contextual element than the document e.g container div or table etc and using the following format
&lt;code&gt;
if ( $(ev.target).is(&#039;a&#039;) ) 
&lt;/code&gt;.
IIRC on a small test I did it uses 50% less method calls  performs 25% quicker than the live method. I think you can safely scale out these numbers on a large complicated dom.</description>
		<content:encoded><![CDATA[<p>I agree with your comments. If performance is critical I would advocate the use of the more traditional delegation method where you delegate an event to a closer contextual element than the document e.g container div or table etc and using the following format<br />
<code><br />
if ( $(ev.target).is('a') )<br />
</code>.<br />
IIRC on a small test I did it uses 50% less method calls  performs 25% quicker than the live method. I think you can safely scale out these numbers on a large complicated dom.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://www.zachleat.com/web/2009/05/08/performance-caveat-with-jquery-selectors-and-live-events/comment-page-1/#comment-972</link>
		<dc:creator>James</dc:creator>
		<pubDate>Fri, 08 May 2009 13:33:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.zachleat.com/web/?p=211#comment-972</guid>
		<description>Very interesting. I never even thought of the overhead caused by the initial selection. A good point and definitely something that needs improving in the jQuery core!</description>
		<content:encoded><![CDATA[<p>Very interesting. I never even thought of the overhead caused by the initial selection. A good point and definitely something that needs improving in the jQuery core!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
