我想比較JS EventEmitter和RxJS的性能。我寫了下面的基準腳本來做到這一點:EventEmitter vs RxJS vs Kefir
性能測試
import Rx from 'rxjs/Rx';
import Kefir from 'kefir';
import { EventEmitter } from "events";
let Benchmark = require ("benchmark");
let suite = new Benchmark.Suite;
suite
.add('for',() => {
let numArray = [1,2,3,4,5,6,7,8,9,10];
let count = 0;
for (let i = 0; i<numArray.length; i++)
count += numArray[i];
})
.add('forEach',() => {
let numArray = [1,2,3,4,5,6,7,8,9,10];
let count = 0;
numArray.forEach((num) => { count += num; });
})
.add('eventEmitter',() => {
let numArray = [1,2,3,4,5,6,7,8,9,10];
let count = 0;
let myEmitter = new EventEmitter();
myEmitter.on('number', (num) => { count += num; });
numArray.forEach((num) => { myEmitter.emit('number', num); });
})
.add('rxjs',() => {
let numArray = [1,2,3,4,5,6,7,8,9,10];
let count = 0;
let source = Rx.Observable.from(numArray)
.do((x) => { count += x }, (error) => {},() => {});
source.subscribe((x) => {}, (error) => {},() => {});
})
.add('kefir',() => {
let numArray = [1,2,3,4,5,6,7,8,9,10];
let count = 0;
let stream = Kefir.sequentially(0, numArray);
count = stream.scan(sum => sum + 1, 0);
})
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Slowest is ' + this.filter('slowest').map('name'));
})
.run({'async': true});
性能結果
for x 47,595,916 ops/sec ±1.58% (87 runs sampled)
forEach x 4,428,485 ops/sec ±0.75% (86 runs sampled)
eventEmitter x 1,478,876 ops/sec ±0.61% (86 runs sampled)
rxjs x 547,732 ops/sec ±0.66% (86 runs sampled)
kefir x 496,709 ops/sec ±5.15% (50 runs sampled)
Slowest is kefir
正如你們所看到酸牛奶竟然是最慢違背要求在這link。
- 我在編寫測試中做錯了什麼?
- 如果有人能解釋爲什麼 發生,那將是非常好的。特別是當你將它與javascript event-emitter進行比較時。
1)該鏈接是RxJS 4,最新版本是v5,這是我認爲你正在使用的。 v5從頭開始重建,重點在於性能。 2)你沒有比較庫之間的相同操作,你爲RxJS添加了'do',並且正在使用'scan'來開發Kefir。 3)你究竟在想什麼?像這樣的微不足道的基準測試並沒有真正告訴你任何關於庫的行爲的任何可觀的規模。 – paulpdaniels
我想比較EventEmitters和Reactive Programming Frameworks之間的性能。哪一個會更高效。我想知道這些性能差異背後的原因。由於我是反應式編程的新手,我不知道所有的功能。我只是用我能找到的第一件事來總結數組中的值。如果你能推薦更好的基準測試技術,那將是非常好的。謝謝 – pntripathi9417