single-on.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. // Benchmark comparing performance of event emit for single listener
  3. // To run it, do following in memoizee package path:
  4. //
  5. // $ npm install eventemitter2 signals
  6. // $ node benchmark/single-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. return ee().on('test', function () { return arguments; });
  15. }());
  16. native = (function () {
  17. var ee = require('events');
  18. return (new ee.EventEmitter()).on('test', function () { return arguments; });
  19. }());
  20. ee2 = (function () {
  21. var ee = require('eventemitter2');
  22. return (new ee.EventEmitter2()).on('test', function () { return arguments; });
  23. }());
  24. signals = (function () {
  25. var Signal = require('signals')
  26. , ee = { test: new Signal() };
  27. ee.test.add(function () { return arguments; });
  28. return ee;
  29. }());
  30. console.log("Emit for single listener", "x" + count + ":\n");
  31. i = count;
  32. time = now();
  33. while (i--) {
  34. ee.emit('test', a, b);
  35. }
  36. data["event-emitter (this implementation)"] = now() - time;
  37. i = count;
  38. time = now();
  39. while (i--) {
  40. native.emit('test', a, b);
  41. }
  42. data["EventEmitter (Node.js native)"] = now() - time;
  43. i = count;
  44. time = now();
  45. while (i--) {
  46. ee2.emit('test', a, b);
  47. }
  48. data.EventEmitter2 = now() - time;
  49. i = count;
  50. time = now();
  51. while (i--) {
  52. signals.test.dispatch(a, b);
  53. }
  54. data.Signals = now() - time;
  55. forEach(data, function (value, name, obj, index) {
  56. console.log(index + 1 + ":", pad.call(value, " ", 5), name);
  57. }, null, function (a, b) {
  58. return this[a] - this[b];
  59. });