<?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>Software Warlock &#187; performance</title>
	<atom:link href="http://softwareblog.morlok.net/tag/performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://softwareblog.morlok.net</link>
	<description></description>
	<lastBuildDate>Sat, 24 Dec 2011 16:06:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>The speed of jQuery.append(&#8230;)</title>
		<link>http://softwareblog.morlok.net/2010/06/22/the-speed-of-jquery-append/</link>
		<comments>http://softwareblog.morlok.net/2010/06/22/the-speed-of-jquery-append/#comments</comments>
		<pubDate>Wed, 23 Jun 2010 04:47:49 +0000</pubDate>
		<dc:creator>Warlock</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://softwareblog.morlok.net/?p=370</guid>
		<description><![CDATA[I&#8217;ve been doing some JavaScript performance optimization recently and I thought I&#8217;d share some of my findings regarding jQuery. The project I&#8217;m working involves creating large DOM trees of elements from data, and I&#8217;ve been trying to speed things up. &#8230;<p class="read-more"><a href="http://softwareblog.morlok.net/2010/06/22/the-speed-of-jquery-append/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing some JavaScript performance optimization recently and I thought I&#8217;d share some of my findings regarding jQuery.</p>
<p>The project I&#8217;m working involves creating large DOM trees of elements from data, and I&#8217;ve been trying to speed things up. Initially I was using <tt>jQuery.append(...)</tt> combined with <tt>document.createElement(...)</tt> in something along these lines:</p>
<pre name="code" class="javascript">
var foo = $(document.createElement("span")).attr("bar", "baz");
$(parent).append(foo);
</pre>
<p>The reason for this style is because I want to be able to keep around references to the created object so I can use it later and not be forced to use selectors.</p>
<p>While doing some profiling, I became concerned that the append call was taking too much time, so I did some testing.  Some <a href="http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx#tip6">articles</a> I&#8217;ve read indicated that it was better to do string concatenation to create a massive strings of the elements that you wanted to create and the use a single <tt>jQuery.append(...)</tt> call to create the elements at the end.  I tested that scenario as well.</p>
<p>My test code:</p>
<pre name="code" class="javascript">
(function() {
	var start = new Date();
	var root = document.createElement("span");
	for(var i = 0; i < 100000; i++) {
		root.appendChild(document.createElement("span"));
	}
	var end = new Date();
	alert("Raw appendChild: " + (end.valueOf() - start.valueOf()));
})();

(function() {
	var start = new Date();
	var root = $(document.createElement("span"));
	for(var i = 0; i < 100000; i++) {
		root.append(document.createElement("span"));
	}
	var end = new Date();
	alert("jQuery append: " + (end.valueOf() - start.valueOf()));
})();

(function() {
	var start = new Date();
	var root = $(document.createElement("span"));
	var inners = "";
	for(var i = 0; i < 100000; i++) {
		inners += "<span></span>";
	}
	root.append(inners);
	var end = new Date();
	alert("jQuery from string: " + (end.valueOf() - start.valueOf()));
})();

(function() {
    var start = new Date();
    var root = document.createElement("span");
    var inners = "";
    for(var i = 0; i < 100000; i++) {
        inners += "<span></span>";
    }
    root.innerHTML = inners;
    var end = new Date();
    alert("innerHTML: " + (end.valueOf() - start.valueOf()));
})();
</pre>
<p><a href="javascript:void(0);" id="runit">Run Code</a><br />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script><br />
<script type="text/javascript">
$("#runit").click(function() {(function() {
	var start = new Date();
	var root = document.createElement("span");
	for(var i = 0; i < 100000; i++) {
		root.appendChild(document.createElement("span"));
	}
	var end = new Date();
	alert("Raw appendChild: " + (end.valueOf() - start.valueOf()));
})();(function() {
	var start = new Date();
	var root = $(document.createElement("span"));
	for(var i = 0; i < 100000; i++) {
		root.append(document.createElement("span"));
	}
	var end = new Date();
	alert("jQuery append: " + (end.valueOf() - start.valueOf()));
})();(function() {
	var start = new Date();
	var root = $(document.createElement("span"));
	var inners = "";
	for(var i = 0; i < 100000; i++) {
		inners += "<span></span>";
	}
	root.append(inners);
	var end = new Date();
	alert("jQuery from string: " + (end.valueOf() - start.valueOf()));
})();(function() {  
    var start = new Date();  
    var root = document.createElement("span");
    var inners = "";  
    for(var i = 0; i < 100000; i++) {  
        inners += "<span></span>";  
    }  
    root.innerHTML = inners;
    var end = new Date();  
    alert("innerHTML: " + (end.valueOf() - start.valueOf()));
})();
});
</script></p>
<p>The numbers I get are as follows on my Mac in Firefox are as follows (creating 10k span elements).</p>
<p>  Raw appendChild: 304 ms<br /> jQuery append: 5492 ms<br />Concatenated string (jQuery Append): 6040 ms<br />Concatenated string (raw innerHTML): 208 ms</p>
<p>My results were somewhat inconsistent.  Initially when I was running this test I did see jQuery append with the concatenated string to be faster, but when running tests later, it was a bit slower.  In general, I think the conclusion of the article is correct.  String concatination is better than many calls to <tt>jQuery.append(...)</tt>, but it doesn&#8217;t hold a candle to just doing the raw DOM manipulations.  This makes sense since jQuery still has to parse the string you give it (it doesn&#8217;t feed it raw into the innerHTML) and then call the raw DOM methods directly.  Lesson learned.</p>
]]></content:encoded>
			<wfw:commentRss>http://softwareblog.morlok.net/2010/06/22/the-speed-of-jquery-append/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

