forked from cujojs/most
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatMap.js
83 lines (73 loc) · 2.39 KB
/
flatMap.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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 xstreamFlattenConcurrently = require('xstream/extra/flattenConcurrently').default;
// flatMapping n streams, each containing m items.
// Results in a single stream that merges in n x m items
// In Array parlance: Take an Array containing n Arrays, each of length m,
// and flatten it to an Array of length n x m.
var mn = runners.getIntArg2(1000, 1000);
var a = build(mn[0], mn[1]);
function build(m, n) {
var a = new Array(n);
for(var i = 0; i< a.length; ++i) {
a[i] = buildArray(i*1000, m);
}
return a;
}
function buildArray(base, n) {
var a = new Array(n);
for(var i = 0; i< a.length; ++i) {
a[i] = base + i;
}
return a;
}
var suite = Benchmark.Suite('flatMap ' + mn[0] + ' x ' + mn[1] + ' streams');
var options = {
defer: true,
onError: function(e) {
e.currentTarget.failure = e.error;
}
};
suite
.add('most', function(deferred) {
runners.runMost(deferred, most.from(a).flatMap(most.from).reduce(sum, 0));
}, options)
.add('rx 4', function(deferred) {
runners.runRx(deferred, rx.Observable.fromArray(a).flatMap(rx.Observable.fromArray).reduce(sum, 0));
}, options)
.add('rx 5', function(deferred) {
runners.runRx5(deferred,
rxjs.Observable.from(a).flatMap(
function(x) {return rxjs.Observable.from(x)}).reduce(sum, 0))
}, options)
.add('xstream', function(deferred) {
runners.runXstream(deferred, xs.fromArray(a).map(xs.fromArray).compose(xstreamFlattenConcurrently).fold(sum, 0).last());
}, options)
.add('kefir', function(deferred) {
runners.runKefir(deferred, kefirFromArray(a).flatMap(kefirFromArray).scan(sum, 0).last());
}, options)
.add('bacon', function(deferred) {
runners.runBacon(deferred, bacon.fromArray(a).flatMap(bacon.fromArray).reduce(0, sum));
}, options)
.add('highland', function(deferred) {
runners.runHighland(deferred, highland(a).flatMap(highland).reduce(0, sum));
}, options);
runners.runSuite(suite);
function sum(x, y) {
return x + y;
}
function even(x) {
return x % 2 === 0;
}
function identity(x) {
return x;
}