The most performant package to find the index of an element in an array or string.
It starts from the center of the array, increments and decrements from the center index until it finds the element. Which means it will be slow for the 25% first elements in an array, but faster than any other implementation for the last 75%.
The median time used however if we have an array with 1000 entries, and look for first 100, 200, 300, 400, 500, 600, 700, 800, 900 then 999, will be faster than all of the other indexOf implementations. Even the native one. Please take a look at the benchmark test.
$ npm i --save cendex
const cendex = require('cendex');
const words = [
'cendex',
'javascript',
'php',
'python',
'node',
'nlp'
];
cendex(words, 'cendex'); // Output: 0
cendex(words, 'python'); // Output: 3
cendex(words, 'not here'); // Output: -1
$ npm test
Contributions are appreciated.
MIT-licensed. See LICENSE.