3.0.0 • Published 7 years ago
diff-sorted-array v3.0.0
diff-sorted-array
Diff two sorted array for best performance.
Install
$ npm i diff-sorted-arrayUsage
const {diff, justDiff, asc, desc} = require('diff-sorted-array')diff(a, b)
- a
Array - b
Array
const a = [2, 3, 1]
const b = [3, 4, 2]
const result = diff(a, b, sorter)
result.unchanged
// [2, 3]
result.added
// [4]
result.deleted
// [1]justDiff(a, b, sorter)
- sorter
Function(a: any, b: any): numberthe compareFunction ofArray.prototype.sort(compareFunction)
Sometimes we want to do the sorting ourself, so that we can manage the process to increase performance.
justDiff only accepts two arrays that both have already been sorted to speed up the matching.
const sorter = (a, b) => a > b
? 1
: - 1
const a = [2, 3, 1]
const b = [3, 4, 2]
a.sort(sorter)
b.sort(sorter)
justDiff(a, b, sorter)
// The same result as aboveasc and desc
Built-in sorter to sort arrays in ascending or descending order.