Gathering detailed insights and metrics for @leondreamed/fast-sort
Gathering detailed insights and metrics for @leondreamed/fast-sort
npm install @leondreamed/fast-sort
Typescript
Module System
Node Version
NPM Version
71.3
Supply Chain
99.3
Quality
75.2
Maintenance
100
Vulnerability
100
License
TypeScript (69.57%)
JavaScript (30.43%)
Total Downloads
4,004
Last Day
2
Last Week
3
Last Month
10
Last Year
479
323 Stars
320 Commits
28 Forks
4 Watching
2 Branches
4 Contributors
Minified
Minified + Gzipped
Latest Version
3.2.0
Package Id
@leondreamed/fast-sort@3.2.0
Unpacked Size
26.64 kB
Size
7.44 kB
File Count
8
NPM Version
8.19.2
Node Version
18.12.1
Cumulative downloads
Total Downloads
Last day
0%
2
Compared to previous day
Last week
0%
3
Compared to previous week
Last month
0%
10
Compared to previous month
Last year
-85.4%
479
Compared to previous year
Fast easy to use and flexible sorting with TypeScript support.
For speed comparison of fast-sort
vs other popular sort libraries check benchmark section.
For list of all available features check highlights section.
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.
Documentation for v2 and older versions is available here.
For migrating to v3 you can reference CHANGELOG for what has been changed.
By default sort
does not mutate provided array it creates new "sorted" instance of array. inPlaceSort
on other hand mutates provided array by sorting it without creating new array instance. Benefits of inPlaceSort
is that it's slightly faster and more generous on memory as it's not creating new array instance every time sorting is done. Other than that there is no difference between using one or another.
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
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
16 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-01-20
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