Blazing fast array sorting with TypeScript support.
Installations
npm install fast-sort
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Node Version
14.17.0
NPM Version
6.14.13
Score
99.6
Supply Chain
100
Quality
77.1
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (69.57%)
JavaScript (30.43%)
Developer
snovakovic
Download Statistics
Total Downloads
10,738,214
Last Day
18,106
Last Week
81,066
Last Month
369,606
Last Year
4,039,408
GitHub Statistics
323 Stars
320 Commits
28 Forks
4 Watching
2 Branches
4 Contributors
Bundle Size
1.84 kB
Minified
848.00 B
Minified + Gzipped
Package Meta Information
Latest Version
3.4.1
Package Id
fast-sort@3.4.1
Unpacked Size
33.82 kB
Size
8.24 kB
File Count
10
NPM Version
6.14.13
Node Version
14.17.0
Publised On
09 Aug 2024
Total Downloads
Cumulative downloads
Total Downloads
10,738,214
Last day
-4.5%
18,106
Compared to previous day
Last week
-16.1%
81,066
Compared to previous week
Last month
18.6%
369,606
Compared to previous month
Last year
49.4%
4,039,408
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
fast-sort
Fast-sort is a lightweight (850 bytes gzip), zero-dependency sorting library with TypeScript support. Its easy-to-use and flexible syntax, combined with incredible speed , make it a top choice for developers seeking efficient, reliable, and customizable sorting solutions.
Quick examples
1 import { sort } from 'fast-sort'; 2 3 // Sort flat arrays 4 const ascSorted = sort([1,4,2]).asc(); // => [1, 2, 4] 5 const descSorted = sort([1, 4, 2]).desc(); // => [4, 2, 1] 6 7 // Sort users (array of objects) by firstName in descending order 8 const sorted = sort(users).desc(u => u.firstName); 9 10 // Sort users in ascending order by firstName and lastName 11 const sorted = sort(users).asc([ 12 u => u.firstName, 13 u => u.lastName 14 ]); 15 16 // Sort users ascending by firstName and descending by city 17 const sorted = sort(users).by([ 18 { asc: u => u.firstName }, 19 { desc: u => u.address.city } 20 ]); 21 22 // Sort based on computed property 23 const sorted = sort(repositories).desc(r => r.openIssues + r.closedIssues); 24 25 // Sort using string for object key 26 // Only available for root object properties 27 const sorted = sort(users).asc('firstName');
Fore more examples check unit tests.
In place sorting
Fast-sort provides an inPlace sorting option that mutates the original array instead of creating a new instance, resulting in marginally faster and more memory-efficient sorting. However, both the inPlaceSort and default sort methods offer exactly the same functionality.
1const { sort, inPlaceSort } = require('fast-sort'); 2 3const array = [3, 1, 5]; 4const sorted = sort(array).asc(); 5 6// sorted => [1, 3, 5] 7// array => [3, 1, 5] 8 9inPlaceSort(array).asc(); 10 11// array => [1, 3, 5]
Natural sorting / Language sensitive sorting
By default fast-sort
is not doing language sensitive sorting of strings.
e.g 'image-11.jpg'
will be sorted before 'image-2.jpg'
(in ascending sorting).
We can provide custom Intl.Collator comparer to fast-sort for language sensitive sorting of strings.
Keep in mind that natural sort is slower then default sorting so recommendation is to use it
only when needed.
1 import { sort, createNewSortInstance } from 'fast-sort'; 2 3 const testArr = ['image-2.jpg', 'image-11.jpg', 'image-3.jpg']; 4 5 // By default fast-sort is not doing natural sort 6 sort(testArr).desc(); // => ['image-3.jpg', 'image-2.jpg', 'image-11.jpg'] 7 8 // We can use `by` sort to override default comparer 9 // with the one that is doing language sensitive comparison 10 sort(testArr).by({ 11 desc: true, 12 comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare, 13 }); // => ['image-11.jpg', 'image-3.jpg', 'image-2.jpg'] 14 15 // Or we can create new sort instance with language sensitive comparer. 16 // Recommended if used in multiple places 17 const naturalSort = createNewSortInstance({ 18 comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare, 19 }); 20 21 naturalSort(testArr).asc(); // => ['image-2.jpg', 'image-3.jpg', 'image-11.jpg'] 22 naturalSort(testArr).desc(); // => ['image-11.jpg', 'image-3.jpg', 'image-2.jpg']
NOTE: It's known that Intl.Collator
might not sort null
values correctly so make sure to cast them to undefine
as described in the following issue
https://github.com/snovakovic/fast-sort/issues/54#issuecomment-1072289388
Custom sorting
Fast sort can be tailored to fit any sorting need or use case by:
- creating custom sorting instances
- overriding default comparer in
by
sorter - custom handling in provided callback function
- combination of any from above
For example we will sort tags
by "custom" tag importance (e.g vip
tag is of greater importance then captain
tag).
1 import { sort, createNewSortInstance } from 'fast-sort'; 2 3 const tags = ['influencer', 'unknown', 'vip', 'captain']; 4 const tagsImportance = { // Domain specific tag importance 5 vip: 3, 6 influencer: 2, 7 captain: 1, 8 }; 9 10 // We can use power of computed prop to sort tags by domain specific importance 11 const descTags = sort(tags).desc(tag => tagImportance[tag] || 0); 12 // => ['vip', 'influencer', 'captain', 'unknown']; 13 14 // Or we can create specialized tagSorter so we can reuse it in multiple places 15 const tagSorter = createNewSortInstance({ 16 comparer: (a, b) => (tagImportance[a] || 0) - (tagImportance[b] || 0), 17 inPlaceSorting: true, // default[false] => Check "In Place Sort" section for more info. 18 }); 19 20 tagSorter(tags).asc(); // => ['unknown', 'captain', 'influencer', 'vip']; 21 tagSorter(tags).desc(); // => ['vip', 'influencer', 'captain', 'unknown']; 22 23 // Default sorter will sort tags by comparing string values not by their domain specific value 24 const defaultSort = sort(tags).asc(); // => ['captain', 'influencer', 'unknown' 'vip']
More examples
1 // Sorting values that are not sortable will return same value back 2 sort(null).asc(); // => null 3 sort(33).desc(); // => 33 4 5 // By default fast-sort sorts null and undefined values to the 6 // bottom no matter if sorting is in asc or decs order. 7 // If this is not intended behaviour you can check "Should create sort instance that sorts nil value to the top in desc order" test on how to override 8 const addresses = [{ city: 'Split' }, { city: undefined }, { city: 'Zagreb'}]; 9 sort(addresses).asc(a => a.city); // => Split, Zagreb, undefined 10 sort(addresses).desc(a => a.city); // => Zagreb, Split, undefined
Migrating from older versions
Documentation for v2 and older versions is available here.
For migrating to v3 you can reference CHANGELOG for what has been changed.
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
Independent benchmark results from MacBook Air can be found in following PR: https://github.com/snovakovic/fast-sort/pull/48
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
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/16 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 18 are checked with a SAST tool
Reason
16 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-7wpw-2hjm-89gp
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94
Score
1.7
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to fast-sort
timsort
TimSort: Fast Sorting for Node.js
array-sort
Fast and powerful array sorting. Sort an array of objects by one or more properties. Any number of nested properties or custom comparison functions may be used.
quickselect
A tiny and fast selection algorithm in JavaScript.
sort-es
Blazing fast, tree-shakeable, type-safe, modern utility library to sort any type of array in less than 1 KB!