If you find this useful, please consider supporting my work with a donation.
A small utility for fast string comparison by character codes. The package provides functions for comparing strings efficiently without the overhead of locale-specific comparisons. This is useful for cases where raw character code comparison is needed for performance reasons.
npm install @humanwhocodes/fast-string
# or
yarn add @humanwhocodes/fast-string
Import into your Node.js project:
// CommonJS
const { compare, equals } = require("@humanwhocodes/fast-string");
// ESM
import { compare, equals } from "@humanwhocodes/fast-string";
Compares two strings by character codes.
Arguments:
str1
(string): The first string to compare.str2
(string): The second string to compare.
Returns:
number
: Returns 0 if the strings are equal, a negative number ifstr1
comes beforestr2
, and a positive number ifstr1
comes afterstr2
.
Throws:
TypeError
: If either argument is not a string.
Example:
import { compare } from "@humanwhocodes/fast-string";
// Equal strings
console.log(compare("hello", "hello")); // 0
// Different strings
console.log(compare("abc", "def")); // negative number
console.log(compare("def", "abc")); // positive number
// Error case
try {
compare("hello", 123); // throws TypeError
} catch (error) {
console.error(error.message);
}
Checks if two strings are equal by character codes.
Arguments:
str1
(any): The first value to compare.str2
(any): The second value to compare.
Returns:
boolean
: Returnstrue
if both arguments are strings and are equal by character codes, otherwisefalse
.
Example:
import { equals } from "@humanwhocodes/fast-string";
// Equal strings
console.log(equals("hello", "hello")); // true
// Different strings
console.log(equals("abc", "def")); // false
// Different lengths
console.log(equals("abc", "abcd")); // false
// Non-string values
console.log(equals("hello", 123)); // false
console.log(equals(123, "hello")); // false
Apache 2.0