fast-sort
Blazing fast array sorting that outperforms other popular sort libraries even up to 20x.
Take a look at the benchmark section for more information about performance.
Quick example
sort([1,4,2]).asc();
sort(users).desc(u => u.firstName);
sort(users).asc([
u => u.firstName,
u => u.lastName
]);
sort(users).by([
{ asc: u => u.name },
{ desc: u => u.age }
]);
Fast sort highlights
- Sort array of objects by one or more properties
- Sort flat arrays
- Sort in multiple directions
- Easy to read syntax
- Faster than other popular sort alternatives
- Undefined and null values are always sorted to bottom
Under the hood sort use a native JavaScript sort.
Usage of native sort implies that sorting is not necessarily stable and it also implies that input array is modified(sorted) same as it would be when applying native sort.
Example
import sort from 'fast-sort';
sort([1,4,2]).asc();
sort([1,4,2]).desc();
sort(users).asc(u => u.firstName);
sort(users).asc('firstName');
sort(users).desc(u => u.address.city);
sort(users).desc([
'firstName',
'lastName',
u => u.address.city
]);
sort(users).by([
{ asc: 'name' },
{ desc: 'age' }
]);
sort(users).asc(u => u.tags === 'vip' ? 1 : -1);
sort(null).asc();
sort(33).desc();
const arr = [1, 4, 2];
const sortedArr = sort(arr).asc();
console.log(arr);
const arr = [1, 4, 2];
const sortedArr = sort([...arr]).asc();
console.log(arr);
console.log(sortedArr);
NOTE: fast-sort is part of js-flock library exported as single module.
Benchmark
Five different benchmarks have been created to get better insight of how fast-sort perform under different scenarios.
Each benchmark is run with different array sizes raging from small 100 items to large 100 000 items.
Every run of benchmark outputs different results but the results are constantly showing better scores compared to similar popular sorting libraries.
Benchmark scores
Benchmark has been run on:
- 16 GB Ram
- Intel® Core™ i5-4570 CPU @ 3.20GHz × 4
- Ubuntu 16.04
- Node 8.9.1
Running benchmark
To run benchmark on your PC follow steps from below
- git clone https://github.com/snovakovic/fast-sort.git
- cd fast-sort/benchmark
- npm install
- npm start
In case you notice any irregularities in benchmark or you want to add sort library to benchmark score
please open issue here