events: improve performance of EventEmitter.emit

This restore some performance we lost when we introduced primordialias.
Improvements are up to +100%.

PR-URL: https://github.com/nodejs/node/pull/29633
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Matteo Collina 2019-09-20 19:06:20 +02:00
parent 1fa403762c
commit c5f5f84a33

View File

@ -22,6 +22,7 @@
'use strict';
const { Math, Object, Reflect } = primordials;
const apply = Reflect.apply;
var spliceOne;
@ -206,12 +207,12 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
return false;
if (typeof handler === 'function') {
Reflect.apply(handler, this, args);
apply(handler, this, args);
} else {
const len = handler.length;
const listeners = arrayClone(handler, len);
for (var i = 0; i < len; ++i)
Reflect.apply(listeners[i], this, args);
apply(listeners[i], this, args);
}
return true;