diff --git a/lib/events.js b/lib/events.js index 589dc4e79ea234..67e224af370e2f 100644 --- a/lib/events.js +++ b/lib/events.js @@ -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(); }