npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

string-best-match

v1.1.1

Published

Finds the longest matching substring (or sequence) between two arrays of strings.

Downloads

18

Readme

string-best-match

npm version build status test status

Finds the longest matching substring (or sequence) between two arrays of strings using a fast Rolling Hash (Rabin-Karp) algorithm with binary search. Useful whenever you need to align or highlight shared runs of tokens.

Installation

npm install string-best-match

Requirements

  • Node.js: 14.0.0+ (ES2020 or later)
  • TypeScript: 4.0+ (if using TypeScript)

This library uses BigInt for precise hash calculations, which requires ES2020 or later. BigInt is supported in:

  • Node.js 10.4.0+
  • Chrome 67+
  • Firefox 68+
  • Safari 14+
  • Edge 79+

Usage

import { findBestMatch } from 'string-best-match';

const source = ['A', 'B', 'C', 'D', 'E'];
const target = ['X', 'B', 'C', 'D', 'Y'];

const result = findBestMatch(source, target);
console.log(result); // { startIndex: 1, sequenceLength: 3 }

Use Cases

  • Text highlighting (e.g., showing best match between user selection and document text)
  • Comparing DNA or code sequences
  • Diff visualization or fuzzy search improvements

API

| Parameter | Type | Description | | --- | --- | --- | | sourceArray | string[] | Array of tokens you want to match against. | | targetArray | string[] | Array to scan for the longest contiguous sequence also present in the source. |

| Returns | Type | Description | | --- | --- | --- | | BestMatchResult | { startIndex: number; sequenceLength: number; } | Start index of the best match inside targetArray and the length of that sequence (-1, 0 when no match is found). |

Performance

Algorithm: Rolling Hash (Rabin-Karp) + Binary Search

This is the fastest approach for large arrays (100k–1M+ elements).

Time Complexity: O((N + M) × log(min(N, M)))

  • N = length of source array
  • M = length of target array
  • Significantly faster than naive O(N × M) for large datasets

Space Complexity: O(N + M)

  • Uses hash maps to store rolling hashes of substrings

How it works:

  1. Binary search is used to find the optimal substring length L
  2. For each candidate length L, compute rolling hashes of all substrings of length L in both arrays
  3. Check if any hashes match between source and target
  4. Verify that matches are genuine (not hash collisions)
  5. Converge to the longest matching substring

This approach is ideal for:

  • Large text processing (100k+ tokens)
  • DNA sequence comparison
  • Code diff algorithms
  • Real-time text analysis

Performance Example:

For arrays with 50,000 elements each:

  • Naive O(N × M): ~2.5 billion operations
  • Rolling Hash O((N+M) log min(N,M)): ~1.6 million operations
  • Speed improvement: ~1,500x faster

Real-world test results (100k total elements):

✓ performs well with very large arrays (108ms)

Examples

See the examples/usage.ts file for comprehensive examples including:

  1. Basic usage with simple arrays
  2. Text token matching (highlighting user selections)
  3. Code diff comparison
  4. DNA sequence alignment
  5. Handling no matches
  6. Performance tests with large arrays (32k elements)
  7. Multiple matches of the same length
  8. Finding longest among partial overlaps

Run the examples:

npx tsx examples/usage.ts

Scripts

  • npm run build — Type-checks and emits ESM output plus type declarations to dist/
  • npm run test — Executes the Vitest suite in tests/

License

MIT © Valentyn