string-similarity-js
A lightweight, zero-dependency string similarity function using the Sorensen-Dice coefficient.
Repository Health
Technical Analysis
string-similarity-js is a lightweight JavaScript/TypeScript library that measures how similar two strings are by comparing the bigrams (two-character substrings) they share, based on the Sorensen-Dice coefficient. It returns a normalized score between 0 and 1, where 1 means an exact match and 0 means no bigrams in common, making it useful for fuzzy matching, deduplication, and typo-tolerant search.
The implementation is optimized for O(n) performance using a Map to track substring counts instead of the O(n^2) array-based approach used in earlier versions. It ships as a single TypeScript file compiled to a roughly 700 byte minified bundle, with no runtime dependencies beyond the built-in Map object, and exposes configurable substring length and case sensitivity for tuning accuracy on short strings or case-sensitive comparisons.
What You Get
- stringSimilarity() function - a single exported function that compares two strings and returns a normalized similarity score between 0 and 1.
- Configurable substring length - pass a custom n-gram size (default 2) to tune sensitivity for short strings or specific matching needs.
- Case sensitivity toggle - opt into case-sensitive comparisons instead of the default case-insensitive matching.
- Tiny footprint - compiles to roughly 700 bytes minified with zero runtime dependencies, safe to drop into any JS/TS project.
- TypeScript type definitions - ships with a .d.ts file for full type safety in TypeScript projects.
Common Use Cases
- Typo-tolerant search - matching user search queries against catalog or database entries despite misspellings.
- Deduplication - flagging near-duplicate records (names, addresses, titles) during data cleaning.
- Fuzzy autocomplete - ranking suggestion lists by how closely they match partial or misspelled input.
- Data matching / record linkage - comparing fields across two datasets to find likely matches without exact string equality.
Under The Hood
Architecture The library is a single-file module (src/string-similarity.ts) exporting one pure function with no internal state, no I/O, and no dependency graph — a deliberately flat, single-abstraction design where the entire public surface is one call. This keeps the blast radius of any change limited to that one function, at the cost of there being no layering to speak of.
Tech Stack Written in TypeScript and compiled via tsc (tsconfig targets ES5/CommonJS) into a dist/ bundle that includes a generated .d.ts declaration file. Tests run on jasmine-node with chai assertions, coverage is measured with istanbul, and linting is configured through both tslint and eslint. Continuous integration is wired through Travis CI, and Codacy/Codecov badges track quality and coverage.
Code Quality The spec suite (spec/string-similarity.spec.js) covers the meaningful edge cases directly — exact matches, empty strings, substring-length mismatches, case sensitivity, rearranged words, and misspellings — which is thorough for the size of the library. Tooling is comprehensive but dated: tslint is deprecated in favor of typescript-eslint, and the repo has had no commits since early 2024.
API Design The public API is a single well-documented function with JSDoc comments describing every parameter, sensible optional defaults (substring length of 2, case-insensitive by default), and both named and default exports. Getting started requires one import and one call, with no configuration objects or setup — about as low-friction as a matching utility can be.