<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>selector Archives - Creatronix</title>
	<atom:link href="https://creatronix.de/tag/selector/feed/" rel="self" type="application/rss+xml" />
	<link>https://creatronix.de/tag/selector/</link>
	<description>My adventures in code &#38; business</description>
	<lastBuildDate>Thu, 24 Mar 2022 06:57:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Inefficient jQuery Selectors</title>
		<link>https://creatronix.de/inefficient-jquery-selectors/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Mon, 23 Jan 2017 08:03:57 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[efficiency]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[selector]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=426</guid>

					<description><![CDATA[<p>My PyCharm IDE warns me if I use inefficient jQuery selectors: As I am a bit nosy I wanted to know &#8220;how&#8221; inefficient these selectors are. So I compared the inefficient $("#items tbody"); with the optimized $("#items").find("tbody"); Here is the test code: window.onload = function () { test("unoptimized", function () { for(var i = 0;&#8230;</p>
<p>The post <a href="https://creatronix.de/inefficient-jquery-selectors/">Inefficient jQuery Selectors</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>My PyCharm IDE warns me if I use inefficient jQuery selectors:<img decoding="async" class="size-medium wp-image-440 aligncenter" src="https://creatronix.de/wp-content/uploads/2017/01/inefficient_jquery-300x74.png" alt="" width="300" height="74" srcset="https://creatronix.de/wp-content/uploads/2017/01/inefficient_jquery-300x74.png 300w, https://creatronix.de/wp-content/uploads/2017/01/inefficient_jquery.png 376w" sizes="(max-width: 300px) 100vw, 300px" /></p>
<p>As I am a bit nosy I wanted to know &#8220;how&#8221; inefficient these selectors are. So I compared the inefficient</p>
<pre>$("#items tbody");</pre>
<p>with the optimized</p>
<pre>$("#items").find("tbody");</pre>
<p>Here is the test code:</p>
<pre>window.onload = function () {
    test("unoptimized", function () {
        for(var i = 0; i &lt; 100000; i++){
            $("#items tbody");
        }
        assert(true, "Test finished");
    });
    test("optimized", function () {
        for(var i = 0; i &lt; 100000; i++){
            $("#items").find("tbody");
        }
        assert(true, "Test finished");
    });
};</pre>
<p>I used the little test framework from the book <a href="https://amzn.to/3rcfuni">Adventures of the JavaScript Ninja</a></p>
<p><img fetchpriority="high" decoding="async" class="wp-image-444 aligncenter" src="https://creatronix.de/wp-content/uploads/2017/01/jQuery_performance-300x137.png" alt="" width="374" height="171" srcset="https://creatronix.de/wp-content/uploads/2017/01/jQuery_performance-300x137.png 300w, https://creatronix.de/wp-content/uploads/2017/01/jQuery_performance-768x352.png 768w, https://creatronix.de/wp-content/uploads/2017/01/jQuery_performance.png 777w" sizes="(max-width: 374px) 100vw, 374px" /></p>
<p>The results vary from run to run but the optimized version just takes around 55% of the time which makes it almost <strong>twice as fast</strong>.</p>
<p>Though the single execution is in the nanosecond range, if you heavily rely on using jQuery, it might be worthwhile to optimize your selector statements.</p>
<p><a href="https://github.com/jboegeholz/javascript_katas/blob/master/jquery_test.html">Code on GitHub</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>The post <a href="https://creatronix.de/inefficient-jquery-selectors/">Inefficient jQuery Selectors</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
