https://github.com/kikobeats/hyperdiff
Find common, removed and added element between two collections.
https://github.com/kikobeats/hyperdiff
comparison difference intersection
Last synced: 2 days ago
JSON representation
Find common, removed and added element between two collections.
- Host: GitHub
- URL: https://github.com/kikobeats/hyperdiff
- Owner: Kikobeats
- License: mit
- Created: 2017-01-03T08:45:07.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2025-11-22T07:28:36.000Z (4 months ago)
- Last Synced: 2025-11-22T09:13:20.017Z (4 months ago)
- Topics: comparison, difference, intersection
- Language: JavaScript
- Homepage: https://kikobeats.github.io/hyperdiff
- Size: 111 KB
- Stars: 14
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# hyperdiff

[](https://coveralls.io/github/Kikobeats/hyperdiff)
[](https://www.npmjs.org/package/hyperdiff)
> Find common, removed and added element between two collections.
## Install
```bash
$ npm install hyperdiff --save
```
## Usage
Using a flat `Array`:
```js
const diff = require('hyperdiff')
const result = diff(
[1, 2, 3, 4, 5, 6],
[1, 2, 4, 5, 6, 0, 9, 10]
)
console.log(result)
// {
// added: [ 0, 9, 10 ],
// removed: [ 3 ],
// common: [ 1, 2, 4, 5, 6 ]
// }
```
Using an `Array` of `Object`'s (in this case you need to provide the unique id):
```js
const diff = require('hyperdiff')
const result = diff(
[
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' },
{ id: 4, name: 'd' },
{ id: 5, name: 'e' }
],
[
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 7, name: 'e' }
],
'id'
)
console.log(result)
// {
// added: [ { id: 7, name: 'e' } ],
// removed: [ { id: 3, name: 'c' }, { id: 4, name: 'd' }, { id: 5, name: 'e' } ],
// common: [ { id: 1, name: 'a' }, { id: 2, name: 'b' } ]
// }
```
It's also support multiple properties as id or provide a `function`.
## Benchmark
```bash
❯ node bench.js
simpleArrayDiff*1000: 143.742ms
hyperDiff*1000: 80.234ms
simpleArrayDiff*1000: 143.405ms
hyperDiff*1000: 75.803ms
```
## API
### hyperdiff(orig, dist, [ids])
#### orig
*Required*
Type: `array`
First array for be compared.
#### dist
*Required*
Type: `array`
Second array for be compared. Notes the results are modeled from the second array.
#### ids
Type: `string`|`array`|`function`
In the case that you provide an `Array` of `Object`'s, you need to specify the `key`'s to be used as `id`.
## Related
* [redis-diff](https://github.com/Kikobeats/redis-diff) - Perform a diff comparison backed by redis.
## License
MIT © [Kiko Beats](https://github.com/Kikobeats).