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 3 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
19 changes: 13 additions & 6 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,16 @@ function spliceOne(list, index) {
list.pop();
}

function arrayClone(arr, i) {
var copy = new Array(i);
while (i--)
copy[i] = arr[i];
return copy;
}
// Optimized common usages (listeners < 50) and using slice for
// large arrays so it scales much better
function arrayClone(arr, length) {
if (length === 1) {
return [arr[0]];
} else if (length < 50) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for else. you're returning early.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also a code comment about why 50 was chosen. At first look the decision seems arbitrary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like using elseif in those cases so the code looks more compacted in easy functions. Anyway in order to add a comment in the second case a simple if looks better.

var copy = new Array(length);
for (var i = 0; i < length; i++)
copy[i] = arr[i];
return copy;
}
return arr.slice();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No newline at the end of file?