-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(scan): add performance benchmark for fold
- Loading branch information
Andre Medeiros
committed
Feb 27, 2016
1 parent
42b6f12
commit 5d5ef94
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
var Benchmark = require('benchmark'); | ||
var xs = require('../lib/index').default; | ||
var most = require('most'); | ||
var rx = require('rx'); | ||
var rxjs = require('@reactivex/rxjs'); | ||
var kefir = require('kefir'); | ||
var bacon = require('baconjs'); | ||
var lodash = require('lodash'); | ||
var highland = require('highland'); | ||
|
||
var runners = require('./runners'); | ||
var kefirFromArray = runners.kefirFromArray; | ||
|
||
// 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; i< a.length; ++i) { | ||
a[i] = i; | ||
} | ||
|
||
var suite = Benchmark.Suite('scan -> reduce ' + n + ' integers'); | ||
var options = { | ||
defer: true, | ||
onError: function(e) { | ||
e.currentTarget.failure = e.error; | ||
} | ||
}; | ||
|
||
suite | ||
.add('xstream', function(deferred) { | ||
runners.runXStream(deferred, xs.from(a).fold(sum, 0).fold(passthrough, 0).last()); | ||
}, options) | ||
.add('most', function(deferred) { | ||
runners.runMost(deferred, most.from(a).scan(sum, 0).reduce(passthrough, 0)); | ||
}, options) | ||
.add('rx 4', function(deferred) { | ||
runners.runRx(deferred, rx.Observable.fromArray(a).scan(sum, 0).reduce(passthrough, 0)); | ||
}, options) | ||
.add('rx 5', function(deferred) { | ||
runners.runRx5(deferred, rxjs.Observable.fromArray(a).scan(sum, 0).reduce(passthrough, 0)); | ||
}, options) | ||
.add('kefir', function(deferred) { | ||
runners.runKefir(deferred, kefirFromArray(a).scan(sum, 0).scan(passthrough, 0).last()); | ||
}, options) | ||
.add('bacon', function(deferred) { | ||
runners.runBacon(deferred, bacon.fromArray(a).scan(0, sum).reduce(0, passthrough)); | ||
}, options) | ||
.add('highland', function(deferred) { | ||
runners.runHighland(deferred, highland(a).scan(0, sum).reduce(0, passthrough)); | ||
}, options) | ||
.add('lodash', function() { | ||
return lodashScan(sum, 0, a).reduce(passthrough, 0); | ||
}) | ||
.add('Array', function() { | ||
return arrayScan(sum, 0, a).reduce(passthrough, 0); | ||
}); | ||
|
||
runners.runSuite(suite); | ||
|
||
function arrayScan(f, initial, a) { | ||
var result = initial; | ||
return a.map(function(x) { | ||
return result = f(result, x); | ||
}); | ||
} | ||
|
||
function lodashScan(f, initial, a) { | ||
var result = initial; | ||
return lodash(a).map(function(x) { | ||
return result = f(result, x); | ||
}); | ||
} | ||
|
||
function sum(x, y) { | ||
return x + y; | ||
} | ||
|
||
function passthrough(z, x) { | ||
return x; | ||
} |