How to sort an array with 3 million objects in JavaScript

This certainly is a challenge and there are many ways to approach it. The easiest of them all is to find a computer with as much RAM as possible and the fastest CPU. Throwing money at a problem certainly will help you solve many problems. But not all of us have this luxury.

The second method is to make sure you have a decent enough machine, and divide the objects in chunks that can fit in memory. It may take a while to sort them all but it will get done eventually. This works great, if you are not in a hurry.

The third option is the one I prefer the most. It consist of asking the question: Why do you need to sort 3 million objects in JavaScript in the first place? No user can consume this amount of content anyway. This amount of data is probably stored in a tested database solution like MySQL, which provides indexing and other efficient ways of storing and sorting.

If you are trying to sort that much to display it on the DOM you are probably doing it wrong.

Sometimes there are no tricks, just use plain old boring technologies to solve your problems.


There are no comments added yet.

Let's hear your thoughts

For my eyes only