Installations
npm install fast-string-compare
Developer
grantila
Developer Guide
Module System
ESM
Min. Node Version
^14.13.1 || >=16.0.0
Typescript Support
Yes
Node Version
18.13.0
NPM Version
8.19.3
Statistics
2 Stars
10 Commits
2 Watching
1 Branches
1 Contributors
Updated on 02 Feb 2023
Bundle Size
376.00 B
Minified
247.00 B
Minified + Gzipped
Languages
TypeScript (97.93%)
JavaScript (2.07%)
Total Downloads
Cumulative downloads
Total Downloads
1,888,438
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
fast-string-compare
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.
Versions
- Since v2 this is a pure ESM package, and requires Node.js >=12.20. It cannot be used from CommonJS.
- Since v3 requires Node.js >= 14.13.1.
API
1import { compare } from 'fast-string-compare' 2 3[ 'c', 'a', 'B' ].sort( compare ); // 'B', 'a', 'c'
Benchmark
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.
Benchmark results (the exported compare function is the 'fast' method):
❯ 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
- 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/10 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/branches.yml:1
- Warn: no topLevel permission defined: .github/workflows/master.yml:1
- Info: no jobLevel write permissions found
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
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
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/branches.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/fast-string-compare/branches.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/branches.yml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/fast-string-compare/branches.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/master.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/fast-string-compare/master.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/master.yml:25: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/fast-string-compare/master.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/master.yml:40: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/fast-string-compare/master.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/master.yml:42: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/fast-string-compare/master.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/master.yml:51: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/fast-string-compare/master.yml/master?enable=pin
- Info: 0 out of 6 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
15 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-7wpw-2hjm-89gp
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Score
2.5
/10
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 MoreOther packages similar to fast-string-compare
react-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