Let's say I have the following JS on my page (compare
is a simple comparator function not relevant to the question):
function sortArray(a) {
a.sort(compare);
}
function sortJQuerySet(b) {
b.sort(compare);
}
$(document).ready(function(){
var a = [], b = [], i = 0, n = 1000;
for(i=0; i<n; ++i) {
a.push($('<div>' + i.toString() + '</div>'));
b.push($('<div>' + i.toString() + '</div>'));
}
b = $(b);
$('#runner').click(function(){
sortArray(a);
sortJQuerySet(b);
});
});
As you can see, a
and b
are essentially the same array, the only difference is that b
is turned into jQuery set
. I'm trying to sort both of those arrays and to profile the sorting. Please note that the number of elements in both arrays is 1000.
Here is the result of profiling of sorting for both containers in Safari:
Safari makes about half a million comparisons on jQuery set with 1000 elements. This looks much more like quadratic sort than like O(n log n) sort. In a meantime, sorting the native array is just fine.
Sorting in Chrome browser works in about equal time for both container types.
P.S. I used Safari 6.0.4, jQuery 1.7.1 and jQuery 1.10.1. Code: https://gist.github.com/ikostia/5925715