homepage Welcome to WebmasterWorld Guest from 54.234.2.88
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / HTML
Forum Library, Charter, Moderators: incrediBILL

HTML Forum

    
Sorting a multi-dimensional array in JS - a small browser test
designaweb




msg:3757334
 11:47 am on Oct 2, 2008 (gmt 0)

I was testing performance of one of my CMS's. I have a large array that I sort on a subkey. The array looks like this:

array = (
[0] => array (
country_code = 'DE',
country_name = 'Germany',
city_name = 'Munich'
),
[1] => array (
country_code = 'DE',
country_name = 'Germany',
city_name = 'Berlin'
),
[2] => array (
country_code = 'US',
country_name = 'United States',
city_name = 'New York'
),
);

I used two methods of sorting this dataset by subkey (e.g. "country_name"), one is a custom sort function:

var cfg_sortby_key = 'country_name';

function cSort(a, b) {

var y = a[cfg_sortby_key];
var x = b[cfg_sortby_key];

return ((x < y) ? -1 : ((x > y) ? 1 : 0));

}

The other is an implementation of quicksort in javascript.

Here's the resultset of sorting 3300 rows on my PC (Intel Quadcore Q9540 @ 2.66 GHz, 4GB RAM using Windows Vista 64 bit)

Opera 9.52:
- sorting took 46 ms. using quicksort
- sorting took 32 ms. using custom sorting

FF 3.03:
- sorting took 40 ms. using quicksort
- sorting took 18 ms. using custom sorting

Safari 3.1.2:
- sorting took 84 ms. using quicksort
- sorting took 57 ms. using custom sorting

Google Chrome 0.2.149.30:
- sorting took 54 ms. using quicksort
- sorting took 84 ms. using custom sorting

IE 7.0.6001.18000
- sorting took 196 ms. using quicksort
- sorting took 298 ms. using custom sorting

Couldn't test out IE8 beta, since I cant get it to install on my system (something with a wups.dll error)

I was slightly disappointed by Google Chrome - didn't expect anything better off IE7. Other then that nothing spectacular, allthough it seems quicksort is not always as quick as people say it is...

 

encyclo




msg:3763662
 1:59 pm on Oct 11, 2008 (gmt 0)

An interesting test - I wouldn't be too concerned about Google Chrome, other than the fact that the difference between it and Safari show that there has been some serious modification to the underlying code (they are built on the same initial codebase). Chrome is still in its first version, I assume that Google will be releasing improved versions over time.

What is good news is that the race is on to improve Javascript performance in future browser versions, an issue which is critical to the success of web applications but had previously been neglected by browser makers.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / HTML
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved