So in JavaScript, there are multiple ways to sort data. The native JavaScript Array.sort() uses an algorithm from the TimSort family. Modern V8 engines use PowerSort, which is an optimized version of TimSort.
I wanted to understand how different sorting approaches perform, so I compared them.
The interesting part is that there is no single "fastest" sorting algorithm — it depends on the type of data and the use case.
⚡ Float64Array.sort() — Fastest for pure numbers
When the data is only numbers, Float64Array.sort() performs extremely well.
new Float64Array(data).sort()
The reason it is faster is because:
It does not need a comparator function
The engine already knows every value is a number
Numbers are stored in a continuous block of memory
No extra type checks or object handling are needed
This allows the engine to directly work with the raw numeric representation in memory.
However, it only works for numeric data and cannot handle objects or custom sorting rules.
⚡ TimSort / PowerSort — Better for large and complex inputs
TimSort combines:
Insertion sort for small sections
Merge sort for efficiently combining sorted sections
It detects already sorted parts of the array (called runs) and uses that information to optimize sorting.
This makes it very effective for:
Large datasets
Partially sorted data
Real-world applications where data already has some order
For larger and more complex inputs, TimSort can perform better than simpler sorting approaches because it adapts to the structure of the data.
⚡ TimSort npm package
Even though it uses the same general algorithm, the npm implementation is usually slower because it runs completely in JavaScript and does not get the same low-level optimizations as the built-in engine.
The biggest lesson:
The fastest sorting method depends on the situation:
🔹 Pure numbers → Float64Array.sort() is usually the fastest
🔹 Large real-world datasets → TimSort/PowerSort is highly optimized
🔹 Custom objects and sorting rules → Array.sort() with a comparator
Performance is not only about the algorithm. It also depends on:
Memory layout
Data types
Native optimizations
Function call overhead
Understanding both the algorithm and the environment where it runs is what helps us write faster code. 🚀
Top comments (0)