Iterare

iterare

Array methods for ES6 Iterators.

ES6 Iterator library for applying multiple transformations to a collection in a single iteration.

Instead of handling ES6 collections in a messy way like:

1
2
3
4
5
6
7
8
9
10
11
12
13
const uris = new Set([
  'file:///foo.txt',
  'http:///npmjs.com',
  'file:///bar/baz.txt'
]);
const paths = new Set();
for (const uri of uris) {
  if (!uri.startsWith('file://')) {
    continue;
  }
  const path = uri.substr('file:///'.length)l
  paths.add(path);
}

or

1
2
3
4
5
new Set(
  Array.from(uris)
    .filter(uri => uri.startsWith('file://'))
    .map(uri => uri.substr('file:///'.length));
)

Using iterare you can do the same with:

1
2
3
4
5
6
import iterate from 'iterare'

iterate(uris)
  .filter(uri => uri.startsWith('file://'))
  .map(uri => uri.substr('file:///'.length))
  .toSet();

GIF FTW!

iterare

Benchmark based on the example above:

Method ops/sec
Loop 2,562,637 ops/sec ±3.95% (80 runs sampled)
iterare 2,023,212 ops/sec ±1.38% (84 runs sampled)
Array method chain 346,117 ops/sec ±2.68% (82 runs sampled)
Lodash (with lazy evalution) 335,890 ops/sec ±0.55% (85 runs sampled)
RxJS 29,480 ops/sec ±7.01% (51 runs sampled)
Suggest a module

Comments