{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/optimize-jquery-sizzle-element-selector/","result":{"data":{"markdownRemark":{"id":"6bb79586-2643-5191-a87e-a735ff6e0963","excerpt":"Almost every active website worldwide uses jQuery, you can check stats here , but using it without optimization might make the DOM very slow. The same goes for…","html":"<p>Almost every active website worldwide uses jQuery, you can check stats <a href=\"http://trends.builtwith.com/javascript/jQuery\">here</a> , but using it without optimization might make the DOM very slow. The same goes for other javascript libraries, such as SizzleJS. To ensure the performance of your DOM, you have to follow some best practices for it.</p>\n<p>In this article I am going to list down some of the most critical factors that you need to watch out. Even though this not a complete list; taking care of these will help you optimize those jQuery Selector.</p>\n<h3 id=\"lets-start\" style=\"position:relative;\"><a href=\"#lets-start\" aria-label=\"lets start permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><strong>Let's start!</strong></h3>\n<h4 id=\"always-cache-your-selector\" style=\"position:relative;\"><a href=\"#always-cache-your-selector\" aria-label=\"always cache your selector permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Always cache your selector</h4>\n<p>Whenever you apply any selector in jQuery or <a href=\"http://sizzlejs.com/\">SizzleJS</a>,  the selector engine goes through the whole DOM to find the specified element.</p>\n<p>For example, if you use the code below, it will go through the whole DOM twice in order to find \".myClass\" selector.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;.myClass&quot;</span><span class=\"mtk1\">).</span><span class=\"mtk11\">show</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;.myClass&quot;</span><span class=\"mtk1\">).</span><span class=\"mtk11\">addClass</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;anotherClass&quot;</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>But instead of that, if you make all the methods in a chained format like this. It will only try to find that class once.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;.myClass&quot;</span><span class=\"mtk1\">).</span><span class=\"mtk11\">show</span><span class=\"mtk1\">().</span><span class=\"mtk11\">addClass</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;anotherClass&quot;</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>Or if you want to use this element in other places; you can do so by doing it in this way.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">myElem</span><span class=\"mtk1\"> = </span><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;.myClass&quot;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">myElem</span><span class=\"mtk1\">.</span><span class=\"mtk11\">show</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">myElem</span><span class=\"mtk1\">.</span><span class=\"mtk11\">addClass</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;anotherCLass&quot;</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>In both of these cases, the selector will be executed only once. Some selectors are very slow to traverse and passing them again and again will make your DOM very slow.</p>\n<p>Read on the next point to understand,  how the type of selector affects performance.</p>\n<h4 id=\"prioritizing-selectors-based-on-their-performance\" style=\"position:relative;\"><a href=\"#prioritizing-selectors-based-on-their-performance\" aria-label=\"prioritizing selectors based on their performance permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Prioritizing selectors based on their performance</h4>\n<p>Selector's type affects the performance of your site. SizzleJS is a smart selector engine that also uses native js APIs for finding specific element. This is the main reason why ID selector and tag selector perform faster than others. But, if you prefer using jQuery, it’s pretty much the same. Modern browsers also have an API to find an element by class name but, let’s just focus on jQuery and SizzleJS.</p>\n<ul>\n<li>The order of selector's performance (fast -> slow) is</li>\n<li>ID selector ($(\"#ID\")) = Fastest</li>\n<li>Tag ($(\"Tag\")) = Fast</li>\n<li>Class ($(\".Class\")) = Average</li>\n<li>Attribute ($(\"[Attribute='Value']\")) = Slow</li>\n<li>Pseudo ($(\":pseudo\")) = Slower</li>\n</ul>\n<p>You can verify performance. In some exceptional cases, the selection of those tags does not matter; It’s all in the combination of the selectors. Because, it affects the performance of your site, let's discuss this on next point.</p>\n<h4 id=\"selecting-id-selector-first-and-then-other-ones\" style=\"position:relative;\"><a href=\"#selecting-id-selector-first-and-then-other-ones\" aria-label=\"selecting id selector first and then other ones permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Selecting ID selector first and then other ones</h4>\n<p>If you have the combination of selectors, then the sequence of selectors matter for optimization. For example:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;#someId div .someClass&quot;</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>The same code can be written as:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;#someId&quot;</span><span class=\"mtk1\">).</span><span class=\"mtk11\">find</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;div .someClass&quot;</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>Both of these variant represent the same thing but in the term of performance, second one is better. The reason for that is because in the first code, Sizzle will go through the DOM 3 times to find #someId, div, and .someClass.</p>\n<p>In the second one, the selector engine will go through the DOM again but, this time, it’ll only look for #someId and then find the rest inside that element without going through the DOM again.</p>\n<p>See how this will affect performance.</p>\n<h4 id=\"being-more-specific-in-right-hand-side-instead-of-left-hand-side\" style=\"position:relative;\"><a href=\"#being-more-specific-in-right-hand-side-instead-of-left-hand-side\" aria-label=\"being more specific in right hand side instead of left hand side permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Being more specific in right hand side instead of left hand side</h4>\n<p>Sizzle executes selector from right to left so it will definitely  improve performance if applied in right except left.</p>\n<p><strong>Unoptimized code:</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">$</span><span class=\"mtk1\">( </span><span class=\"mtk8\">&quot;div.myclass .myChildClass&quot;</span><span class=\"mtk1\"> );</span></span></code></pre>\n<p><strong>Optimized code:</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">$</span><span class=\"mtk1\">( </span><span class=\"mtk8\">&quot;.myclass td.myChildClass&quot;</span><span class=\"mtk1\"> );</span></span></code></pre>\n<p>If you don’t see the difference, find the div and td.</p>\n<h4 id=\"selection-inside-a-parent-always-improves-performance\" style=\"position:relative;\"><a href=\"#selection-inside-a-parent-always-improves-performance\" aria-label=\"selection inside a parent always improves performance permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Selection inside a parent always improves performance</h4>\n<p>When you have a context, or any level of parent, then you can select an element inside that parent. It will perform better this way than selecting it directly. Because, in this case, the selector engine goes through the DOM once to find the parent.</p>\n<p>For example, assuming you are trying to find “.child” class:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;.child&quot;</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>Is slower than</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">parent</span><span class=\"mtk1\"> = </span><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;#parent&quot;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">parent</span><span class=\"mtk1\">.</span><span class=\"mtk11\">find</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;.child&quot;</span><span class=\"mtk1\">).</span><span class=\"mtk11\">show</span><span class=\"mtk1\">();</span></span></code></pre>\n<p>You can also specify context by following syntax</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;.child&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">parent</span><span class=\"mtk1\">).</span><span class=\"mtk11\">show</span><span class=\"mtk1\">();</span></span></code></pre>\n<h4 id=\"excessive-selector-slows-down-your-query\" style=\"position:relative;\"><a href=\"#excessive-selector-slows-down-your-query\" aria-label=\"excessive selector slows down your query permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Excessive selector slows down your query</h4>\n<p>The selector engine always checks every selector you have specified and it might traverse slowly. That being said, always make sure to specify minimum selectors in order to maintain the performance.</p>\n<p>For example, you are  trying to find “.myClass” using both of these code variants,</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;#div div span.myClass&quot;</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>Is slower than</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"11\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;#div&quot;</span><span class=\"mtk1\">).</span><span class=\"mtk11\">find</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;.myClass&quot;</span><span class=\"mtk1\">);</span></span></code></pre>\n<h4 id=\"\" style=\"position:relative;\"><a href=\"#\" aria-label=\" permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h4>\n<p><strong>The .children() tag is quicker than .find()</strong></p>\n<p>In case, you are trying to find a children element, it is recommended to use .children() instead of .find(). Using .find() will tell jQuery to look on every level of children, while .children() will find only the first level children. Therefore .children() is faster than .find().</p>\n<p>For example, you are trying to find “.child” inside $parent and it is the first level children of the $parent.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"12\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">parent</span><span class=\"mtk1\">.</span><span class=\"mtk11\">find</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;.child&quot;</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>Is slower than</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"13\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">parent</span><span class=\"mtk1\">.</span><span class=\"mtk11\">children</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;.child&quot;</span><span class=\"mtk1\">).</span><span class=\"mtk11\">show</span><span class=\"mtk1\">();</span></span></code></pre>\n<h4 id=\"-1\" style=\"position:relative;\"><a href=\"#-1\" aria-label=\" 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h4>\n<p>Use minimum DOM append</p>\n<p>DOM manipulation is very heavy so always try to ignore or minimize using it.</p>\n<p>For example, by using the code below, it will make the process sluggish because you didn’t apply any selector caching. Resulting in going through  the DOM ten times and appending an element.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"14\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\">( </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">i</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">0</span><span class=\"mtk1\">; </span><span class=\"mtk12\">i</span><span class=\"mtk1\"> &lt; </span><span class=\"mtk7\">10</span><span class=\"mtk1\">; </span><span class=\"mtk12\">i</span><span class=\"mtk1\">++) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;.myClass&quot;</span><span class=\"mtk1\">).</span><span class=\"mtk11\">append</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;&quot;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p>But instead of using the above code, using the code below will solve the whole issue of appending and traversal. Not only that, it will merge the 10 times manipulation of DOM into a single call.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"15\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">myClassInnerHtml</span><span class=\"mtk1\"> = </span><span class=\"mtk8\">&quot;&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\">( </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">i</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">0</span><span class=\"mtk1\">; </span><span class=\"mtk12\">i</span><span class=\"mtk1\"> &lt; </span><span class=\"mtk7\">10</span><span class=\"mtk1\">; </span><span class=\"mtk12\">i</span><span class=\"mtk1\">++ ){</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">myClassInnerHtml</span><span class=\"mtk1\"> += </span><span class=\"mtk8\">&quot;&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;.myClass&quot;</span><span class=\"mtk1\">).</span><span class=\"mtk11\">append</span><span class=\"mtk1\">(</span><span class=\"mtk12\">myClassInnerHtml</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>All the tips I have mentioned above is highly dependant on your requirement but one thing is for sure; Optimization will definitely improve your process.  ‘SizzleJS’ is most the powerful and quick element selector. But, without writing optimized code you can’t prevent the DOM from freezing. With that being said,  jQuery is awesome but without optimized code it can get more DOM freezes and frustrate your users.</p>\n<p>I hope this help you optimize your element selecting. Thank you and have a great coding.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n</style>","headings":[{"value":"Let's start!","depth":3},{"value":"Always cache your selector","depth":4},{"value":"Prioritizing selectors based on their performance","depth":4},{"value":"Selecting ID selector first and then other ones","depth":4},{"value":"Being more specific in right hand side instead of left hand side","depth":4},{"value":"Selection inside a parent always improves performance","depth":4},{"value":"Excessive selector slows down your query","depth":4},{"value":"","depth":4},{"value":"","depth":4}],"fields":{"slug":"/engineering/optimize-jquery-sizzle-element-selector/"},"frontmatter":{"metatitle":null,"metadescription":null,"description":null,"title":"Optimize jQuery & Sizzle Element Selector","canonical":null,"date":"November 05, 2015","updated_date":null,"tags":["Engineering","JQuery"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1,"src":"/static/4eb8d5a3c886c469b47f2341713e60af/e8bcc/jquery-sizzle-element-selector-150x150.webp","srcSet":"/static/4eb8d5a3c886c469b47f2341713e60af/e8bcc/jquery-sizzle-element-selector-150x150.webp 150w","sizes":"(max-width: 150px) 100vw, 150px"}}},"author":{"id":"Kundan Singh","github":null,"bio":"Director of Product Development @ LoginRadius.","avatar":null}}}},"pageContext":{"id":"6bb79586-2643-5191-a87e-a735ff6e0963","fields__slug":"/engineering/optimize-jquery-sizzle-element-selector/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}