Welcome to WebmasterWorld Guest from

Forum Moderators: incrediBILL

Message Too Old, No Replies

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

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

Preferred Member

10+ Year Member

joined:Dec 5, 2002
votes: 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
- 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...

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

Senior Member from CA 

WebmasterWorld Senior Member encyclo is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 31, 2003
votes: 2

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.


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members