EDIT: It looks like what I've written below is a form of Pigeonhole Sort.
I've been looking into how JavaScript Arrays are really special Objects and additionally have in-order iterators (since they are collections).
Because of that, this code is possible:
let inputArray = []; // inputArray.length === n
// non-duplicate entries for the sake of this question (easy to alter)
let sparse = new Array(); // Sets up as dictionary
// O(n)
inputArray.forEach(function(entry) { // Uses Dictionary mode
sparse[entry] = entry; // O(1)
});
// O(n) (actual is n log n)
let sortedArray = [];
for (let entry of sparse) { // Uses Array mode
sortedArray.push(entry); // O(1)
}
But since sorting algorithms have a fastest runtime of O(n log(n)), the conversion between JavaScript's object dictionary mode and array mode must slow it down. I tested it, and it does slow down as expected (although it is faster than the native .sort()), but it only covers sorting by toString() so obviously I'd go with the native version.
So I have the following questions:
- Where specifically in the above code does it devolve into an O(n log(n)) algorithm?
- Can you link me the source code for this? In V8 for example.
I'm assuming it would be a lazy iterator creation or access during the for...of phase, but I would love to see the code and why it is.