forked from cujojs/most
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skipRepeats.js
55 lines (49 loc) · 1.76 KB
/
skipRepeats.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require('buba/register')
var Benchmark = require('benchmark');
var most = require('../../src/index');
var rx = require('rx');
var rxjs = require('@reactivex/rxjs');
var kefir = require('kefir');
var bacon = require('baconjs');
var highland = require('highland');
var xs = require('xstream').default;
var runners = require('./runners');
var kefirFromArray = runners.kefirFromArray;
var xstreamDropRepeats = require('xstream/extra/dropRepeats').default;
// Create a stream from an Array of n integers
// filter out odds, map remaining evens by adding 1, then reduce by summing
var n = runners.getIntArg(1000000);
var a = new Array(n);
for(var i = 0, j = 0; i< a.length; i+=2, ++j) {
a[i] = a[i+1] = j;
}
var suite = Benchmark.Suite('skipRepeats -> reduce 2 x ' + n + ' integers');
var options = {
defer: true,
onError: function(e) {
e.currentTarget.failure = e.error;
}
};
suite
.add('most', function(deferred) {
runners.runMost(deferred, most.from(a).skipRepeats().reduce(sum, 0));
}, options)
.add('rx 4', function(deferred) {
runners.runRx(deferred, rx.Observable.fromArray(a).distinctUntilChanged().reduce(sum, 0));
}, options)
.add('rx 5', function(deferred) {
runners.runRx5(deferred, rxjs.Observable.from(a).distinctUntilChanged().reduce(sum, 0));
}, options)
.add('xstream', function(deferred) {
runners.runXstream(deferred, xs.fromArray(a).compose(xstreamDropRepeats()).fold(sum, 0).last());
}, options)
.add('kefir', function(deferred) {
runners.runKefir(deferred, kefirFromArray(a).skipDuplicates().scan(sum, 0).last());
}, options)
.add('bacon', function(deferred) {
runners.runBacon(deferred, bacon.fromArray(a).skipDuplicates().reduce(0, sum));
}, options)
runners.runSuite(suite);
function sum(x, y) {
return x + y;
}