# leven

> Measure the difference between two strings using the Levenshtein distance algorithm

Latest version **4.1.0** (published 2025-09-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install leven
pnpm add leven
yarn add leven
bun add leven
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.1.0 |
| Published | 2025-09-11 |
| First published | 2014-08-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | ^12.20.0 \|\| ^14.13.1 \|\| >=16.0.0 |
| Dependencies | 0 |
| Unpacked size | 10 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 734 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | leven, levenshtein, distance, algorithm, string, difference, diff, fast, fuzzy, similar, similarity, compare, comparison, edit, text, match, matching |

## Links

- npm: https://www.npmjs.com/package/leven
- Repository: https://github.com/sindresorhus/leven
- Homepage: https://github.com/sindresorhus/leven#readme
- Issues: https://github.com/sindresorhus/leven/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/leven

## Alternatives

- [ext-list](https://npm.io/package/ext-list.md) — 6.3M weekly downloads
- [@lexical/selection](https://npm.io/package/@lexical/selection.md) — 3.8M weekly downloads
- [@lexical/text](https://npm.io/package/@lexical/text.md) — 3.6M weekly downloads
- [@lexical/clipboard](https://npm.io/package/@lexical/clipboard.md) — 3.0M weekly downloads
- [@tiptap/extension-mention](https://npm.io/package/@tiptap/extension-mention.md) — 3.0M weekly downloads

## Recent versions

- 4.1.0 (latest) — 2025-09-11
- 4.0.0 — 2021-08-10
- 3.1.0 — 2019-04-04
- 3.0.0 — 2019-03-12
- 2.1.0 — 2017-02-18
- 2.0.0 — 2015-09-07
- 1.0.2 — 2015-05-16
- 1.0.1 — 2014-10-07
- 1.0.0 — 2014-08-09

## README

# leven

> Measure the difference between two strings using the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) algorithm

## Install

```sh
npm install leven
```

## Usage

```js
import leven from 'leven';

leven('cat', 'cow');
//=> 2
```

## API

### leven(first, second, options?)

#### first

Type: `string`

First string.

#### second

Type: `string`

Second string.

#### options

Type: `object`

##### maxDistance

Type: `number`

Maximum distance to calculate.

If the actual distance exceeds this value, the function will return `maxDistance` instead of the actual distance. This can significantly improve performance when you only care about matches within a certain threshold.

```js
import leven from 'leven';

leven('abcdef', '123456', {maxDistance: 3});
//=> 3

leven('cat', 'cow', {maxDistance: 5});
//=> 2
```

### closestMatch(target, candidates, options?)

Find the closest matching string from an array of candidates.

#### target

Type: `string`

The string to find matches for.

#### candidates

Type: `string[]`

Array of candidate strings to search through.

#### options

Type: `object`

Same options as `leven()`.

##### maxDistance

Type: `number`

Maximum distance to consider. Candidates with a distance greater than this value will be ignored.

Returns the closest matching string from candidates, or `undefined` if no candidates are provided or if no match is found within `maxDistance`.

```js
import {closestMatch} from 'leven';

closestMatch('kitten', ['sitting', 'kitchen', 'mittens']);
//=> 'kitchen'

closestMatch('hello', ['jello', 'yellow', 'bellow'], {maxDistance: 2});
//=> 'jello'

// No match within distance threshold
closestMatch('abcdef', ['123456', '1234567890'], {maxDistance: 2});
//=> undefined
```

## Related

- [leven-cli](https://github.com/sindresorhus/leven-cli) - CLI for this module

---
_Source: https://npm.io/package/leven · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
