Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
events: improve arrayClone performance#33774
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -322,7 +322,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) { | ||
| } | ||
| } else { | ||
| const len = handler.length; | ||
| const listeners = arrayClone(handler, len); | ||
| const listeners = arrayClone(handler); | ||
| for (let i = 0; i < len; ++i) { | ||
| const result = ReflectApply(listeners[i], this, args); | ||
| @@ -560,7 +560,7 @@ function _listeners(target, type, unwrap) { | ||
| return unwrap ? [evlistener.listener || evlistener] : [evlistener]; | ||
| return unwrap ? | ||
| unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length); | ||
| unwrapListeners(evlistener) : arrayClone(evlistener); | ||
| } | ||
| EventEmitter.prototype.listeners = function listeners(type) { | ||
| @@ -599,11 +599,17 @@ EventEmitter.prototype.eventNames = function eventNames() { | ||
| return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : []; | ||
| }; | ||
| function arrayClone(arr, n) { | ||
| const copy = new Array(n); | ||
| for (let i = 0; i < n; ++i) | ||
| copy[i] = arr[i]; | ||
| return copy; | ||
| function arrayClone(arr) { | ||
| // At least since V8 8.3, this implementation is faster than the previous | ||
| // which always used a simple for-loop | ||
| switch (arr.length) { | ||
| case 2: return [arr[0], arr[1]]; | ||
mscdex marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| case 3: return [arr[0], arr[1], arr[2]]; | ||
| case 4: return [arr[0], arr[1], arr[2], arr[3]]; | ||
| case 5: return [arr[0], arr[1], arr[2], arr[3], arr[4]]; | ||
| case 6: return [arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]]; | ||
| } | ||
| return arr.slice(); | ||
| } | ||
| function unwrapListeners(arr) { | ||
Uh oh!
There was an error while loading. Please reload this page.