<?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>Higher-Order &#187; performance</title>
	<atom:link href="http://blog.higher-order.net/category/performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.higher-order.net</link>
	<description>topics: functional programming, concurrency, web-development, REST, dynamic languages</description>
	<lastBuildDate>Mon, 19 Jul 2010 06:45:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>One aspect of lazy computation</title>
		<link>http://blog.higher-order.net/2009/04/23/one-aspect-of-lazy-computation/</link>
		<comments>http://blog.higher-order.net/2009/04/23/one-aspect-of-lazy-computation/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 20:38:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Laziness]]></category>

		<guid isPermaLink="false">http://blog.higher-order.net/?p=298</guid>
		<description><![CDATA[I&#8217;ve often needed to do a combination of filtering and mapping on arrays. E.g. in a Ruby on Rails app, I might have a list of &#8220;RecurringActivation&#8221; model objects which have a product_id and an integer period. Now I would like a list of product_ids where the current time &#8220;matches&#8221; the period; in code this [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve often needed to do a combination of filtering and mapping on arrays. E.g. in a Ruby on Rails app, I might have a list of &#8220;RecurringActivation&#8221; model objects which have a product_id and an integer period. Now I would like a list of product_ids where the current time &#8220;matches&#8221; the period; in code this might be:</p>
<pre><tt><span class="comment">#version 1 - 'functional'</span>
<span class="normal">cur_period </span><span class="symbol">=</span><span class="normal"> Time</span><span class="symbol">.</span><span class="normal">now</span><span class="symbol">.</span><span class="normal">month </span><span class="symbol">-</span><span class="normal"> </span><span class="number">1</span>
<span class="normal">RecurringActivation</span><span class="symbol">.</span><span class="normal">find</span><span class="symbol">(:</span><span class="normal">all</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">:</span><span class="normal">select </span><span class="symbol">=&gt;</span><span class="normal"> </span><span class="string">"product_id, period"</span><span class="symbol">).</span><span class="normal">select </span><span class="cbracket">{</span><span class="normal"> </span><span class="symbol">|</span><span class="normal">r</span><span class="symbol">|</span>
<span class="normal">  cur_period </span><span class="symbol">%</span><span class="normal"> r</span><span class="symbol">.</span><span class="normal">period </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span>
<span class="cbracket">}</span><span class="symbol">.</span><span class="normal">map </span><span class="symbol">&amp;:</span><span class="normal">product_id</span>

<span class="comment">#version 2 - 'imperative'</span>
<span class="normal">cur_period </span><span class="symbol">=</span><span class="normal"> Time</span><span class="symbol">.</span><span class="normal">now</span><span class="symbol">.</span><span class="normal">month </span><span class="symbol">-</span><span class="normal"> </span><span class="number">1</span>
<span class="normal">result </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">[]</span>
<span class="normal">RecurringActivation</span><span class="symbol">.</span><span class="normal">find</span><span class="symbol">(:</span><span class="normal">all</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">:</span><span class="normal">select </span><span class="symbol">=&gt;</span><span class="normal"> </span><span class="string">"product_id, period"</span><span class="symbol">).</span><span class="normal">each </span><span class="cbracket">{</span><span class="normal"> </span><span class="symbol">|</span><span class="normal">r</span><span class="symbol">|</span>
<span class="normal">  result </span><span class="symbol">&lt;&lt;</span><span class="normal"> r</span><span class="symbol">.</span><span class="normal">product_id  </span><span class="keyword">if</span><span class="normal"> cur_period </span><span class="symbol">%</span><span class="normal"> r</span><span class="symbol">.</span><span class="normal">period </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span>
<span class="cbracket">}</span></tt></pre>
<p>In the first version I compose functions (methods) &#8216;select&#8217; and &#8216;map&#8217;: the blocks have no side-effects, and this is why I call it &#8216;functional.&#8217; This is shorter and more clear. Of course, this is implemented by the library using iteration and imperative assignment, but at least my code feels functional.</p>
<p>In the second version I use &#8216;each&#8217; with a block that does both filtering and &#8216;mapping&#8217; in one step: I add the product_id explicitly to the products array for each object which satisfies my criterion. </p>
<p>Notice that the first version first produces a complete filtered list which is then passed on to the map method which produces a complete mapped and filtered list: the list is iterated twice, and an intermediate array containing only the filtered objects exists in memory and is later collected as garbage. In the second version the list is only iterated once, and such an intermediate array of filtered objects is never allocated. Hence one might choose the latter for performance reasons. </p>
<p>Now consider the same in Clojure or any lazy functional language. We might have (no active record here):</p>
<pre><tt>
krukow:~/Projects/private/okooko-prod/tmp$ cl
Clojure
user=> (<span class="keyword">def</span> recurring_activations '({:product_id 1 :period 2}
                                    {:product_id 2 :period 3}
				    {:product_id 3 :period 2}))
#'user/recurring_activations
user=> (<span class="keyword">def</span> res
         (map <span class="symbol">#</span>(<span class="keyword">do</span> (println <span class="string">"map"</span>) (<span class="symbol">:product_id</span> <span class="symbol">%</span>))
             (filter <span class="symbol">#</span>(<span class="keyword">do</span> (println <span class="string">"filter"</span>) (= 0 (mod (:period <span class="symbol">%</span>) <span class="number">2</span>)))
                 recurring_activations)))
#'user/res
user=>
</tt></pre>
<p>Nothing has been mapped or filtered yet since Clojure is fully lazy. So res is now a lazy seq which will compute exactly what is needed on demand. Now think about what will happen if I just peek at the first element of res… In a non-lazy language, already the entire recurring_activations list would have been filtered (printing &#8220;filter&#8221; three times), then that result would have been mapped (printing &#8220;map&#8221; twice) and finally the first element would be looked up. So the output would be<br />
&#8220;filter&#8221;<br />
&#8220;filter&#8221;<br />
&#8220;filter&#8221;<br />
&#8220;map&#8221;<br />
&#8220;map&#8221;<br />
(return first element of list).</p>
<p>What happens in Clojure? It prints &#8220;filter&#8221; &#8220;map&#8221; and gives the first element:</p>
<pre><tt>
user=> (first res)
filter
map
1
user=>
</tt></pre>
<p>In effect we don&#8217;t need an intermediate list containing the filtered elements which is then garbage collected. Notice also that if we just look at the entire list (recomputing res first) we get:</p>
<pre><tt>
user=> res
(filter
map
filter
filter
map
1 3)
user=>
</tt></pre>
<p>So the side effects are evaluated in a different order than had we realized the entire filtered list first. This can be counter intuitive when one is used to eager languages (which all mainstream languages are); however, once understood laziness can be extremely powerful and elegant.</p>
<p>In effect, <em>in lazy languages we can compose functions that work on entire sequences, e.g., map and filter, to obtain a lazy sequence which evaluates the all of the composed functions on each element in sequence.</em> </p>
<p>If that sounded abstract and poorly phrased, I can try to say it more concretely: In Ruby (or Java) I would need to write a new function: filter_and_map taking two &#8220;blocks&#8221;/&#8221;procs&#8221; and a list, then using e.g. &#8220;each&#8221; on the list and apply first the filter &#8220;proc&#8221; then the map &#8220;proc&#8221; &#8212; the existing functions &#8220;map&#8221; and &#8220;select&#8221; can&#8217;t help me. In Clojure (or Haskell) I can simply compose the existing library functions filter and map to obtain the same thing:</p>
<pre><tt>
(def filter_map
	(comp (partial map :product_id)
	          (partial filter #(= 0 (mod (:period %) 2)))))
</pre>
<p></tt><br />
or just use it inline</p>
<pre><tt>
user=> (def res
	(map :product_id
	 (filter #(= 0 (mod (:period %) 2)) recurring_activations)))
#'user/res
user=> res
(1 3)
user=>
</pre>
<p></tt></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.higher-order.net/2009/04/23/one-aspect-of-lazy-computation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manual code specialization:a poor-mans partial evaluation in JavaScript</title>
		<link>http://blog.higher-order.net/2008/07/04/manual-code-specializationa-poor-mans-partial-evaluation-in-javascript/</link>
		<comments>http://blog.higher-order.net/2008/07/04/manual-code-specializationa-poor-mans-partial-evaluation-in-javascript/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 17:46:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[code specialization]]></category>
		<category><![CDATA[partial evaluation]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[sharing]]></category>

		<guid isPermaLink="false">http://blog.higher-order.net/2008/07/04/manual-code-specializationa-poor-mans-partial-evaluation-in-javascript/</guid>
		<description><![CDATA[Recall the object function that Douglas Crockford is promoting in his work on prototypal inheritance in JavaScript: function object(p) { function F(){} F.prototype = p; return new F(); } The object function creates a new object which has the input object (p) as it&#8217;s prototype. On the comp.lang.javascript newsgroup Richard Cornford showed a functionally equivalent [...]]]></description>
			<content:encoded><![CDATA[<p>Recall the object function that Douglas Crockford is promoting in his work on prototypal inheritance in JavaScript:</p>
<pre><tt><strong><span style="color: #0000ff;">function</span></strong> <strong><span style="color: #000000;">object</span></strong><span style="color: #990000;">(</span>p<span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
   <strong><span style="color: #0000ff;">function</span></strong> <strong><span style="color: #000000;">F</span></strong><span style="color: #990000;">()</span><span style="color: #ff0000;">{}</span>
   F<span style="color: #990000;">.</span><strong><span style="color: #0000ff;">prototype</span></strong> <span style="color: #990000;">=</span> p<span style="color: #990000;">;</span>
   <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">F</span></strong><span style="color: #990000;">();</span>
<span style="color: #ff0000;">}</span>
</tt></pre>
<p>The object function creates a new object which has the input object (p) as it&#8217;s prototype.</p>
<p>On the <a href="http://groups.google.com/group/comp.lang.javascript/topics">comp.lang.javascript newsgroup</a> Richard Cornford showed a <a href="http://groups.google.com/group/comp.lang.javascript/msg/e04726a66face2a2">functionally equivalent version</a> which has better performance in general:</p>
<pre><tt><tt><strong><span style="color: #0000ff;">var</span></strong> object <span style="color: #990000;">=</span> <span style="color: #990000;">(</span><strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span><span style="color: #ff0000;">{</span>
    <strong><span style="color: #0000ff;">function</span></strong> <strong><span style="color: #000000;">F</span></strong><span style="color: #990000;">()</span><span style="color: #ff0000;">{}</span>
    <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span>p<span style="color: #990000;">)</span><span style="color: #ff0000;">{</span>
        F<span style="color: #990000;">.</span><strong><span style="color: #0000ff;">prototype</span></strong> <span style="color: #990000;">=</span> p<span style="color: #990000;">;</span>
        <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">F</span></strong><span style="color: #990000;">();</span>
    <span style="color: #ff0000;">}</span><span style="color: #990000;">;</span>
<span style="color: #ff0000;">}</span><span style="color: #990000;">)();</span></tt></tt></pre>
<p>In this version, all invocations of object share the same F which has its prototype mutated with each call. Cornford argues:</p>
<blockquote><p><tt>[The first version of 'object']... is an example of the process that clearly expresses what is being done, but is not particularly efficient as it creates a new - F - function each time it is executed, but all of those - F - functions are essentially identical. If this is to be done often then a more efficient approach would be to only create a single - F - function and put it where it could not be modified by external code.</tt></p></blockquote>
<p>Now, it is important to notice that in general one has to be careful when applying this technique to recursive functions as a variable mutation in one level of recursion may affect others. Also, if we were to put threads into JavaScript, this code would go from being thread-safe in the original form to non-thread safe in the optimized form. However, for now, this technique can be applied in performance-critical functions.</p>
<p>In fact, there is a general technique here that one might call code specialization via higher-order functions (which can be seen as a poor-mans form of partial evaluation). Here is a simple example of that general technique: The &#8216;mk_tag&#8217; function creates the string for an html tag with a class attribute and a text-contents.</p>
<pre><tt><strong><span style="color: #0000ff;">function</span></strong> <strong><span style="color: #000000;">mk_tag</span></strong><span style="color: #990000;">(</span>tag_name<span style="color: #990000;">,</span>clazz<span style="color: #990000;">,</span>contents<span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
   <strong><span style="color: #0000ff;">return</span></strong> <span style="color: #ff0000;">'&lt;'</span><span style="color: #990000;">+</span>tag_name<span style="color: #990000;">+</span><span style="color: #ff0000;">' class="'</span><span style="color: #990000;">+</span>clazz<span style="color: #990000;">+</span><span style="color: #ff0000;">'"&gt;'</span><span style="color: #990000;">+</span>contents<span style="color: #990000;">+</span><span style="color: #ff0000;">'&lt;/'</span><span style="color: #990000;">+</span>tag_name<span style="color: #990000;">+</span><span style="color: #ff0000;">'&gt;'</span><span style="color: #990000;">;</span>
<span style="color: #ff0000;">}</span>
</tt></pre>
<p>Using code specialization via higher-order functions (by currying), we can make specialized functions for writing e.g. &#8216;div&#8217; tags, and specialized (faster) functions for making &#8216;div&#8217; tags with certain classes. The trick is to compute as much as is possible with the given inputs before returning the specialized function:</p>
<pre><tt><em><span style="color: #9a1900;">//a curried version which specializes to it's input</span></em>
<strong><span style="color: #0000ff;">function</span></strong> <strong><span style="color: #000000;">curried_mk_tag</span></strong><span style="color: #990000;">(</span>tag_name<span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
  <strong><span style="color: #0000ff;">var</span></strong> tag_hd <span style="color: #990000;">=</span> <span style="color: #ff0000;">'&lt;'</span><span style="color: #990000;">+</span>tag_name<span style="color: #990000;">+</span><span style="color: #ff0000;">' class="'</span><span style="color: #990000;">,</span>
      tag_tail <span style="color: #990000;">=</span> <span style="color: #ff0000;">'&lt;/'</span><span style="color: #990000;">+</span>tag_name<span style="color: #990000;">+</span><span style="color: #ff0000;">'&gt;'</span><span style="color: #990000;">;</span>
  <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span>clazz<span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
     <strong><span style="color: #0000ff;">var</span></strong> head <span style="color: #990000;">=</span> tag_hd<span style="color: #990000;">+</span>clazz<span style="color: #990000;">+</span><span style="color: #ff0000;">'"&gt;'</span><span style="color: #990000;">;</span>
     <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span>contents<span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
         <strong><span style="color: #0000ff;">return</span></strong> head<span style="color: #990000;">+</span>contents<span style="color: #990000;">+</span>tag_tail<span style="color: #990000;">;</span>
     <span style="color: #ff0000;">}</span><span style="color: #990000;">;</span>
  <span style="color: #ff0000;">}</span><span style="color: #990000;">;</span>
<span style="color: #ff0000;">}</span>

<strong><span style="color: #0000ff;">var</span></strong> mk_div <span style="color: #990000;">=</span> <strong><span style="color: #000000;">curried_mk_tag</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">"div"</span><span style="color: #990000;">);</span>
<strong><span style="color: #0000ff;">var</span></strong> mk_div_green <span style="color: #990000;">=</span> <strong><span style="color: #000000;">mk_div</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">"green"</span><span style="color: #990000;">);</span>
<strong><span style="color: #0000ff;">var</span></strong> mk_div_blue <span style="color: #990000;">=</span> <strong><span style="color: #000000;">mk_div</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">"blue"</span><span style="color: #990000;">);</span>
<strong><span style="color: #000000;">mk_div_green</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">"karl"</span><span style="color: #990000;">)</span><em><span style="color: #9a1900;">//&lt;-- "&lt;div class="green"&gt;karl&lt;/div&gt;"</span></em>
<strong><span style="color: #000000;">mk_div_blue</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">"karl"</span><span style="color: #990000;">)</span><em><span style="color: #9a1900;">//&lt;-- "&lt;div class="blue"&gt;karl&lt;/div&gt;"</span></em>
</tt></pre>
<p>This is elegant as functions can be reused, e.g., &#8216;mk_div_green(&#8220;karl&#8221;);mk_div_green(&#8220;krukow&#8221;)&#8217;. But notice that it is more efficient than simply using a general currier (e.g., <a href="http://www.dustindiaz.com/javascript-curry/">Diaz</a>); essentially it is a form of manual partial evaluation.</p>
<p>I&#8217;ll post some performance measurements in a later posting, but initial results show that we can reduce execution time by roughly 40% by using the sharing form of the object function.</p>
<h2>More Examples</h2>
<p>I&#8217;m not sure how many JavaScript programmers are familiar with this type of optimization. Here are a bunch of real-world examples where it can be applied:</p>
<p><em>Prototype &#8211; Ajax.request function</em></p>
<pre><tt><strong><span style="color: #0000ff;">var</span></strong> Ajax <span style="color: #990000;">=</span> <span style="color: #ff0000;">{</span><em><span style="color: #9a1900;">//original</span></em>
  getTransport<span style="color: #990000;">:</span> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span>
    <strong><span style="color: #0000ff;">return</span></strong> Try<span style="color: #990000;">.</span><strong><span style="color: #000000;">these</span></strong><span style="color: #990000;">(</span>
      <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span><strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">XMLHttpRequest</span></strong><span style="color: #990000;">()</span><span style="color: #ff0000;">}</span><span style="color: #990000;">,</span>
      <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span><strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">ActiveXObject</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">'Msxml2.XMLHTTP'</span><span style="color: #990000;">)</span><span style="color: #ff0000;">}</span><span style="color: #990000;">,</span>
      <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span><strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">ActiveXObject</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">'Microsoft.XMLHTTP'</span><span style="color: #990000;">)</span><span style="color: #ff0000;">}</span>
    <span style="color: #990000;">)</span> <span style="color: #990000;">||</span> <strong><span style="color: #0000ff;">false</span></strong><span style="color: #990000;">;</span>  <span style="color: #ff0000;">}</span><span style="color: #990000;">,</span>

  activeRequestCount<span style="color: #990000;">:</span> <span style="color: #993399;">0</span>
<span style="color: #ff0000;">}</span><span style="color: #990000;">;</span>
</tt></pre>
<p>The thing to notice here is that every time getTransport is called prototype will recompute which XMLHttp transport to use. However, the result of Try.these will always  be the same in a particular run of Prototype, i.e., the showing of a page in one browser. So we might as well precompute which object is the correct one:</p>
<pre><tt><strong><span style="color: #0000ff;">var</span></strong> Ajax <span style="color: #990000;">=</span> <span style="color: #ff0000;">{</span><em><span style="color: #9a1900;">//modified form</span></em>
  getTransport<span style="color: #990000;">:</span> Try<span style="color: #990000;">.</span><strong><span style="color: #000000;">these</span></strong><span style="color: #990000;">(</span>
         <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">XMLHttpRequest</span></strong><span style="color: #990000;">();</span> <em><span style="color: #9a1900;">//test if it exists</span></em>
            <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span><strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">XMLHttpRequest</span></strong><span style="color: #990000;">();</span><span style="color: #ff0000;">}</span>
         <span style="color: #ff0000;">}</span><span style="color: #990000;">,</span>
         <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">ActiveXObject</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">'Msxml2.XMLHTTP'</span><span style="color: #990000;">);</span> <em><span style="color: #9a1900;">//test</span></em>
            <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span><strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">ActiveXObject</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">'Msxml2.XMLHTTP'</span><span style="color: #990000;">);</span> <span style="color: #ff0000;">}</span>
         <span style="color: #ff0000;">}</span><span style="color: #990000;">,</span>
         <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">ActiveXObject</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">'Microsoft.XMLHTTP'</span><span style="color: #990000;">);</span>
            <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span><strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">ActiveXObject</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">'Microsoft.XMLHTTP'</span><span style="color: #990000;">);</span> <span style="color: #ff0000;">}</span>
         <span style="color: #ff0000;">}</span><span style="color: #990000;">),</span>

  activeRequestCount<span style="color: #990000;">:</span> <span style="color: #993399;">0</span>
<span style="color: #ff0000;">}</span><span style="color: #990000;">;</span></tt></pre>
<p><tt><em>jQuery - attr function</em></tt></p>
<pre><tt><tt>attr<span style="color: #990000;">:</span> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span> name<span style="color: #990000;">,</span> value<span style="color: #990000;">,</span> type <span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
 <strong><span style="color: #0000ff;">var</span></strong> options <span style="color: #990000;">=</span> name<span style="color: #990000;">;</span>

 <em><span style="color: #9a1900;">// Look for the case where we're accessing a style value</span></em>
 <strong><span style="color: #0000ff;">if</span></strong> <span style="color: #990000;">(</span> name<span style="color: #990000;">.</span>constructor <span style="color: #990000;">==</span> String <span style="color: #990000;">)</span>
  <strong><span style="color: #0000ff;">if</span></strong> <span style="color: #990000;">(</span> value <span style="color: #990000;">===</span> undefined <span style="color: #990000;">)</span>
   <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">[</span><span style="color: #993399;">0</span><span style="color: #990000;">]</span> <span style="color: #990000;">&amp;</span>amp<span style="color: #990000;">;&amp;</span>amp<span style="color: #990000;">;</span> jQuery<span style="color: #990000;">[</span> type <span style="color: #990000;">||</span> <span style="color: #ff0000;">"attr"</span> <span style="color: #990000;">](</span> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">[</span><span style="color: #993399;">0</span><span style="color: #990000;">],</span> name <span style="color: #990000;">);</span>

  <strong><span style="color: #0000ff;">else</span></strong> <span style="color: #ff0000;">{</span>
   options <span style="color: #990000;">=</span> <span style="color: #ff0000;">{}</span><span style="color: #990000;">;</span>
   options<span style="color: #990000;">[</span> name <span style="color: #990000;">]</span> <span style="color: #990000;">=</span> value<span style="color: #990000;">;</span>
  <span style="color: #ff0000;">}</span>

  <em><span style="color: #9a1900;">// Check to see if we're setting style values</span></em>
 <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">.</span><strong><span style="color: #000000;">each</span></strong><span style="color: #990000;">(</span><strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span>i<span style="color: #990000;">)</span><span style="color: #ff0000;">{</span>
  <em><span style="color: #9a1900;">// Set all the styles</span></em>
  <strong><span style="color: #0000ff;">for</span></strong> <span style="color: #990000;">(</span> name <strong><span style="color: #0000ff;">in</span></strong> options <span style="color: #990000;">)</span>
   jQuery<span style="color: #990000;">.</span><strong><span style="color: #000000;">attr</span></strong><span style="color: #990000;">(</span>    type <span style="color: #990000;">?</span>  <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">.</span>style <span style="color: #990000;">:</span>     <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">,</span>
    name<span style="color: #990000;">,</span> jQuery<span style="color: #990000;">.</span><strong><span style="color: #000000;">prop</span></strong><span style="color: #990000;">(</span> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">,</span> options<span style="color: #990000;">[</span> name <span style="color: #990000;">],</span> type<span style="color: #990000;">,</span> i<span style="color: #990000;">,</span> name <span style="color: #990000;">)</span>   <span style="color: #990000;">);</span>
 <span style="color: #ff0000;">}</span><span style="color: #990000;">);</span>
<span style="color: #ff0000;">}</span></tt></tt></pre>
<p>With jQuery we can&#8217;t write a curried form as that would break compatability. However, we can still perform optimizations like what we had with the &#8216;object&#8217; function. Notice that the function supplied to &#8216;each&#8217; is created with each invocation of &#8216;attr&#8217;, you can also see a for-loop where a check to &#8216;type&#8217; is made with each iteration. In our optimized version, attr chooses which inner function to give to &#8216;each&#8217; by checking type first.</p>
<pre><tt><tt>jQuery<span style="color: #990000;">.</span>fn<span style="color: #990000;">.</span>attr <span style="color: #990000;">=</span> <span style="color: #990000;">(</span><strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span><span style="color: #ff0000;">{</span>
    <strong><span style="color: #0000ff;">var</span></strong> type<span style="color: #990000;">,</span>
        options<span style="color: #990000;">,</span>
        inner_type <span style="color: #990000;">=</span> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span>i<span style="color: #990000;">)</span><span style="color: #ff0000;">{</span>
          <em><span style="color: #9a1900;">// Set all the styles</span></em>
          <strong><span style="color: #0000ff;">var</span></strong> t  <span style="color: #990000;">=</span> type<span style="color: #990000;">,</span>
              s  <span style="color: #990000;">=</span> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">.</span>style<span style="color: #990000;">;</span>
          <strong><span style="color: #0000ff;">for</span></strong> <span style="color: #990000;">(</span><strong><span style="color: #0000ff;">var</span></strong> name <strong><span style="color: #0000ff;">in</span></strong> options <span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
              jQuery<span style="color: #990000;">.</span><strong><span style="color: #000000;">attr</span></strong><span style="color: #990000;">(</span>s<span style="color: #990000;">,</span>
                          name<span style="color: #990000;">,</span>
                           jQuery<span style="color: #990000;">.</span><strong><span style="color: #000000;">prop</span></strong><span style="color: #990000;">(</span> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">,</span> options<span style="color: #990000;">[</span> name <span style="color: #990000;">],</span> t<span style="color: #990000;">,</span> i<span style="color: #990000;">,</span> name <span style="color: #990000;">)</span>
              <span style="color: #990000;">);</span>
          <span style="color: #ff0000;">}</span>
        <span style="color: #ff0000;">}</span><span style="color: #990000;">,</span>
        inner_no_type <span style="color: #990000;">=</span> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span>i<span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
          <strong><span style="color: #0000ff;">for</span></strong> <span style="color: #990000;">(</span><strong><span style="color: #0000ff;">var</span></strong> name <strong><span style="color: #0000ff;">in</span></strong> options <span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
              jQuery<span style="color: #990000;">.</span><strong><span style="color: #000000;">attr</span></strong><span style="color: #990000;">(</span><strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">,</span>
                          name<span style="color: #990000;">,</span>
                           jQuery<span style="color: #990000;">.</span><strong><span style="color: #000000;">prop</span></strong><span style="color: #990000;">(</span> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">,</span> options<span style="color: #990000;">[</span> name <span style="color: #990000;">],</span> <strong><span style="color: #0000ff;">null</span></strong><span style="color: #990000;">,</span> i<span style="color: #990000;">,</span> name <span style="color: #990000;">)</span>
              <span style="color: #990000;">);</span>
          <span style="color: #ff0000;">}</span>
        <span style="color: #ff0000;">}</span><span style="color: #990000;">;</span>

    <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span> name<span style="color: #990000;">,</span> value<span style="color: #990000;">,</span> t <span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
                type <span style="color: #990000;">=</span> t<span style="color: #990000;">;</span>
                options <span style="color: #990000;">=</span> name<span style="color: #990000;">;</span>
  <em><span style="color: #9a1900;">// Look for the case where we're accessing a style value</span></em>
  <strong><span style="color: #0000ff;">if</span></strong> <span style="color: #990000;">(</span> name<span style="color: #990000;">.</span>constructor <span style="color: #990000;">==</span> String <span style="color: #990000;">)</span>
   <strong><span style="color: #0000ff;">if</span></strong> <span style="color: #990000;">(</span> value <span style="color: #990000;">===</span> undefined <span style="color: #990000;">)</span>
    <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">[</span><span style="color: #993399;">0</span><span style="color: #990000;">]</span> <span style="color: #990000;">&amp;</span>amp<span style="color: #990000;">;&amp;</span>amp<span style="color: #990000;">;</span> jQuery<span style="color: #990000;">[</span> type <span style="color: #990000;">||</span> <span style="color: #ff0000;">"attr"</span> <span style="color: #990000;">](</span> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">[</span><span style="color: #993399;">0</span><span style="color: #990000;">],</span> name <span style="color: #990000;">);</span>

   <strong><span style="color: #0000ff;">else</span></strong> <span style="color: #ff0000;">{</span>
    options <span style="color: #990000;">=</span> <span style="color: #ff0000;">{}</span><span style="color: #990000;">;</span>
    options<span style="color: #990000;">[</span> name <span style="color: #990000;">]</span> <span style="color: #990000;">=</span> value<span style="color: #990000;">;</span>
   <span style="color: #ff0000;">}</span>

  <em><span style="color: #9a1900;">// Check to see if we're setting style values</span></em>
  <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">.</span><strong><span style="color: #000000;">each</span></strong><span style="color: #990000;">(</span>t <span style="color: #990000;">?</span> inner_type <span style="color: #990000;">:</span> inner_no_type<span style="color: #990000;">);</span>
 <span style="color: #ff0000;">}</span><span style="color: #990000;">;</span>
<span style="color: #ff0000;">}</span><span style="color: #990000;">)();</span></tt></tt></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.higher-order.net/2008/07/04/manual-code-specializationa-poor-mans-partial-evaluation-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
