Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bjarneo/cendex
The most performant package to find the index of an element in an array or string
https://github.com/bjarneo/cendex
Last synced: 3 days ago
JSON representation
The most performant package to find the index of an element in an array or string
- Host: GitHub
- URL: https://github.com/bjarneo/cendex
- Owner: bjarneo
- License: mit
- Created: 2017-04-05T18:26:53.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-04-06T05:42:55.000Z (over 7 years ago)
- Last Synced: 2024-10-12T14:56:45.337Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 47.9 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
cendex [center index]
--
![Travis](https://travis-ci.org/bjarneo/cendex.svg?branch=master)The most performant package to find the index of an element in an array or string.
How it works
--
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%.
![How cendex work](https://github.com/bjarneo/cendex/blob/master/chart.png?raw=true)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.
Usage
--```bash
$ npm i --save cendex
``````js
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
```Benchmark
--
Result by using node v7.7.2
![How cendex work](https://github.com/bjarneo/cendex/blob/master/benchmark.png?raw=true)Tests
--
```bash
$ npm test
```Contribution
--
Contributions are appreciated.License
--
MIT-licensed. See LICENSE.