• Stars
    star
    285
  • Rank 144,698 (Top 3 %)
  • Language
    TypeScript
  • License
    ISC License
  • Created over 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Array methods + ES6 Iterators = ❤️

iterare

lat. to repeat, to iterate

npm downloads build codecov dependencies node code style: prettier semantic-release license chat: on gitter

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

API Documentation

Motivation

Ever wanted to iterate over ES6 collections like Map or Set with Array-built-ins like map(), filter(), reduce()? Lets say you have a large Set of URIs and want to get a Set back that contains file paths from all file:// URIs.

The loop solution is very clumsy and not very functional:

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)
  paths.add(path)
}

Much more readable is converting the Set to an array, using its methods and then converting back:

new Set(
  Array.from(uris)
    .filter(uri => uri.startsWith('file://'))
    .map(uri => uri.substr('file:///'.length))
)

But there is a problem: Instead of iterating once, you iterate 4 times (one time for converting, one time for filtering, one time for mapping, one time for converting back). For a large Set with thousands of elements, this has significant overhead.

Other libraries like RxJS or plain NodeJS streams would support these kinds of "pipelines" without multiple iterations, but they work only asynchronously.

With this library you can use many methods you know and love from Array and lodash while only iterating once - thanks to the ES6 iterator protocol:

import iterate from 'iterare'

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

iterate accepts any kind of Iterator or Iterable (arrays, collections, generators, ...) and returns a new Iterator object that can be passed to any Iterable-accepting function (collection constructors, Array.from(), for of, ...). Only when you call a method like toSet(), reduce() or pass it to a for of loop will each value get pulled through the pipeline, and only once.

This library is essentially

  • RxJS, but fully synchronous
  • lodash, but with first-class support for ES6 collections.

Performance

Benchmarks based on the examples above:

map + filter

Simulate iterating over a very lage Set of strings and applying a filter and a map on it.

Method ops/sec
Loop 466 ops/sec ±1.31% (84 runs sampled)
iterare 397 ops/sec ±2.01% (81 runs sampled)
RxJS 339 ops/sec ±0.77% (83 runs sampled)
Array method chain 257 ops/sec ±1.73% (79 runs sampled)
Lodash 268 ops/sec ±0.84% (81 runs sampled)
IxJS (ES6) 216 ops/sec ±0.81% (81 runs sampled)
IxJS (ES5) 141 ops/sec ±0.87% (77 runs sampled)

filter + take

Simulate iterating over a very lage Set of strings and applying a filter on it, then taking only the first 1000 elements. A smart implementations should only apply the filter predicate to the first 5 elements.

Method ops/sec
Loop 3,059,466 ops/sec ±0.75% (88 runs sampled)
iterare 963,257 ops/sec ±0.68% (89 runs sampled)
IxJS (ES6) 424,488 ops/sec ±0.63% (89 runs sampled)
RxJS 168,853 ops/sec ±2.58% (86 runs sampled)
IxJS (ES5) 107,961 ops/sec ±1.88% (78 runs sampled)
Lodash 41.71 ops/sec ±1.15% (54 runs sampled)
Array method chain 24.74 ops/sec ±3.69% (45 runs sampled)

Lazy Evaluation

Going a step further, if you only care about a specific number of elements in the end, only these elements will run through the pipeline:

iterate(collection)
  .filter(uri => uri.startsWith('file://'))
  .take(5)

In this example, the filter predicate is called only until 5 elements have been found. The alternative with an array would call it for every element in the collection:

Array.from(collection)
  .filter(uri => uri.startsWith('file://'))
  .slice(0, 5)

Contributing

The source is written in TypeScript.

  • npm run build compiles TS
  • npm run watch compiles on file changes
  • npm test runs tests
  • node lib/benchmarks/____ runs a benchmark

More Repositories

1

php-language-server

PHP Implementation of the VS Code Language Server Protocol 🆚↔🖥
PHP
1,126
star
2

svg-screenshots

📸🧩 Browser extension to take scalable, semantic, accessible screenshots of websites in SVG format.
TypeScript
834
star
3

node-sql-template-strings

ES6 tagged template strings for prepared SQL statements 📋
JavaScript
588
star
4

vscode-php-intellisense

Advanced PHP IntelliSense for Visual Studio Code 🆚💬
TypeScript
397
star
5

dom-to-svg

Library to convert a given HTML DOM node into an accessible SVG "screenshot".
TypeScript
378
star
6

cli-highlight

Syntax highlighting for your terminal 💻✨
TypeScript
282
star
7

php-advanced-json-rpc

A more advanced PHP implementation of the JSONRPC Protocol 📞❗
PHP
245
star
8

php-language-server-protocol

Protocol classes for the Language Server Protocol in PHP
PHP
216
star
9

semantic-release-docker

🐳 Set of semantic-release plugins to publish to DockerHub
JavaScript
76
star
10

sequelize-decorators

Sequelize + Decorators = ❤
TypeScript
76
star
11

PSKubectl

kubectl with the power of the object pipeline
C#
61
star
12

merkel

Handles your database migration crisis 🛄
TypeScript
42
star
13

link-preview-sidebar

Browser extension to open links in a sidebar instead of a new tab.
TypeScript
38
star
14

PowerGit

Git with the power of the PowerShell object pipeline. For macOS, Linux and Windows.
PowerShell
30
star
15

vscode-php-pack

Visual Studio Code extension pack for PHP 📦
17
star
16

vscode-css-stacking-contexts

VS Code extension to highlight stacking contexts in CSS and ineffective z-index declarations 💤
TypeScript
17
star
17

abortable-rx

Drop-in replacements for RxJS Observable methods and operators that work with AbortSignal
TypeScript
13
star
18

semantic-release-firefox

🦊📦🚀 semantic-release plugin to automatically release Firefox extensions
HTML
10
star
19

angular-async-filter

Angular2's async pipe for Angular 1. Promise in controller, value in view.
JavaScript
6
star
20

aggregate-map

Read-only ES6 Map implementation that aggregates results from multiple Maps in O(n)
TypeScript
4
star
21

PowerShellXSD

XML schema definitions for PowerShell Format and Types.ps1xml files. NOTE: MOVED TO THE OFFICIAL POWERSHELL REPO
PowerShell
4
star
22

PSDefaults

Manage macOS preferences from PowerShell
PowerShell
4
star
23

PSBuildkite

Module to interact with the Buildkite API from PowerShell
PowerShell
3
star
24

rx-component

Functional, Reactive React Components
TypeScript
3
star
25

PSTravis

Interact with the Travis API from PowerShell
PowerShell
2
star
26

iterare-php

Functional utilities for Iterators
PHP
2
star
27

npm-changing-lockfile-repro

Minimal reproduction case for https://npm.community/t/package-lock-json-keeps-changing-between-platforms-and-runs/1129/4
2
star
28

vscode-postgresql-syntax

Better syntax highlighting for PostgreSQL in VSCode
2
star
29

lint-blame

Blames your lint complaints to enable incremental adoption of new lint rules
TypeScript
2
star
30

whatwg-url-custom-host-repro

Website to show inconsistent behavior of the WHATWG URL API in different browsers when parsing custom protocols
HTML
2
star
31

olfaction

Storage server for code smell analysis data
TypeScript
1
star
32

node-date-only

DONT USE THIS ATM, WILL SEE v2 SOON
JavaScript
1
star
33

rxx

Reactive JSX
TypeScript
1
star
34

stringscore

String scoring algorithm used by VS Code ported to Go
Go
1
star
35

lspindex

CLI tool to build a symbol graph by querying a language server
TypeScript
1
star
36

php-semantic-release-test

PHP
1
star
37

sourcegraph-css-stacking-contexts

Sourcegraph extension to highlight CSS declarations that introduce new stacking contexts.
TypeScript
1
star
38

react-resizable-css-grid

TypeScript
1
star
39

PSCodeCovIo

Helper to use in CI to convert test coverage data emitted by https://github.com/pester/Pester to JSON that can be uploaded to https://codecov.io
PowerShell
1
star