4 ms·
> The sort() function was using a bubble sort. (This is of course a bad choice, and I think the maintainers plan to replace it.) There are some valid use cases
by readittwice 9y ago
> The sort() function was using a bubble sort. (This is of course a bad choice, and I think the maintainers plan to replace it.)
There are some valid use cases for Bubble Sort for small and almost sorted arrays (although Array.prototype.sort sounds like a very general solution, which I suppose is not a good idea). I've seen this in JavaScriptCore where they use Cocktail Sort (a variant of Bubble Sort) for sorting in some situations. They also added some performance numbers and explanation in [1] that show that it's faster than e.g. std::stable_sort. I actually tried to write an Insertion Set (which is supposed to be better than Bubble Sort), but couldn't get it to perform better than their Cocktail Sort implementation.
[1] https://github.com/WebKit/webkit/blob/master/Source/WTF/wtf/BubbleSort.h#L31 https://github.com/WebKit/webkit/blob/master/Source/WTF/wtf/...
- varjag 9y agoDoubt whoever wrote the library sort() was optimizing for long tail use cases.