1.7.1 • Published 7 years ago
interseco v1.7.1
Interseco
The fastest way to do set operations (intersection, difference, union) on sorted arrays.
Installation
npm i interseco -S
Usage
var interseco = require('interseco');
var first = [1, 2, 3, 4, 8];
var second = [2, 4, 6, 7, 9];
var inversedFirst = [8, 4, 3, 2, 1];
var inversedSecond = [9, 7, 6, 4, 2];
var customComparator = function (a, b) {
if (a > b) return -1;
if (a < b) return 1;
return 0;
}
// To find the intersection of sorted arrays:
interseco.getIntersection(first, second);
//=> [ 2, 4 ]
// intersection using a custom comparator:
interseco.getIntersection(inversedFirst, inversedSecond, customComparator);
//=> [ 4, 2 ]
// intersection score of sorted arrays:
interseco.getIntersectionScore(first, second);
//=> 2
// intersection score using a custom comparator:
interseco.getIntersectionScore(inversedFirst, inversedSecond, customComparator);
//=> 2
// difference of sorted arrays:
interseco.getDifference(first, second);
//=> [ 1, 3, 8 ]
// difference score of sorted arrays:
interseco.getDifferenceScore(first, second);
//=> 3
// symmetric difference of sorted arrays:
interseco.getDifference(first, second, true);
//=> [ 1, 3, 6, 7, 8, 9 ]
// symmetric difference score of sorted arrays:
interseco.getDifferenceScore(first, second, true);
//=> 6
// difference using a custom comparator:
interseco.getDifference(inversedFirst, inversedSecond, false, customComparator);
//=> [ 8, 3, 1 ]
// union of sorted arrays:
interseco.getUnion(first, second);
//=> [ 1, 2, 2, 3, 4, 4, 6, 7, 8, 9 ]
// union of sorted arrays without duplicates:
interseco.getUnion(first, second, true);
//=> [ 1, 2, 3, 4, 6, 7, 8, 9 ]
// union using a custom comparator:
interseco.getUnion(inversedFirst, inversedSecond, true, customComparator);
//=> [ 9, 8, 7, 6, 4, 3, 2, 1 ]
// index of an item in a sorted array:
interseco.getIndex(first, 4);
//=> 3
// index of an item using custom comparator:
interseco.getIndex(inversedFirst, 2, customComparator)
//=> 3
// rank of an item in a sorted array:
interseco.getRank(second, 5);
//=> 2
// rank of an item using a custom comparator:
interseco.getRank(inversedSecond, 5, customComparator)
//=> 3
// range
interseco.getRange(first, 2, 4);
//=> [ 2, 3, 4 ]
// range using a custom comparator:
interseco.getRange(inversedFirst, 8, 3, customComparator);
//=> [ 8, 4, 3 ]
// check whether an array is sorted:
interseco.isSorted(first);
//=> true
interseco.isSorted(inversedFirst);
//=> false
interseco.isSorted(inversedFirst, customComparator);
//=> true
Benchmark
> node benchmark.js
Get Intersection
interseco x 321,832 ops/sec ±0.86% (91 runs sampled)
sorted-intersect x 265,736 ops/sec ±1.18% (90 runs sampled)
intersect x 15,278 ops/sec ±0.71% (91 runs sampled)
lodash.intersection x 20,374 ops/sec ±1.22% (89 runs sampled)
just-intersect x 24,673 ops/sec ±0.78% (91 runs sampled)
Fastest is interseco
Get Intersection Score
interseco x 347,879 ops/sec ±0.90% (89 runs sampled)
sorted-intersect x 265,852 ops/sec ±1.23% (91 runs sampled)
intersect x 15,444 ops/sec ±0.52% (90 runs sampled)
lodash.intersection x 20,266 ops/sec ±1.33% (87 runs sampled)
just-intersect x 24,850 ops/sec ±0.58% (91 runs sampled)
Fastest is interseco
Get Difference
interseco x 229,060 ops/sec ±1.01% (91 runs sampled)
difference x 17,442 ops/sec ±0.69% (90 runs sampled)
lodash.difference x 33,828 ops/sec ±1.34% (90 runs sampled)
arr-diff x 15,119 ops/sec ±0.69% (90 runs sampled)
array-differ x 15,278 ops/sec ±1.04% (92 runs sampled)
Fastest is interseco
Get Difference Score
interseco x 356,444 ops/sec ±0.85% (89 runs sampled)
difference x 16,951 ops/sec ±2.77% (89 runs sampled)
lodash.difference x 33,819 ops/sec ±1.39% (91 runs sampled)
arr-diff x 15,137 ops/sec ±0.71% (89 runs sampled)
array-differ x 15,345 ops/sec ±1.05% (89 runs sampled)
Fastest is interseco
Get Union
interseco x 156,080 ops/sec ±0.64% (89 runs sampled)
native x 4,903 ops/sec ±1.05% (89 runs sampled)
Fastest is interseco
Find Index
interseco x 6,751,172 ops/sec ±0.75% (90 runs sampled)
native x 2,131,811 ops/sec ±0.96% (88 runs sampled)
Fastest is interseco
License
MIT © Maga D. Zandaqo