Follow on Twitter for Updates
Stream.js is a lightweight (2.6 KB minified, gzipped), intensely tested (700+ assertions, 97% coverage) functional programming library for operating upon collections of in-memory data. It requires EcmaScript 5+, has built-in support for ES6 features and works in all current browsers, Node.js and Java 8 Nashorn.
npm install streamjs# orbower install streamjs
Read the APIDOC
Before explaining how Stream.js works in detail, here's a few real world code samples.
Filter and sort a collection of persons, then group everything by age.
Filter and transform a collection of tweets to its text content, then limit the tweets to a maximum of 100 and partition the results into 10 tweets per page:
Create an array of 100 odd random numbers between 1 and 1000:
Create an infinite iterator, generating an odd random number between 1 and 1000 on each call to
var iterator = Stream;iteratornext;iteratornext;
Create an array of 100 parent objects, each holding an array of 10 children:
Stream.js defines a single function
Stream to create new streams from different input collections like arrays, maps or number ranges:
Streams are monadic types with a bunch of useful operations. Those functions can be chained one after another to make complex computations upon the input elements. Operations are either intermediate or terminal. Intermediate operations are lazy and return the stream itself to enable method chaining. Terminal operations return a single result (or nothing at all). Some terminal operations return a special monadic
Optional type which is described later.
Streams are not limited to finite data sources like collections. You can also create infinite streams of elements by utilizing generator or iterator functions.
What's so different between Stream.js and other functional libraries like Underscore.js?
Stream.js is built around a lazily evaluated operation pipeline. Instead of consecutively performing each operation on the whole input collection, objects are passed vertically and one by one upon the chain. Interim results will not be stored in internal collections (except for some stateful operations like
sorted). Instead objects are directly piped into the resulting object as specified by the terminal operation. This results in minimized memory consumption and internal state.
Stream operations are lazily evaluated to avoid examining all of the input data when it's not necessary. Streams always perform the minimal amount of operations to gain results. E.g. in a
filter - map - findFirst stream you don't have to filter and map the whole data. Instead
findFirst are executed just one time before returning the single result. This results in increased performance when operation upon large amounts of input elements.
; // called once
A type definition for using Stream.js with Typescript is available here.
Found a bug or missing feature? Please open an issue! Your feedback and help is highly appreciated. Please refer to the contributing rules before sending a pull request. I would also love to hear your feedback via Twitter.
Created and copyright (c) 2014-2015 by Benjamin Winterberg.
Stream.js may be freely distributed under the MIT license.