<?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>Jonathan Weiß &#187; Optimization Monday</title>
	<atom:link href="http://mysterycity.de/blog/category/optimization-monday/feed" rel="self" type="application/rss+xml" />
	<link>http://mysterycity.de/blog</link>
	<description>Online Marketing</description>
	<lastBuildDate>Tue, 01 Nov 2011 09:28:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Optimization Monday — Part VIII: use function pointers</title>
		<link>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-viii-use-function-pointers</link>
		<comments>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-viii-use-function-pointers#comments</comments>
		<pubDate>Mon, 04 Feb 2008 06:00:34 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Optimization Monday]]></category>

		<guid isPermaLink="false">http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-viii-use-function-pointers</guid>
		<description><![CDATA[Let us assume that you have a function which will execute different code based on conditions which do not change during runtime (e.g. the rendering engine of the user agent). function doStuff&#40;&#41;&#123; if&#40;userAgent == &#34;internetexplorer&#34;&#41;&#123; // ... &#125;else if&#40;userAgent == &#34;firefox&#34;&#41;&#123; // .. &#125;else if&#40;userAgent == &#34;webkit&#34;&#41;&#123; // .. &#125;else if&#40;userAgent == &#34;opera&#34;&#41;&#123; // .. [...]]]></description>
			<content:encoded><![CDATA[<p>Let us assume that you have a function which will execute different code based on conditions which do not change during runtime (e.g. the rendering engine of the user agent).</p>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> doStuff<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>userAgent == <span style="color: #3366CC;">&quot;internetexplorer&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #009900; font-style: italic;">// ...</span>
  <span style="color: #66cc66;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>userAgent == <span style="color: #3366CC;">&quot;firefox&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #009900; font-style: italic;">// ..</span>
  <span style="color: #66cc66;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>userAgent == <span style="color: #3366CC;">&quot;webkit&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #009900; font-style: italic;">// ..</span>
  <span style="color: #66cc66;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>userAgent == <span style="color: #3366CC;">&quot;opera&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #009900; font-style: italic;">// ..</span>
  <span style="color: #66cc66;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #009900; font-style: italic;">// ..</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>Each time the function is called the string comparisons have to be done, even if the value for <code>userAgent</code> will not change at all.</p>
<p>To optimize this situation assign the actual function code to a variable named <code>doStuff</code>:</p>
<pre class="javascript">&nbsp;
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>userAgent == <span style="color: #3366CC;">&quot;internetexplorer&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  doStuff = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #009900; font-style: italic;">/* .. */</span> <span style="color: #66cc66;">&#125;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>userAgent == <span style="color: #3366CC;">&quot;firefox&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  doStuff = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #009900; font-style: italic;">/* .. */</span> <span style="color: #66cc66;">&#125;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>userAgent == <span style="color: #3366CC;">&quot;webkit&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  doStuff = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #009900; font-style: italic;">/* .. */</span> <span style="color: #66cc66;">&#125;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>userAgent == <span style="color: #3366CC;">&quot;opera&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  doStuff = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #009900; font-style: italic;">/* .. */</span> <span style="color: #66cc66;">&#125;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #66cc66;">&#123;</span>
  doStuff = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #009900; font-style: italic;">/* .. */</span> <span style="color: #66cc66;">&#125;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>After this no more string comparisons are needed. :-)</p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-viii-use-function-pointers/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Optimization Monday — Part VII: set CSS in one step</title>
		<link>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-vii-set-css-at-once</link>
		<comments>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-vii-set-css-at-once#comments</comments>
		<pubDate>Mon, 07 Jan 2008 06:00:30 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Optimization Monday]]></category>

		<guid isPermaLink="false">http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-vii-set-css-at-once</guid>
		<description><![CDATA[Often you need to set just a single CSS attribute in your web application. You will usually do this by applying a value to the specified style attribute. myElement.style.color = 'red'; Sometimes you will have to set a number of attribute values at once. For example if you have just created an element and can [...]]]></description>
			<content:encoded><![CDATA[<p>Often you need to set just a single CSS attribute in your web application. You will usually do this by applying a value to the specified <code>style</code> attribute.</p>
<pre class="javascript">myElement.<span style="color: #006600;">style</span>.<span style="color: #006600;">color</span> = <span style="color: #3366CC;">'red'</span>;</pre>
<p>Sometimes you will have to set a number of attribute values at once. For example if you have just created an element and can not assign a CSS class, because all attributes depend on specified conditions. In this case it is significant faster to set a value for the whole <code>style</code> attribute instead of setting each CSS attribute.</p>
<p>This five assignments</p>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> myDiv = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;
myDiv.<span style="color: #006600;">style</span>.<span style="color: #006600;">backgroundColor</span> = <span style="color: #3366CC;">'blue'</span>;
myDiv.<span style="color: #006600;">style</span>.<span style="color: #006600;">border</span> = <span style="color: #3366CC;">'solid 4px red'</span>;
myDiv.<span style="color: #006600;">style</span>.<span style="color: #006600;">fontFamily</span> = <span style="color: #3366CC;">'Helvetica, Arial'</span>;
myDiv.<span style="color: #006600;">style</span>.<span style="color: #006600;">width</span> = <span style="color: #3366CC;">'400px'</span>;
myDiv.<span style="color: #006600;">style</span>.<span style="color: #006600;">height</span> = <span style="color: #3366CC;">'100px'</span>;
&nbsp;</pre>
<p>can be combined into one</p>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> myDiv = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;
myDiv.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'style'</span>, <span style="color: #3366CC;">'background-color:blue;border:solid 4px red;font-family:Helvetica, Arial;width:400px;height:100px;'</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>If you are dealing with dynamically created elements, be sure to read <a href="http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-iii-append-elements-after-doing-all-modifications">part three</a> of this series. ;-)</p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-vii-set-css-at-once/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Optimization Monday — Part VI: expression order</title>
		<link>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-vi-expression-order</link>
		<comments>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-vi-expression-order#comments</comments>
		<pubDate>Mon, 03 Dec 2007 06:00:05 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Optimization Monday]]></category>

		<guid isPermaLink="false">http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-vi-expression-order</guid>
		<description><![CDATA[JavaScript has the nice ability to stop evaluating expressions in a condition as soon as the condition result is final. function test&#40;&#41;&#123; alert&#40;&#34;this message will never be shown!&#34;&#41;; return true; &#125; &#160; if&#40; true &#124;&#124; test&#40;&#41; &#41;&#123; // .. &#125; &#160; if&#40; false &#38;&#38; test&#40;&#41; &#41;&#123; // .. &#125; Since expressions are evaluated from left [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript has the nice ability to stop evaluating expressions in a condition as soon as the condition result is final.</p>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;this message will never be shown!&quot;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #003366; font-weight: bold;">true</span> || test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #009900; font-style: italic;">// ..</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span> <span style="color: #003366; font-weight: bold;">false</span> &amp;&amp; test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
 <span style="color: #009900; font-style: italic;">// ..</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>Since expressions are evaluated from left to right we have the following possibilities to optimize the expression order:</p>
<ol>
<li>align less complex expressions left</li>
<li>for <em>disjunctions</em>: put the expression which will most likely be <strong>true</strong> to the left</li>
<li>for <em>conjunctions</em>: put the expression which will most likely be <strong>false</strong> to the left</li>
<li>group depending expressions</li>
</ol>
<p>Of course the rules also apply to nested expressions.</p>
<p>This example</p>
<pre class="javascript"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>
  <span style="color: #66cc66;">&#40;</span>
    <span style="color: #66cc66;">&#40;</span>complexCalculation<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> &lt; <span style="color: #CC0000;">42</span><span style="color: #66cc66;">&#41;</span> &amp;&amp;
    <span style="color: #66cc66;">&#40;</span>
      <span style="color: #66cc66;">&#40;</span>userAgentVersion &lt; <span style="color: #CC0000;">7</span><span style="color: #66cc66;">&#41;</span> &amp;&amp;
      activexDisabled<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> &amp;&amp;
      <span style="color: #66cc66;">&#40;</span>getUserAgent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #3366CC;">&quot;Internet Explorer&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span> || <span style="color: #66cc66;">&#40;</span>
    <span style="color: #66cc66;">&#40;</span>value &gt; <span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
 <span style="color: #009900; font-style: italic;">// ..</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>can be optimized to</p>
<pre class="javascript"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>
  <span style="color: #66cc66;">&#40;</span>value &gt; <span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#41;</span> || <span style="color: #66cc66;">&#40;</span>
    <span style="color: #66cc66;">&#40;</span>
      <span style="color: #66cc66;">&#40;</span>getUserAgent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #3366CC;">&quot;Internet Explorer&quot;</span><span style="color: #66cc66;">&#41;</span> &amp;&amp;
      <span style="color: #66cc66;">&#40;</span>userAgentVersion &lt; <span style="color: #CC0000;">7</span><span style="color: #66cc66;">&#41;</span> &amp;&amp;
      activexDisabled<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span> &amp;&amp;
    <span style="color: #66cc66;">&#40;</span>complexCalculation<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> &lt; <span style="color: #CC0000;">42</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
 <span style="color: #009900; font-style: italic;">// ..</span>
<span style="color: #66cc66;">&#125;</span></pre>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-vi-expression-order/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Optimization Monday — Part V: initilize variables and assign values in one step</title>
		<link>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-v-initilize-variables-and-assign-values-in-one-step</link>
		<comments>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-v-initilize-variables-and-assign-values-in-one-step#comments</comments>
		<pubDate>Mon, 05 Nov 2007 06:00:04 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Optimization Monday]]></category>

		<guid isPermaLink="false">http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-v-initilize-variables-and-assign-values-in-one-step</guid>
		<description><![CDATA[You can save execution time by assigning values to variables at the same time there are initilized. var myString; var myArray; &#160; myString = 'lorem '; myString += 'ipsum'; &#160; myArray = new Array&#40;&#41;; myArray.push&#40;'foo'&#41;; myArray.push&#40;'bar'&#41;; You can also save a little amount of time by avoiding concating strings via the += operator. var myString [...]]]></description>
			<content:encoded><![CDATA[<p>You can save execution time by assigning values to variables at the same time there are initilized.</p>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> myString;
<span style="color: #003366; font-weight: bold;">var</span> myArray;
&nbsp;
myString = <span style="color: #3366CC;">'lorem '</span>;
myString += <span style="color: #3366CC;">'ipsum'</span>;
&nbsp;
myArray = <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
myArray.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #66cc66;">&#41;</span>;
myArray.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bar'</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>You can also save a little amount of time by avoiding concating strings via the <code>+=</code> operator.</p>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> myString = <span style="color: #3366CC;">'lorem ipsum'</span>;
<span style="color: #003366; font-weight: bold;">var</span> myArray = <span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">'foo'</span>, <span style="color: #3366CC;">'bar'</span><span style="color: #66cc66;">&#93;</span>;</pre>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-v-initilize-variables-and-assign-values-in-one-step/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Optimization Monday — Part IV: use object literals</title>
		<link>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-iv-use-object-literals</link>
		<comments>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-iv-use-object-literals#comments</comments>
		<pubDate>Mon, 01 Oct 2007 06:00:42 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Optimization Monday]]></category>

		<guid isPermaLink="false">http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-iv-use-object-literals</guid>
		<description><![CDATA[Use object literals to initialize or set your object attributes. Instead of Initech.legalForm = 'corporation'; Initech.workEnvironment = 'cubeFarm'; Initech.coffee = 'lousy'; write Initech = &#123; legalForm : 'corporation', workEnvironment : 'cubeFarm', coffee : 'lousy' &#125;;]]></description>
			<content:encoded><![CDATA[<p>Use object literals to initialize or set your object attributes.</p>
<p>Instead of</p>
<pre class="javascript">Initech.<span style="color: #006600;">legalForm</span> = <span style="color: #3366CC;">'corporation'</span>;
Initech.<span style="color: #006600;">workEnvironment</span> = <span style="color: #3366CC;">'cubeFarm'</span>;
Initech.<span style="color: #006600;">coffee</span> = <span style="color: #3366CC;">'lousy'</span>;</pre>
<p>write</p>
<pre class="javascript">Initech = <span style="color: #66cc66;">&#123;</span>
  legalForm : <span style="color: #3366CC;">'corporation'</span>,
  workEnvironment : <span style="color: #3366CC;">'cubeFarm'</span>,
  coffee : <span style="color: #3366CC;">'lousy'</span>
<span style="color: #66cc66;">&#125;</span>;</pre>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-iv-use-object-literals/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Optimization Monday — Part III: append elements after doing all modifications</title>
		<link>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-iii-append-elements-after-doing-all-modifications</link>
		<comments>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-iii-append-elements-after-doing-all-modifications#comments</comments>
		<pubDate>Mon, 03 Sep 2007 06:00:41 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Optimization Monday]]></category>

		<guid isPermaLink="false">http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-iii-append-elements-after-doing-all-modifications</guid>
		<description><![CDATA[Todays hint is: append newly created HTML element objecs to the document object first when all modifications to them are done. This can lead to great performance boots, because style or behavior changes (during the creation process) do not have to be applied by the browser. So to improve this code snippet var blockElement = [...]]]></description>
			<content:encoded><![CDATA[<p>Todays hint is: <strong>append newly created HTML element objecs to the document object first when all modifications to them are done</strong>. This can lead to great performance boots, because style or behavior changes (during the creation process) do not have to be applied by the browser.</p>
<p>So to improve this code snippet</p>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> blockElement = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> paragraphElement = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'p'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> boldElement = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'b'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
document.<span style="color: #006600;">body</span>.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>blockElement<span style="color: #66cc66;">&#41;</span>;
&nbsp;
blockElement.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>paragraphElement<span style="color: #66cc66;">&#41;</span>;
blockElement.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>boldElement<span style="color: #66cc66;">&#41;</span>;
blockElement.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">innerHTML</span> = <span style="color: #3366CC;">'test paragraph'</span>;
blockElement.<span style="color: #006600;">className</span> = <span style="color: #3366CC;">'myClass'</span>;</pre>
<p>apply the newly created element at the end.</p>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> blockElement = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> paragraphElement = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'p'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> boldElement = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'b'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
blockElement.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>paragraphElement<span style="color: #66cc66;">&#41;</span>;
blockElement.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>boldElement<span style="color: #66cc66;">&#41;</span>;
blockElement.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">innerHTML</span> = <span style="color: #3366CC;">'test paragraph'</span>;
blockElement.<span style="color: #006600;">className</span> = <span style="color: #3366CC;">'myClass'</span>;
&nbsp;
document.<span style="color: #006600;">body</span>.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>blockElement<span style="color: #66cc66;">&#41;</span>;</pre>
<p>If the style settings described in <code>myClass</code> are applied to <code>blockElement</code>'s child nodes, they have to be rendered again &mdash; even if they were just added to the document.</p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-iii-append-elements-after-doing-all-modifications/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Optimization Monday — Part II: function handles</title>
		<link>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-ii-function-handles</link>
		<comments>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-ii-function-handles#comments</comments>
		<pubDate>Wed, 22 Aug 2007 18:11:42 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Optimization Monday]]></category>

		<guid isPermaLink="false">http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-ii-function-handles</guid>
		<description><![CDATA[This entry is a little late, due my holiday (prearrangement), but nevertheless here is the hint for August 2007: use function handles instead of strings for window.setTimeout() and window.setInterval(). By using function handles you avoid an unnecessary evaluation of a string. So instead of var myHandle = window.setTimeout&#40;&#34;doStuff()&#34;, 100&#41;; use var myHandle = window.setTimeout&#40;doStuff, 100&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>This entry is a little late, due my holiday (prearrangement), but nevertheless here is the hint for August 2007: <strong>use function handles instead of strings for <code>window.setTimeout()</code> and <code>window.setInterval()</code>.</strong><br />
By using function handles you avoid an unnecessary evaluation of a string.</p>
<p>So instead of</p>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> myHandle = window.<span style="color: #006600;">setTimeout</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;doStuff()&quot;</span>, <span style="color: #CC0000;">100</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>use</p>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> myHandle = window.<span style="color: #006600;">setTimeout</span><span style="color: #66cc66;">&#40;</span>doStuff, <span style="color: #CC0000;">100</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>Of course this hint is only useful if want to call a parameter less function &mdash; which should be no problem in connection with <code>window.setTimeout()</code> or <code>window.setInterval()</code>. ;-)</p>
<p><b>Update:</b><br />
Actually, there <b>is</b> a way for having a function call with parameters. All we need is a wrapper function and a little bit <i>closures magic</i>:</p>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> wrapperFunc<span style="color: #66cc66;">&#40;</span>firstParam, secondParam<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    doStuff<span style="color: #66cc66;">&#40;</span>firstParam, secondParam<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> myWrapper = wrapperFunc<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'foo'</span>, <span style="color: #3366CC;">'bar'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> myHandle = window.<span style="color: #006600;">setTimeout</span><span style="color: #66cc66;">&#40;</span>myWrapper, <span style="color: #CC0000;">100</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>The real trick is that the <code>myWrapper</code> variable contains an anonymous function together with two variables, which were set when <code>wrapperFunc()</code> was called. By applying this variable to <code>windows.setTimeout()</code> method, the anonymous function (containing the call of <code>doStuff()</code>) will be executed after the given timeout.</p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://mysterycity.de/blog/optimization-monday-%e2%80%94-part-ii-function-handles/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Optimization Monday &#8212; Part I: Prevent lookups</title>
		<link>http://mysterycity.de/blog/optimization-monday-part-i-prevent-lookups</link>
		<comments>http://mysterycity.de/blog/optimization-monday-part-i-prevent-lookups#comments</comments>
		<pubDate>Mon, 02 Jul 2007 06:00:41 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Optimization Monday]]></category>

		<guid isPermaLink="false">http://mysterycity.de/blog/optimization-monday-part-i-prevent-lookups</guid>
		<description><![CDATA[For the most of us, Monday is the beginning of a new working week. Often you feel a bit slow and sleepy (especially if you have a case of Mondays ;-) ). But having a slow developer in front of the screen does not mean that your code has to be slow as well. So, [...]]]></description>
			<content:encoded><![CDATA[<p>For the most of us, Monday is the beginning of a new working week. Often you feel a bit slow and sleepy (especially if you have <a href="http://www.youtube.com/watch?v=e-4FkmGoABA">a case of Mondays</a> ;-) ).<br />
But having a slow developer in front of the screen does not mean that your code has to be slow as well. So, I started the series <em>Optimization Monday</em> which will include one small, but effective hint for optimizing and speeding up your JavaScript code on each month's first Monday.</p>
<p>Today's hint is: <strong>prevent lookups</strong>.<br />
Every word in your object chain has to be found in the interpreter's lookup table. You can shorten the chain by using a local variable which holds a reference to your target.</p>
<p>So, instead of
<pre class="javascript"><span style="color: #000066; font-weight: bold;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #CC0000;">0</span>; i&lt;<span style="color: #CC0000;">100</span>; i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  Initech.<span style="color: #006600;">Web</span>.<span style="color: #006600;">Framework</span>.<span style="color: #006600;">WebsuiteObjects</span>.<span style="color: #006600;">Counter</span>.<span style="color: #006600;">increase</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>write this:
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> incr = Initech.<span style="color: #006600;">Web</span>.<span style="color: #006600;">Framework</span>.<span style="color: #006600;">WebsuiteObjects</span>.<span style="color: #006600;">Counter</span>.<span style="color: #006600;">increase</span>;
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #66cc66;">&#40;</span>i=<span style="color: #CC0000;">0</span>; i&lt;<span style="color: #CC0000;">100</span>; i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  incr<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>If you are using DOM functions, the advantage of using a reference will be even greater.</p>
<p>Slow:
<pre class="javascript">document.<span style="color: #006600;">body</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">8</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeValue</span> = <span style="color: #3366CC;">'foo'</span>;
document.<span style="color: #006600;">body</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">8</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">6</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeValue</span> = <span style="color: #3366CC;">'bar'</span>;
document.<span style="color: #006600;">body</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">8</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">8</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeValue</span> = <span style="color: #3366CC;">'42'</span>;</pre>
<p>Faster:
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> elem = document.<span style="color: #006600;">body</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">8</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">firstChild</span>;
elem.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeValue</span> = <span style="color: #3366CC;">'foo'</span>;
elem.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">6</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeValue</span> = <span style="color: #3366CC;">'bar'</span>;
elem.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">8</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeValue</span> = <span style="color: #3366CC;">'42'</span>;</pre>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://mysterycity.de/blog/optimization-monday-part-i-prevent-lookups/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

