
<?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>Vjeux &#187; sort</title>
	<atom:link href="http://blog.vjeux.com/tag/sort/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.vjeux.com</link>
	<description>French Web Developer</description>
	<lastBuildDate>Wed, 21 Jul 2010 01:30:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Javascript &#8211; Sorting Table</title>
		<link>http://blog.vjeux.com/2010/javascript/javascript-sorting-table.html</link>
		<comments>http://blog.vjeux.com/2010/javascript/javascript-sorting-table.html#comments</comments>
		<pubDate>Fri, 08 Jan 2010 12:16:58 +0000</pubDate>
		<dc:creator>vjeux</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[sort]]></category>

		<guid isPermaLink="false">http://blog.vjeux.com/?p=737</guid>
		<description><![CDATA[For my new project on World of Raids I have to implement a table sorting. The browser not stable sorting and the faster sorting trick add difficulty to the task.
String Comparison
As mentionned in the Speed Up Javascript Sort() article, using a string as a key to represent each element is faster than using a custom [...]]]></description>
			<content:encoded><![CDATA[<p>For my new project on World of Raids I have to implement a table sorting. The browser not stable sorting and the faster sorting trick add difficulty to the task.</p>
<h3>String Comparison</h3>
<p>As mentionned in the <a href="/2009/javascript/speed-up-javascript-sort.html">Speed Up Javascript Sort()</a> article, using a string as a key to represent each element is faster than using a custom sort function in most browsers.</p>
<p>Tables are commonly made of two data types: Numbers and Strings. Strings are the final representation so no work is involved there so our work will be focused on Numbers. What we want is to fit a number into a string. A string is a succession of characters, each one able to hold 256 values. So, we can see the problem as encoding the number into a base 256 which is trivial.</p>
<h4>Padding</h4>
<p>String comparison does not work exactly like we want it. It reads all the characters one by one and if they mismatch then it returns who is the highest. So for example <code>"10" < "5"</code> because </code><code>'1' < '5'</code>. We have to pad every numbers with </code><code>0</code> at the beginning and become <code>"10" > "05"</code>.</p>
<p>In this case we are adding one <code>0</code> because 10 is 2 digits and 5 is only one. We won't know the values of other elements when making a comparison, so two solutions apply: either we iterate over all the elements and retrieve the highest, or we set an arbitrary maximum.</p>
<table>
<thead>
<tr>
<th style="text-align: center;">Characters</th>
<th style="text-align: center;">Maximum</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center;">1</td>
<td style="text-align: right;">256</td>
</tr>
<tr>
<td style="text-align: center;">2</td>
<td style="text-align: right;">65 536</td>
</tr>
<tr>
<td style="text-align: center;">3</td>
<td style="text-align: right;">23 330 816</td>
</tr>
<tr>
<td style="text-align: center;">4</td>
<td style="text-align: right;">5 972 688 896</td>
</tr>
</tbody>
</table>
<p>With that table in mind, it is obvious that it would be a waste of time to inspect every element just to find the highest value. 1 or 2 characters will probably fit most uses but if you want to be safe, you will always be able to store your number into 4 characters which is acceptable.</p>
<p>Here is the code to do the conversion:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> compareAsc<span style="color: #009900;">&#40;</span>num<span style="color: #339933;">,</span> digits<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// Encode num in base 256</span>
  <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>num <span style="color: #339933;">!==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    s <span style="color: #339933;">=</span> String.<span style="color: #660066;">fromCharCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>num <span style="color: #339933;">%</span> <span style="color: #CC0000;">256</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> s<span style="color: #339933;">;</span>
    num <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>num <span style="color: #339933;">/</span> <span style="color: #CC0000;">256</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    digits <span style="color: #339933;">-=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #006600; font-style: italic;">// Fill with 0</span>
  <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>digits <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    s <span style="color: #339933;">=</span> String.<span style="color: #660066;">fromCharCode</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> s<span style="color: #339933;">;</span>
    digits <span style="color: #339933;">-=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> s<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For the sake of simplicity, this code is using string concatenation which requires to build up new strings and make copy several times. Since this is for small strings (up to 4 characters!) this is probably alright but one looking for performances could bench this with the use of an <code>Array</code> with a <code>.concat()</code> at the end to build the string.</p>
<p>Also note that numbers are stored as float in Javascript, so we are rounding (flooring to be exact) all the values using the <code>|0</code> trick.</p>
<h4>Descending Order</h4>
<p>We have made the code for an ascending sort but we have to handle the other case. We cannot just revert the order of the sorting or adding a minus sign before the number. What we have to do is to do a 256-complement of the final result, in other terms: digit = 255 - digit. Let see an example for a base 10.</p>
<pre>00 -> 99
01 -> 98
...
54 -> 45
55 -> 44
56 -> 43
...
98 -> 01
99 -> 00
</pre>
<p>You can see that all the numbers are now sorted in the opposite order.</p>
<p>Here is the associated code :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> compareDesc<span style="color: #009900;">&#40;</span>num<span style="color: #339933;">,</span> digits<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// Encode num in base 256 and do the 256-complement</span>
  <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>num <span style="color: #339933;">!==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    s <span style="color: #339933;">=</span> String.<span style="color: #660066;">fromCharCode</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">255</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>num <span style="color: #339933;">%</span> <span style="color: #CC0000;">256</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> s<span style="color: #339933;">;</span>
    num <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>num <span style="color: #339933;">/</span> <span style="color: #CC0000;">256</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    digits <span style="color: #339933;">-=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>digits <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    s <span style="color: #339933;">=</span> String.<span style="color: #660066;">fromCharCode</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">255</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> s<span style="color: #339933;">;</span>
    digits <span style="color: #339933;">-=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> s<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4>Floats and Negatives</h4>
<p>We have handled all the positive integers but they are not alone. In order to deal with the negative numbers you can apply the <a href="http://en.wikipedia.org/wiki/Two%27s_complement">2-complement</a> to the number. This is how it is done in computer arithmetic. </p>
<p>To handle floats, it is possible to treat them as integers by multiplying by 10^(number of decimal displayed). So <code>12.34</code> is translated to <code>1234</code> and <code>11</code> to <code>1100</code> and it's working just fine. You will probably need more than 4 characters if you are using huge numbers though.</p>
<h3>Stable Sorting</h3>
<p>Implementing a table sorting requires the sorting algorithm to be stable (values that have the same key keep their original order). This property allows people to sort by multiple columns, the last one having the highest weight.</p>
<p>However, browser implementations of the <code>Array.sort()</code> are not stable. From there we have two solutions, implementing a stable sort in javascript or find a way to emulate that behaviour. Since Javascript is slow in many browsers, implementing such a computation heavy algorithm in Javascript is going to slow down things and requires more efforts than I am willing to spend on this project.</p>
<p>The solution instead is to understand what the stable property means and find a way to code it.</p>
<blockquote><p>Stable sorting algorithms maintain the relative order of records with equal keys. [...] Whenever there are two records (let's say R and S) with the same key, and R appears before S in the original list, then R will always appear before S in the sorted list.</p>
<div style="text-align: right; margin-top: 5px;"><a href="http://en.wikipedia.org/wiki/Sorting_algorithm">Sorting Algorithm - Wikipedia</a></div>
</blockquote>
<p>We can easily transpose that sentence into the sorting function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>R.<span style="color: #660066;">key</span> <span style="color: #339933;">==</span> S.<span style="color: #660066;">key</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000066; font-weight: bold;">return</span> compare<span style="color: #009900;">&#40;</span>R.<span style="color: #660066;">position</span><span style="color: #339933;">,</span> S.<span style="color: #660066;">position</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>We need to know one more thing to do the computation: the position of each element in the list before sorting. This is straightforward to do, you have to iterate over all the elements and update their position field. This requires <i>n</i> steps which is acceptable.</p>
<p>Here is the full Javascript code</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> sort <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a.<span style="color: #660066;">key</span> <span style="color: #339933;">===</span> b.<span style="color: #660066;">key</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">return</span> a.<span style="color: #660066;">position</span> <span style="color: #339933;">-</span> b.<span style="color: #660066;">position</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a.<span style="color: #660066;">key</span> <span style="color: #339933;">&lt;</span> b.<span style="color: #660066;">key</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>String comparison</h4>
<p>To apply this concept to the string comparison, you can append the position at the end of the string. <code>"Key"</code> now being <code>"Key1"</code>, <code>"Key2"</code> and so on. When the keys are equal, the position is used for the comparison instead. We can reuse the <code>compareAsc()</code> function to encode the position using the minimal size.</p>
<p>This works well for fixed-size strings like the representation of numbers. However common strings do not share that property and a problem arise when two strings share a common base. </p>
<p>Take "Method" and "Methodology". It is obvious that <code>"Method" &lt; "Methodology"</code>. Now append the position: <code>"Method" + chr(250) &gt; "Method<strong>ology</strong>" + chr(251)</code> the comparison changed because <code>chr(250) > 'o'</code>. (Where <code>chr = String.fromCharCode'</code>).</p>
<p>The way to fix this problem is to add a <code>chr(0)</code> before the position. Considering that a normal string never contains a <code>chr(0)</code>, the <code>chr(0)</code> will always be lesser than any character of the string and therefore stop the comparison. The longer string always wins!</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.vjeux.com/2010/javascript/javascript-sorting-table.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Speed Up Javascript Sort()</title>
		<link>http://blog.vjeux.com/2009/javascript/speed-up-javascript-sort.html</link>
		<comments>http://blog.vjeux.com/2009/javascript/speed-up-javascript-sort.html#comments</comments>
		<pubDate>Mon, 10 Aug 2009 17:39:39 +0000</pubDate>
		<dc:creator>vjeux</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[sort]]></category>

		<guid isPermaLink="false">http://blog.vjeux.com/?p=46</guid>
		<description><![CDATA[By overriding the toString Object prototype, it is possible to speed up by 5x the sort function. This is an easy to implement trick that gives astonishing results
I wanted to know if there were ways to speed up the Javascript Sort function. I came across an interesting article (Yet another faster Javascript Sorting) that presents [...]]]></description>
			<content:encoded><![CDATA[<p><i>By overriding the toString Object prototype, it is possible to speed up by 5x the sort function. This is an easy to implement trick that gives astonishing results</i></p>
<p>I wanted to know if there were ways to speed up the Javascript Sort function. I came across an interesting article (<a href="http://blog.piterpen.net/2007/01/08/yet-another-faster-javascript-sorting/">Yet another faster Javascript Sorting</a>) that presents a way to boost the builtin sort function. However, the link with the detailed explanation is dead, so i make you a summary here.</p>
<p>To sort some data, you are likely to do something that looks like that:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">data.<span style="color: #660066;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> b.<span style="color: #660066;">key</span> <span style="color: #339933;">-</span> a.<span style="color: #660066;">key</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The comparison function is being called <i>n log n</i> times. Since it's a javascript function, it is slow. sort() with no parameters will first convert all elements into strings and then use native (therefore faster) string comparison.</p>
<p>To make this work, we just have to override the toString method of the Object prototype to return the key.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> save <span style="color: #339933;">=</span> Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span><span style="color: #339933;">;</span>
Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">key</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
data.<span style="color: #660066;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span> <span style="color: #339933;">=</span> save<span style="color: #339933;">;</span></pre></div></div>

<p>You have to make sure that the key variable is a string. In my application, the key range is [0, 100] so the it is written as String.fromCharCode(key). If you have to deal with larger key range, the best solution is to convert the number into base 256. Make sure the number is padded with 0 because of the string comparison.</p>
<p>I made a little <a href="http://blog.vjeux.com/wp-content/uploads/2009/08/javascript.sort.html">benchmark</a> of the implementation to see how well it performs</p>
<style>
.sort_results { border-collapse: collapse; margin-left: 2px; margin-bottom: 0; }
.sort_results td { text-align: center; width: 100px; border: 1px solid #ccc; padding: 0 5px; }
.sort_results td.right { text-align: right; width: 200px; }
</style>
<table class="sort_results">
<thead>
<tr>
<td><a href="http://blog.vjeux.com/wp-content/uploads/2009/08/javascript.sort.html">toString Sort Benchmark</a></td>
<td><b>Firefox<br />3.5.2</b></td>
<td><b>IE<br />8</b></td>
<td><b>Safari<br />4.528</b></td>
<td><b>Chrome<br />3.0.197</b></td>
</tr>
</thead>
<tbody>
<tr>
<td class="right">Normal - 10 000</td>
<td>135ms</td>
<td>188ms</td>
<td>45ms</td>
<td>16ms</td>
</tr>
<tr>
<td class="right">Fast - 10 000</td>
<td>10ms</td>
<td>31ms</td>
<td>14ms</td>
<td>68ms</td>
</tr>
<tr>
<td class="right">Improvement - 10 000</td>
<td><strong>x13.5</strong></td>
<td><strong>x6.1</strong></td>
<td><strong>x3.2</strong></td>
<td><strong>/4.3</strong></td>
</tr>
<tr>
<td class="right">Normal - 100 000</td>
<td>695ms</td>
<td>2125ms</td>
<td>200ms</td>
<td>128ms</td>
</tr>
<tr>
<td class="right">Fast - 100 000</td>
<td>101ms</td>
<td>437ms</td>
<td>46ms</td>
<td>326ms</td>
</tr>
<tr>
<td class="right">Improvement - 100 000</td>
<td><strong>x6.9</strong></td>
<td><strong>x4.9</strong></td>
<td><strong>x4.3</strong></td>
<td><strong>/2.5</strong></td>
</tr>
<tr>
<td class="right">Normal - 1 000 000</td>
<td>10102ms</td>
<td>*</td>
<td>2736ms</td>
<td>970ms</td>
</tr>
<tr>
<td class="right">Fast - 1 000 000</td>
<td>1158ms</td>
<td>6828ms</td>
<td>482ms</td>
<td>2593ms</td>
</tr>
<tr>
<td class="right">Improvement - 1 000 000</td>
<td><strong>x8.7</strong></td>
<td><strong></strong></td>
<td><strong>x5.7</strong></td>
<td><strong>/2.7</strong></td>
</tr>
</tbody>
</table>
<p><small style="float: right; margin-top: -10px;">*: Script time limit has been exceeded</small></p>
<p>It gives about a 5x increase of all the browsers I have tested with except in Chrome with a 3x decrease.</p>
<p>Since Chrome is already times faster than all the browsers, it doesn't look slowed by this feature. However it gives a real boost to all other browsers.</p>
<p><strong>Update (24 December 2009)</strong>: <a href="http://www.google.com/codesearch/p?hl=fr#W9JxUuHYyMg/trunk/src/array.js&#038;q=ArraySort%20package:http://v8%5C.googlecode%5C.com&#038;sa=N&#038;cd=1&#038;ct=rc&#038;l=616">Chrome Array.sort()</a> function is written directly in javascript and calls the ToString function everytime when a comparison is needed. Therefore, it is making 2 function calls (ToString(x), ToString(y) instead of one (compare(x, y)).</p>
<p>In order to check if that optimization will indeed give an actual boost, we can count the number of time the ToString method is being executed for 3 values. 3 times means that it is executed <i>n</i> time and more means that it is executed <i>n log n</i> times.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> need_custom_sort <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// Fill the array with 3 values</span>
  <span style="color: #003366; font-weight: bold;">var</span> array <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Override the toString method that counts how many times it is being called</span>
  <span style="color: #003366; font-weight: bold;">var</span> count <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> save <span style="color: #339933;">=</span> Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span><span style="color: #339933;">;</span>
  Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> count <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Sort</span>
  array.<span style="color: #660066;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span> <span style="color: #339933;">=</span> save<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// 3 times is good, more is bad!</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>count <span style="color: #339933;">===</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.vjeux.com/2009/javascript/speed-up-javascript-sort.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
