https://github.com/dherault/array-differences
Find the differences between two arrays and their positions
https://github.com/dherault/array-differences
Last synced: 10 months ago
JSON representation
Find the differences between two arrays and their positions
- Host: GitHub
- URL: https://github.com/dherault/array-differences
- Owner: dherault
- License: mit
- Created: 2019-11-06T19:20:19.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-01-06T13:18:17.000Z (over 1 year ago)
- Last Synced: 2025-08-21T07:43:34.174Z (11 months ago)
- Language: TypeScript
- Size: 351 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# array-differences
[](https://badge.fury.io/js/array-differences)
Find the differences between two arrays and their positions.
## Installation
`npm install array-differences --save`
or
`yarn add array-differences`
## Usage
```js
import { diffArray } from 'array-differences'
const input = [0, 1, 1, 4, 1, 1]
const output = [111, 1, 112, 1, 4, 1]
const diff = diffArray(input, output)
--> [['modified', 0, 111], ['inserted', 2, 112], ['deleted', 5]]
```
Using a custom comparison function:
```js
const diff = diffArray(
[{ name: 'John' }, { name: 'Marie' }],
[{ name: 'John' }, { name: 'Marie' }, { name: 'Chloe' }],
(a, b) => a.name === b.name
)
--> [['inserted', 2, { name: 'Chloe' }]]
```
You can recontruct the second array by iterating over the differences on the first array or using the utility:
```js
import { reconstructArray } from 'array-differences'
const reconstructed = reconstructArray(input, diff) // builds the output
// You can reconstruct in place:
reconstructArray(input, diff, true) // modifies input to recreate output from diff
```
## Undefined values
The algorythm does not support arrays containing `undefined` values. Please replace your `undefined` values with a placeholder before applying it.
## Performance on large arrays
Entry one is length m,
Entry two is length n,
Perfomance is m * m * n.
Therefore, it works best on smaller first arrays or if growing an array. Performance is best when differences indexes are close to one another in the array.
## License
MIT