fastrange
A fast range
function for JavaScript
Create an array of numbers within a given range.
This is an useful alternative for the for
loop when using a functional programming style
where using map
, filter
and similar functions are preferred.
Fastrange implements the range
function known from Python.
Fastrange is faster and more lightweight than alternative JavaScript implementations.
Installation
$ npm install fastrange
Usage
Using import, as a ESM module, for use with a bundler like webpack or rollup:
import range from 'fastrange';
As a commonjs module, for use in Node.js:
const range = require('fastrange');
Then:
range(10);
range(5, 10);
range(4, 10, 2);
API
range( [start=0], stop, [step=1] )
Returns an array of numbers dependent of the given parameters.
start
Type: number
Optional, start of the range, by default 0
stop
Type: number
End of the range, this value is not included in the result.
step
Type: number
Optional, step between the numbers in the range, by default 1
Performance
Many different implementations have been tested for perfomance and
the fastest across browsers, using array push in a for loop,
is being used in fastrange.
Tests
npm test
Related
License
MIT © 2019 Edwin Martin