Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,21 @@ function spliceOne(list, index) {
list.pop();
}

function arrayClone(arr, i) {
var copy = new Array(i);
while (i--)
copy[i] = arr[i];
return copy;
// As of node.js v5.4.1 (v8 v4.6.85.31) benchmarks showed, a simple for
// loop performs better than Array#slice() for arrays smaller than 27
// elements, it is worth it, because it's more likely to use EventEmitters
// with a small number of listeners.
function arrayClone(arr, length) {
if (length === 1) {
return [arr[0]];
}

if (length < 27) {
var copy = new Array(length);
for (var i = 0; i < length; i++)
copy[i] = arr[i];
return copy;
}

return arr.slice();
}