Skip to content

Performance issue #48

@kuanyui

Description

@kuanyui

First, thanks for your project. This library is fabulous for TypeScript as a robust strong-typed event emitter, I doesn't find any other library can do this so well.

However, I found the performance is an issue which cannot be neglected in this library. I profile some EventEmitter libraries:

const Benchmark = require('benchmark')
const EETS = require('ee-ts/lib/ee').EventEmitter
const EE3 = require('eventemitter3')
const FE = require("@foxify/events").default

let TMP = ''
function dummyFn(x) {
  TMP = x
}

class KlsOnXXX {
  run() {
    this.onHello('hello')
  }
}

class KlsEETS extends EETS {
  run() {
    this.emit('hello', 'hello')
  }
}
class KlsEE3 extends EE3 {
  run () {
    this.emit('hello', 'hello')
  }
}
class KlsFE extends FE {
  run () {
    this.emit('hello', 'hello')
  }
}

const kls_onxxx = new KlsOnXXX()
const kls_eets = new KlsEETS()
const kls_ee3 = new KlsEE3()
const kls_fe = new KlsFE()

kls_onxxx.onHello = function (ev) { dummyFn(ev) }
kls_eets.on('hello', function (ev) { dummyFn(ev) }  )
kls_ee3.on('hello', function (ev) { dummyFn(ev) }  )
kls_fe.on('hello', function (ev) { dummyFn(ev) }  )

var suite = new Benchmark.Suite
suite
.add('onXXX', function () {
  kls_onxxx.run()
})
.add('ee-ts', function () {
  kls_eets.run()
})
.add('ee3', function () {
  kls_ee3.run()
})
.add('fe', function () {
  kls_fe.run()
})
.on('cycle', function(event) {
  console.log(String(event.target));
})
.on('complete', function() {
  console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run({
  'async': false, // https://benchmarkjs.com/docs#options_async
  'delay': 1
});

And the result is astonishing...

onXXX x      193,150,752 ops/sec ±4.00% (88 runs sampled)
ee-ts x        1,732,183 ops/sec ±3.64% (77 runs sampled)
ee3 x         52,713,790 ops/sec ±1.11% (89 runs sampled)
fe x         122,749,677 ops/sec ±0.55% (89 runs sampled)
Fastest is onXXX

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions