Gathering detailed insights and metrics for fast-sort
Gathering detailed insights and metrics for fast-sort
Gathering detailed insights and metrics for fast-sort
Gathering detailed insights and metrics for 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.
fast-sort-lens
API wrapper around fast-sort
Blazing fast array sorting with TypeScript support.
npm install fast-sort
Typescript
Module System
Node Version
NPM Version
99.6
Supply Chain
100
Quality
77.1
Maintenance
100
Vulnerability
100
License
TypeScript (69.57%)
JavaScript (30.43%)
Total Downloads
11,960,909
Last Day
18,873
Last Week
99,367
Last Month
397,404
Last Year
4,321,451
MIT License
328 Stars
320 Commits
27 Forks
3 Watchers
2 Branches
4 Contributors
Updated on Apr 26, 2025
Minified
Minified + Gzipped
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
Published on
Aug 09, 2024
Cumulative downloads
Total Downloads
Last Day
73.3%
18,873
Compared to previous day
Last Week
17.4%
99,367
Compared to previous week
Last Month
-1.4%
397,404
Compared to previous month
Last Year
42.5%
4,321,451
Compared to previous year
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.
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.
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]
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
Fast sort can be tailored to fit any sorting need or use case by:
by
sorterFor 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']
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
Documentation for v2 and older versions is available here.
For migrating to v3 you can reference CHANGELOG for what has been changed.
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 has been run on:
Independent benchmark results from MacBook Air can be found in following PR: https://github.com/snovakovic/fast-sort/pull/48
To run benchmark on your PC follow steps from below
In case you notice any irregularities in benchmark or you want to add sort library to benchmark score please open issue here
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/16 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
17 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-04-21
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 More