<?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>DaveOnCode &#187; array</title>
	<atom:link href="http://www.daveoncode.com/tag/array/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>Objective C, iOS and more programming stuff</description>
	<lastBuildDate>Mon, 19 Dec 2011 12:11:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Actionscript Vector class initialization with a source Array</title>
		<link>http://www.daveoncode.com/2009/04/06/actionscript-vector-class-initialization-with-a-source-array/</link>
		<comments>http://www.daveoncode.com/2009/04/06/actionscript-vector-class-initialization-with-a-source-array/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 15:35:29 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=367</guid>
		<description><![CDATA[What you maybe already know: Vector class is a powerful class introduced with flash player 10. It is fundamentally a special typed array into which all elements must be the same type and it is faster than a normal Array. What you maybe don&#8217;t know: It&#8217;s possible to initialize a Vector with an Array or [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/04/06/actionscript-vector-class-initialization-with-a-source-array/' addthis:title='Actionscript Vector class initialization with a source Array ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p><strong>What you maybe already know:</strong></p>
<p>Vector class is a powerful class introduced with flash player 10. It is fundamentally a special typed array into which all elements must be the same type and it is faster than a normal Array.</p>
<p><strong>What you maybe don&#8217;t know:</strong></p>
<p>It&#8217;s possible to initialize a Vector with an Array or another Vector as a source but you must use the Vector function not the class constructor. The class constructor in fact has the following signature:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Vector<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">length</span>:uint = <span style="color: #cc66cc;">0</span>, fixed:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span></div></td></tr></tbody></table></div>
<p>Vector function instead has this:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Vector<span style="color: #66cc66;">&#40;</span>sourceArray:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:Vector.<span style="color: #66cc66;">&lt;</span>T<span style="color: #66cc66;">&gt;</span></div></td></tr></tbody></table></div>
<p>So, rather than using such approach:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> vector:Vector.<span style="color: #66cc66;">&lt;</span>String<span style="color: #66cc66;">&gt;</span> = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>String<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
vector<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;value1&quot;</span>;<br />
vector<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;value2&quot;</span>;<br />
vector<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;value3&quot;</span>;<br />
<span style="color: #808080; font-style: italic;">// ...and so on</span></div></td></tr></tbody></table></div>
<p>We can do the following (Notice the absence of &#8220;this&#8221; keyword):</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// create a vector with an inline Array as source</span><br />
<span style="color: #000000; font-weight: bold;">var</span> vector:Vector.<span style="color: #66cc66;">&lt;</span>String<span style="color: #66cc66;">&gt;</span> = Vector.<span style="color: #66cc66;">&lt;</span>String<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;v1&quot;</span>, <span style="color: #ff0000;">&quot;v2&quot;</span>, <span style="color: #ff0000;">&quot;v3&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// create a vector with an inline Vector as source</span><br />
<span style="color: #000000; font-weight: bold;">var</span> vector2:Vector.<span style="color: #66cc66;">&lt;</span>String<span style="color: #66cc66;">&gt;</span> = Vector.<span style="color: #66cc66;">&lt;</span>String<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span>vector<span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// Testing (both trace will print the same string)</span><br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>vector<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>vector2<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/04/06/actionscript-vector-class-initialization-with-a-source-array/' addthis:title='Actionscript Vector class initialization with a source Array ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/04/06/actionscript-vector-class-initialization-with-a-source-array/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Javascript string concatenation performance on modern browsers: array.join(&quot;&quot;) is now slower!</title>
		<link>http://www.daveoncode.com/2008/12/09/javascript-string-concatenation-performance-on-modern-browsers-arrayjoin-is-now-slower/</link>
		<comments>http://www.daveoncode.com/2008/12/09/javascript-string-concatenation-performance-on-modern-browsers-arrayjoin-is-now-slower/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 15:52:32 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[browser performance]]></category>
		<category><![CDATA[dhtml]]></category>
		<category><![CDATA[javascript buffer]]></category>
		<category><![CDATA[javascript performance]]></category>
		<category><![CDATA[javascript string]]></category>
		<category><![CDATA[join]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[string buffer]]></category>
		<category><![CDATA[string concatenation]]></category>

		<guid isPermaLink="false">http://daveoncode.wordpress.com/?p=107</guid>
		<description><![CDATA[Well&#8230; as many Javascript developers know, a common practice to handle big string concatenation, is to implement a sort of  &#8220;stringBuffer&#8221; by using an array as data container and then convert it to a string by using the method join(). Ie: var buffer = []; var target = document.getElementById("box"); for (var i=0; i&#60;10000; i++) { [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2008/12/09/javascript-string-concatenation-performance-on-modern-browsers-arrayjoin-is-now-slower/' addthis:title='Javascript string concatenation performance on modern browsers: array.join(&#34;&#34;) is now slower! ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Well&#8230; as many Javascript developers know, a common practice to handle big string concatenation, is to implement a sort of  &#8220;stringBuffer&#8221; by using an array as data container and then convert it to a string by using the method <strong><em>join()</em></strong>.  Ie:</p>
<pre>
var buffer = [];
var target = document.getElementById("box");

for (var i=0; i&lt;10000; i++) {
    buffer .push(['&lt;p id="node_', i, '" id="node_', i, '"&gt;Content ', i, '&lt;/p&gt;'].join(""));
}

target.innerHTML = buffer.join("");
</pre>
<p>This is theoretically the fastest way to manage huge strings and to speed up the building of dynamic content.</p>
<p><span id="more-107"></span></p>
<p>
It&#8217;s been a long time since I used this technique, but today I was doing some tests and I&#8217;ve realized that is not obsolete, but somehow inadequate to some modern browsers (Firefox 3, Opera 9 and Google Chrome), due to the worst performance compared to the old and classic string concatenation technique with the plus (+) operator. In my test I&#8217;ve compared the time required to concatenate a string built in a 10.000 steps loop with the time required to filling an array, and the amazing result is that, except for internet explorer (for the which buffer technique improve a lot the browser performance) and safari 3, other browser run faster with normal string concatenation with +&#8230; even 70/80% faster!<br />
The results are the following:</p>
<p>- String concatenation with +:</p>
<ul>
<li>17000 milliseconds on Internet Explorer 6</li>
<li>17000 milliseconds on Internet Explorer 7</li>
<li>265 milliseconds on Opera 9</li>
<li>125 milliseconds on Firefox 3</li>
<li>165 milliseconds on Safari 3</li>
<li>50 milliseconds on Google Chrome</li>
</ul>
<p>- Array based technique:</p>
<ul>
<li><span style="color:#008000;">530 milliseconds on Internet Explorer 6 -&gt; -16470 milliseconds (damned good!)</span></li>
<li><span style="color:#008000;">470 milliseconds on Internet Explorer 7 -&gt; -16530 milliseconds (damned good!)</span></li>
<li> <span style="color:#ff0000;">450 milliseconds on Opera 9 -&gt; +185  milliseconds (very bad!)<br />
</span></li>
<li><span style="color:#ff0000;"> 200 milliseconds on Firefox 3 -&gt; +75 milliseconds (bad!)</span></li>
<li><span style="color:#ff0000;"><span style="color:#008000;">145 milliseconds on Safari 3 -&gt; -20 milliseconds (good)</span><br />
</span></li>
<li><span style="color:#ff0000;">95 milliseconds on Google Chrome -&gt; +45 milliseconds (bad)</span></li>
</ul>
<p>In conclusion:  if we want to get the best performances from Firefox, Opera and Chrome we have to avoid the array based technique (and use &#8220;+&#8221; operator instead),  however this technique is still valid if we don&#8217;t want to import a browser specific javascript file or branch the code from one browser to another, because it enhance meaningfully the code execution speed of Internet Explorer and decrease in an acceptable manner the execution on other browsers.</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2008/12/09/javascript-string-concatenation-performance-on-modern-browsers-arrayjoin-is-now-slower/' addthis:title='Javascript string concatenation performance on modern browsers: array.join(&quot;&quot;) is now slower! ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2008/12/09/javascript-string-concatenation-performance-on-modern-browsers-arrayjoin-is-now-slower/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

