closest-to
v2.0.0
Published
A function that, when given a target number and an array of numbers, will return the array value closest to the target.
Downloads
72
Readme
closest-to
closest-to finds the closest value to a given target in a sorted array of numbers
Install
npm install closest-to
Usage
require('closest-to')(needle, haystack, [returnIndex])
require('closest-to/nd')(needle, haystack, [returnIndex])
closest(10, [1, 3, 5, 7, 9])
> 9
closest(10, [1, 3, 5, 7, 9], true)
> 4
ndclosest([1, 2], [[1, 1], [2, 3], [3, 4]])
> [1, 1]
ndclosest([1, 2], [[1, 1], [2, 3], [3, 4]], true)
> 0
Benchmarks
[email protected] x 116,156 ops/sec ±1.02% (85 runs sampled)
[email protected] x 860,497 ops/sec ±1.09% (83 runs sampled)