Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>here's your example in "one" line.</p> <pre><code>this.$OuterDiv = $('&lt;div&gt;&lt;/div&gt;') .hide() .append($('&lt;table&gt;&lt;/table&gt;') .attr({ cellSpacing : 0 }) .addClass("text") ) ; </code></pre> <hr> <p><em>Update</em>: I thought I'd update this post since it still gets quite a bit of traffic. In the comments below there's some discussion about <code>$("&lt;div&gt;")</code> vs <code>$("&lt;div&gt;&lt;/div&gt;")</code> vs <code>$(document.createElement('div'))</code> as a way of creating new elements, and which is "best".</p> <p>I put together <a href="http://jsbin.com/elula3" rel="noreferrer">a small benchmark</a>, and here's roughly the results of repeating the above options 100,000 times:</p> <p><strong>jQuery 1.4, 1.5, 1.6</strong></p> <pre><code> Chrome 11 Firefox 4 IE9 &lt;div&gt; 440ms 640ms 460ms &lt;div&gt;&lt;/div&gt; 420ms 650ms 480ms createElement 100ms 180ms 300ms </code></pre> <p><strong>jQuery 1.3</strong></p> <pre><code> Chrome 11 &lt;div&gt; 770ms &lt;div&gt;&lt;/div&gt; 3800ms createElement 100ms </code></pre> <p><strong>jQuery 1.2</strong></p> <pre><code> Chrome 11 &lt;div&gt; 3500ms &lt;div&gt;&lt;/div&gt; 3500ms createElement 100ms </code></pre> <p>I think it's no big surprise, but <code>document.createElement</code> is the fastest method. Of course, before you go off and start refactoring your entire codebase, remember that the differences we're talking about here (in all but the archaic versions of jQuery) equate to about an extra 3 milliseconds <em>per thousand elements</em>. </p> <p><strong>Update 2</strong></p> <p>Updated for jQuery 1.7.2 and put the benchmark on JSBen.ch which is probably a bit more scientific than my primitive benchmarks, plus it can be crowdsourced now!</p> <p><a href="http://jsben.ch/#/ARUtz" rel="noreferrer">http://jsben.ch/#/ARUtz</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload