Code Monkey home page Code Monkey logo

stream's Introduction

Stream API

Code Style: Google CI Pipeline donate

Type-safe API for processing Iterable data (Arrays, Sets, Maps, Iterables) similarly to Java 8 Stream API, LINQ or Kotlin Sequences.

Classic Javascript solution

const result = [1, 2, 3].filter(x => x % 2 === 0).map(x => x * 2);

Stream API solution

Creating a Stream object

import {Stream, of, from} from '@szilanor/stream';

let stream: Stream<number>;
stream = new Stream([1, 2, 3]); // With constructor;
stream = of(1, 2, 3); // With the 'of' creator function
stream = from([1, 2, 3]); // With the 'from' creator function

Operations on stream entries for the same result

import {filter, map, compound} from '@szilanor/stream';

stream = stream.pipe(
  filter(x => x % 2 === 0),
  map(x => x * 2)
);

Process the stream for the same result

import {toArray} from '@szilanor/stream';

// With a collector
result = stream.collect(toArray());

// Using for..of
let result = [];
for (let entry of stream) {
  result.push(entry);
}

// Using spread operator
result = [...stream];

Why Stream API?

  • Can achieve faster results due to sequential processing
let allEven: boolean;
const input = [1, 2, 3, 4, 5];

// Classic JS maps all the entries first then returns false
allEven = input.map(x => x + 1).every(x => x % 2 === 0);

// vs Stream API maps only the first element then returns false
allEven = from(input)
  .pipe(map(x => x + 1))
  .collect(all(x => x % 2 === 0));
  • More readable code
import {from, distinct, collect} from '@szilanor/stream';

// Filtering duplicates and group them by whether they are even or odd
const input = [1, 1, 1, 1, 2, 3, 4, 4, 5];

// Classic JS
const resultClassic: Map<string, number[]> = new Map<string, number[]>();
Array.from(new Set<number>(input)).forEach(x => {
  const key = x % 2 === 0 ? 'even' : 'odd';
  resultClassic.set(key, [...(resultClassic.get(key) || []), x]);
});

// Stream API
const resultStreamApi: Map<string, number[]> = from(input)
  .pipe(distinct())
  .collect(groupBy(x => (x % 2 === 0 ? 'even' : 'odd')));
  • You can create your own operators and collectors if you don't find what you need
import {CollectorFunction, OperationFunction} from '@szilanor/stream';

const myAwesomeCollector: CollectorFunction<unknown, unknown> = {
  /* your own implementation */
};
const myAwesomeOperation: OperationFunction<unknown, unknown> = {
  /* your own implementation */
};

const result = of(1, 2, 3)
  .pipe(myAwesomeOperation())
  .collect(myAwesomeCollector());

stream's People

Contributors

jharrilim avatar szilanor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

stream's Issues

Benchmark not correct(?)

When using the provided benchmark, I noticed that I could not reproduce the stated result. In my tests, this library always was the slowest.

The results of my benchmark (I added a For Loop as comparison):

Classic JS x 11,375,579 ops/sec ±10.54% (48 runs sampled)
Stream API x 33,607 ops/sec ±25.26% (40 runs sampled)
For loop x 34,567,827 ops/sec ±91.45% (48 runs sampled)
Fastest is For loop

Online REPL

Did I maybe do something wrong in my setup? If not, I think it'd be good to either remove the benchmark or update them.

Use "fairer" comparison to "Classical JS"

The examples used for "Classical JS" seems to use code that is very slow and contains extensive typing and unneeded code.

As an example, here's a benchmark using the second to last code example (concerning readability):

Map x 636 ops/sec ±124.98% (5 runs sampled)
Stream x 30,502 ops/sec ±15.18% (37 runs sampled)
Object x 441,113 ops/sec ±19.31% (34 runs sampled)
Reduce x 1,285,775 ops/sec ±5.84% (43 runs sampled)
Object with For Loop x 443,450 ops/sec ±13.82% (43 runs sampled)
Fastest is Reduce

As it can be seen, the map from the example is very very slow. Using an object instead of map greatly improves performance and outperforms this library (as also indicated in #2 )

REPL with the benchmark

In terms of readability I'd like to suggest a more "cleaned up" comparison with less extensive typing and removing unneeded code so users can have a fair comparison between this library and "Classic JS". See following TS Playground as an example with different approaches to the problem from the example.

Example TS Playground

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.