1.0.9 • Published 6 years ago

rxjs-min-max v1.0.9

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

rxjs-min-max

RxJs Operators for emits minimum/maximum value on each iteration.

NPM

Example

  import { maxStream, minStream } from 'rxjs-min-max'

  of(1, 1, 2, 2, 2, 0, 1, 2, 3, -1, 4).pipe(
    maxStream(),
  )
  .subscribe(x => console.log(x)); // 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4

  of<Person>(
    { age: 4, name: 'Foo'},
    { age: 7, name: 'Bar'},
    { age: 3, name: 'Foo'},
    { age: 6, name: 'Foo'},
  ).pipe(
    minStream((p: Person, q: Person) => p.age > q.age),
  )
  .subscribe(x => console.log(x));

  // displays:
  // { age: 4, name: 'Foo' }
  // { age: 4, name: 'Foo' }
  // { age: 3, name: 'Foo'},
  // { age: 3, name: 'Foo'},

Performance

You can used this operators like min/max:

  .pipe(
    minStream(), // maxStream
    last()
) // equal min() / max()

If size of stream big(n > 10000), minStream(maxStream) work faster than min(max):

var rxjsMinMax = require('./dist/index.js');
var rxjs = require('rxjs');
var last = require('rxjs/operators').last;
var min = require('rxjs/operators').min;

const n = 100000;
const array = Array.from({ length: n }, () => Math.floor(Math.random() * n));

console.time('minStream');
rxjs.from(array).pipe(rxjsMinMax.minStream(), last()).subscribe((x) => console.log(x));
console.timeEnd('minStream');

console.time('min');
rxjs.from(array).pipe(min()).subscribe((x) => console.log(x));
console.timeEnd('min');

// count n = 100000
// 0
// minStream: 11.276ms
// 0
// min: 40.306ms

// count n = 1000000
// 0
// minStream: 24.084ms
// 0
// min: 72.204ms

// count n = 10000000
// 1
// minStream: 137.856ms
// 1
// min: 437.046ms

// count n = 1000
// 0
// minStream: 5.738ms
// 0
// min: 1.779ms