var a=new Array(4e9);
for (var i=1; i<a.length; i=Math.ceil(i*1.0001))
a[i]=new Number(i).toString();
a.sort();
The speed suggest that this sparse string array is not sorted with a quadratic algorithm (today).EDIT: In order to go down the fallback path the array needs to be both sufficiently large and sufficiently sparse (< 12.5% occupied). You can see the difference in algorithms by modifying your test case like so:
Dense, fast path:
var a = [];
for (var i = 1; i < 4e6; i++)
a[i * 5] = i.toString();
a.sort();
Sparse, fallback path: var a = [];
for (var i = 1; i < 4e6; i++)
a[i * 10] = i.toString();
a.sort();See this animated example that danso posted: http://www.sorting-algorithms.com/
Yes, a difference of maybe 5ms won't be noticed... unless you're going a lot of sorting. Selection Sort is basically the slowest sort of all.
That piece of code you think must be slow and must be impacting the application speed? It might not be consequential at all. Spending 4 hours optimizing it might get you exactly 0% improvement.
And that one line you think is benign and would take 5 minutes to opt might end up being the key to speeding up your entire algorithm tenfold.
It's important to understand speed and complexity when architecting software, but it's just as important to know how it really performs in the real world, on the CPU, with real memory constraints and real data.
I'm not saying this is true at all for this particular algorithm, but it's been uncannily true for me in the past and I still fool myself to this day trying to optimise prematurely based on my primitive human computer.