Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

17 Commits

Repository files navigation

basedArray 1

A correct and based array implementation. 1-based indexing. obviously.

Inspired by Why you're wrong about 0-based indexing by @tjdevries

Install

npm install --save based-array
yarn add based-array
pnpm add based-array

Use

import{BasedArray}from"based-array";constfunkyReduce=(arr: number[])=>{returnarr.reduce((accum,item,index)=>{returnaccum+item*index;},0);};letarray=newBasedArray<number>(5).fill(1);console.log(array[1]);// 1console.log(funkyReduce(array));// 15array=BasedArray.from([1,2,3,4,5]);console.log(array[1]);// 1console.log(funkyReduce(array));// 55

Strict mode

import{BasedArray}from"based-array";letarray=BasedArray.from([1,2,3,4,5]);console.log(array[1]);// 1console.log(array[0]);// undefined// console.warn: you are counting from zeroBasedArray.strict();array=BasedArray.from([1,2,3,4,5]);console.log(array[1]);// 1console.log(array[0]);// Throws error: CountingFromZeroError

Testing

Every based array method is tested against its vanilla counterpart in a thrilling batte over in tests/vanilla-vs.spec.

The example above is tested in tests/readme-example.spec.

Performance

You can run a simple performance test.

➜ based-array git:(main) ✗ npm run perf
Starting performance test with 10,000 items.
Vanilla: 0.809ms
Based: 0.657ms
Vanilla: 0.524ms
Based: 0.548ms
Finished performance test.

No but honestly you shouldn't trust these perf test results, it makes no sense for it to be anything but slower than vanilla Array, maybe will get better tests to demonstrate when I'm bored again.

About

A correct and based array implementation. 1-based indexing. obviously. 😎🧊🧊🧊

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages