many-on.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. // Benchmark comparing performance of event emit for many listeners
  3. // To run it, do following in memoizee package path:
  4. //
  5. // $ npm install eventemitter2 signals
  6. // $ node benchmark/many-on.js
  7. var forEach = require('es5-ext/object/for-each')
  8. , pad = require('es5-ext/string/#/pad')
  9. , now = Date.now
  10. , time, count = 1000000, i, data = {}
  11. , ee, native, ee2, signals, a = {}, b = {};
  12. ee = (function () {
  13. var ee = require('../')();
  14. ee.on('test', function () { return arguments; });
  15. ee.on('test', function () { return arguments; });
  16. return ee.on('test', function () { return arguments; });
  17. }());
  18. native = (function () {
  19. var ee = require('events');
  20. ee = new ee.EventEmitter();
  21. ee.on('test', function () { return arguments; });
  22. ee.on('test', function () { return arguments; });
  23. return ee.on('test', function () { return arguments; });
  24. }());
  25. ee2 = (function () {
  26. var ee = require('eventemitter2');
  27. ee = new ee.EventEmitter2();
  28. ee.on('test', function () { return arguments; });
  29. ee.on('test', function () { return arguments; });
  30. return ee.on('test', function () { return arguments; });
  31. }());
  32. signals = (function () {
  33. var Signal = require('signals')
  34. , ee = { test: new Signal() };
  35. ee.test.add(function () { return arguments; });
  36. ee.test.add(function () { return arguments; });
  37. ee.test.add(function () { return arguments; });
  38. return ee;
  39. }());
  40. console.log("Emit for 3 listeners", "x" + count + ":\n");
  41. i = count;
  42. time = now();
  43. while (i--) {
  44. ee.emit('test', a, b);
  45. }
  46. data["event-emitter (this implementation)"] = now() - time;
  47. i = count;
  48. time = now();
  49. while (i--) {
  50. native.emit('test', a, b);
  51. }
  52. data["EventEmitter (Node.js native)"] = now() - time;
  53. i = count;
  54. time = now();
  55. while (i--) {
  56. ee2.emit('test', a, b);
  57. }
  58. data.EventEmitter2 = now() - time;
  59. i = count;
  60. time = now();
  61. while (i--) {
  62. signals.test.dispatch(a, b);
  63. }
  64. data.Signals = now() - time;
  65. forEach(data, function (value, name, obj, index) {
  66. console.log(index + 1 + ":", pad.call(value, " ", 5), name);
  67. }, null, function (a, b) {
  68. return this[a] - this[b];
  69. });