Gathering detailed insights and metrics for fast-string-compare
Gathering detailed insights and metrics for fast-string-compare
Gathering detailed insights and metrics for fast-string-compare
Gathering detailed insights and metrics for fast-string-compare
npm install fast-string-compare
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2 Stars
10 Commits
2 Watching
1 Branches
1 Contributors
Updated on 02 Feb 2023
Minified
Minified + Gzipped
TypeScript (97.93%)
JavaScript (2.07%)
Cumulative downloads
Total Downloads
Last day
-8.6%
4,984
Compared to previous day
Last week
-3.7%
25,050
Compared to previous week
Last month
16.1%
103,442
Compared to previous month
Last year
73.2%
962,253
Compared to previous year
This is a (much) faster version of String.prototype.localeCompare() to compare two strings, useful for Array.prototype.sort() or for ordering strings in trees.
This is not equivalent to localeCompare
, and does not return the same result. localeCompare
respects the current locale (language), and orders e.g. both 'a' and 'A' before 'b'. See Intl.Collator.
The function exported by this package - compare
- returns a pure binary comparison, and is therefore just as stable and useful if some deterministic order is needed, but not necessarily a human friendly order.
1import { compare } from 'fast-string-compare' 2 3[ 'c', 'a', 'B' ].sort( compare ); // 'B', 'a', 'c'
Running this under a system heavily slowing down user code (e.g. Jest, with or without coverage), it will run much slower than localeCompare()
. This is misleading, and not the case when not running in a test environment.
Look at the benchmark code and run it using yarn benchmark
.
In these benchmarks, the fast compare is ~2-3x faster than localeCompare
, but a large part of the benchmark is logic around the actual comparison, so the difference is likely larger for the pure comparison.
❯ yarn -s benchmark
Benchmarking raw comparison algorithms...
Running test of: raw compare
fast x 12,190,097 ops/sec ±0.50% (91 runs sampled)
fast double x 11,263,299 ops/sec ±0.44% (91 runs sampled)
fast and slice x 7,343,412 ops/sec ±0.62% (91 runs sampled)
fast codepoint x 11,552,827 ops/sec ±0.67% (87 runs sampled)
Intl.Collator x 6,341,841 ops/sec ±1.12% (86 runs sampled)
localeCompare x 5,599,850 ops/sec ±1.01% (90 runs sampled)
Benchmarking comparison algorithms for sort...
Running test of: english words
fast x 2.15 ops/sec ±3.61% (10 runs sampled)
fast double x 2.04 ops/sec ±5.87% (10 runs sampled)
fast and slice x 1.36 ops/sec ±2.53% (8 runs sampled)
fast codepoint x 1.74 ops/sec ±2.18% (9 runs sampled)
Intl.Collator x 0.87 ops/sec ±0.82% (7 runs sampled)
localeCompare x 0.78 ops/sec ±4.24% (6 runs sampled)
Running test of: english words reversed
fast x 2.12 ops/sec ±5.38% (10 runs sampled)
fast double x 2.11 ops/sec ±1.76% (10 runs sampled)
fast and slice x 1.39 ops/sec ±3.90% (8 runs sampled)
fast codepoint x 1.79 ops/sec ±1.79% (9 runs sampled)
Intl.Collator x 0.89 ops/sec ±0.28% (7 runs sampled)
localeCompare x 0.79 ops/sec ±3.82% (7 runs sampled)
Running test of: english words randomized
fast x 2.12 ops/sec ±7.95% (10 runs sampled)
fast double x 2.13 ops/sec ±1.59% (10 runs sampled)
fast and slice x 1.42 ops/sec ±0.55% (8 runs sampled)
fast codepoint x 1.81 ops/sec ±0.53% (9 runs sampled)
Intl.Collator x 0.88 ops/sec ±3.13% (7 runs sampled)
localeCompare x 0.80 ops/sec ±2.20% (7 runs sampled)
Running test of: data type words
fast x 10,857 ops/sec ±0.74% (92 runs sampled)
fast double x 9,770 ops/sec ±0.76% (93 runs sampled)
fast and slice x 6,135 ops/sec ±0.44% (94 runs sampled)
fast codepoint x 8,212 ops/sec ±0.89% (93 runs sampled)
Intl.Collator x 3,778 ops/sec ±0.47% (94 runs sampled)
localeCompare x 3,434 ops/sec ±0.87% (92 runs sampled)
Running test of: data type words reversed
fast x 10,731 ops/sec ±1.09% (94 runs sampled)
fast double x 9,643 ops/sec ±0.72% (92 runs sampled)
fast and slice x 6,109 ops/sec ±0.46% (92 runs sampled)
fast codepoint x 8,158 ops/sec ±1.34% (93 runs sampled)
Intl.Collator x 3,800 ops/sec ±0.47% (93 runs sampled)
localeCompare x 3,438 ops/sec ±0.41% (92 runs sampled)
Running test of: data type words randomized
fast x 10,776 ops/sec ±1.26% (92 runs sampled)
fast double x 9,789 ops/sec ±0.38% (93 runs sampled)
fast and slice x 6,177 ops/sec ±0.32% (96 runs sampled)
fast codepoint x 8,202 ops/sec ±1.04% (93 runs sampled)
Intl.Collator x 3,786 ops/sec ±0.39% (93 runs sampled)
localeCompare x 3,431 ops/sec ±0.50% (94 runs sampled)
Benchmarking comparison algorithms for trees...
Running test of: english words
fast x 0.40 ops/sec ±2.35% (5 runs sampled)
fast double x 0.36 ops/sec ±1.33% (5 runs sampled)
fast and slice x 0.28 ops/sec ±1.56% (5 runs sampled)
fast codepoint x 0.32 ops/sec ±2.45% (5 runs sampled)
Intl.Collator x 0.24 ops/sec ±4.09% (5 runs sampled)
localeCompare x 0.22 ops/sec ±0.87% (5 runs sampled)
Running test of: english words reversed
fast x 0.38 ops/sec ±1.87% (5 runs sampled)
fast double x 0.36 ops/sec ±2.17% (5 runs sampled)
fast and slice x 0.28 ops/sec ±5.81% (5 runs sampled)
fast codepoint x 0.32 ops/sec ±1.70% (5 runs sampled)
Intl.Collator x 0.24 ops/sec ±0.98% (5 runs sampled)
localeCompare x 0.22 ops/sec ±0.86% (5 runs sampled)
Running test of: english words randomized
fast x 0.38 ops/sec ±1.03% (5 runs sampled)
fast double x 0.36 ops/sec ±1.42% (5 runs sampled)
fast and slice x 0.28 ops/sec ±1.35% (5 runs sampled)
fast codepoint x 0.33 ops/sec ±4.34% (5 runs sampled)
Intl.Collator x 0.24 ops/sec ±0.79% (5 runs sampled)
localeCompare x 0.22 ops/sec ±1.07% (5 runs sampled)
Running test of: data type words
fast x 2,688 ops/sec ±0.69% (91 runs sampled)
fast double x 2,386 ops/sec ±0.77% (95 runs sampled)
fast and slice x 1,653 ops/sec ±0.44% (95 runs sampled)
fast codepoint x 1,898 ops/sec ±0.52% (93 runs sampled)
Intl.Collator x 1,259 ops/sec ±0.38% (93 runs sampled)
localeCompare x 1,143 ops/sec ±0.48% (94 runs sampled)
Running test of: data type words reversed
fast x 2,728 ops/sec ±0.84% (92 runs sampled)
fast double x 2,389 ops/sec ±0.45% (94 runs sampled)
fast and slice x 1,654 ops/sec ±0.38% (94 runs sampled)
fast codepoint x 1,913 ops/sec ±0.34% (95 runs sampled)
Intl.Collator x 1,272 ops/sec ±0.39% (94 runs sampled)
localeCompare x 1,143 ops/sec ±0.54% (93 runs sampled)
Running test of: data type words randomized
fast x 2,723 ops/sec ±0.73% (94 runs sampled)
fast double x 2,399 ops/sec ±0.39% (95 runs sampled)
fast and slice x 1,639 ops/sec ±0.56% (93 runs sampled)
fast codepoint x 1,919 ops/sec ±0.44% (93 runs sampled)
Intl.Collator x 1,272 ops/sec ±0.38% (93 runs sampled)
localeCompare x 1,140 ops/sec ±0.89% (91 runs sampled)
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/10 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
15 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
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 Morereact-fast-compare
Fastest deep equal comparison for React. Great for React.memo & shouldComponentUpdate. Also really fast general-purpose deep comparison.
string-natural-compare
Compare alphanumeric strings the same way a human would, using a natural order algorithm
natural-compare
Compare strings containing a mix of letters and numbers in the way a human being would in sort order.
compare-func
Get a compare function for array to sort