Skip to content

Commit

Permalink
events: optimize listener array cloning
Browse files Browse the repository at this point in the history
This both switches to a single algorithm for array cloning and also
speeds up (by ~100% in the ee-listeners-many benchmark) the
"many elements"  case that was previously handled by
`array.slice()`.

PR-URL: #1050
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Julian Duque <[email protected]>
  • Loading branch information
mscdex committed Mar 5, 2015
1 parent 563771d commit 555a7c4
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ EventEmitter.prototype.listeners = function listeners(type) {
else if (typeof evlistener === 'function')
ret = [evlistener];
else
ret = arrayClone(evlistener);
ret = arrayClone(evlistener, evlistener.length);
}

return ret;
Expand Down Expand Up @@ -413,16 +413,9 @@ function spliceOne(list, index) {
list.pop();
}

function arrayClone(arr, len) {
var ret;
if (len === undefined)
len = arr.length;
if (len >= 50)
ret = arr.slice();
else {
ret = new Array(len);
for (var i = 0; i < len; i += 1)
ret[i] = arr[i];
}
return ret;
function arrayClone(arr, i) {
var copy = new Array(i);
while (i--)
copy[i] = arr[i];
return copy;
}

0 comments on commit 555a7c4

Please sign in to comment.