Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: Faster ascending array index #3976

Closed
wants to merge 5 commits into from
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();
}