iteragain
Another Javascript library for iterating.
Pure JavaScript, ES6 Iterable/Iterator utilities. No dependencies and shipped with types as is.
Inspired by iterplus, iterare and the Python itertools module. See benchmark section for performance.
The package is designed around the use of the ExtendedIterator
class. It's a class that extends the ES6 Iterator with methods like those in Array
and other iterator methods in python. It's chainable and looks closely like normal Javascript code instead of the more Python itertools
way of doing things:
filter(lambda x: x % 2 == 0, map(lambda x: x * x, iterable))
There is also other utility functions like range
, enumerate
, etc.
Code demo
import { iter } from 'iteragain';
import iter from 'iteragain/iter';
const nums = iter([1, 2, 3])
.map(n => n * n)
.filter(n => n % 2 === 0)
.toArray();
import range from 'iteragain/range';
range(10).toArray();
range(10, 0).toArray();
range(0, -10).toArray();
range(0, 10, 2).toArray();
let r = range(10);
r.length;
r.includes(5);
r.nth(-1);
r.nth(1);
r.nth(10);
let r = range(3);
const nums = [...r, ...r];
Benchmark
Starting benchmark suite: index.bm.ts
for of loop 10000 x 1,198 ops/sec, ±35 ops/sec or ±2.94% (88 runs sampled)
iteragain 10000 x 856 ops/sec, ±14 ops/sec or ±1.67% (89 runs sampled)
iterare 10000 x 747 ops/sec, ±13 ops/sec or ±1.77% (92 runs sampled)
iterplus 10000 x 547 ops/sec, ±8 ops/sec or ±1.43% (92 runs sampled)
Fastest is for of loop 10000