Browse Source

events: optimize listener array cloning

This both switches to a single algorithm for array cloning and also
speeds up (by ~100% in the ee-listeners-many benchmark) the
"many elements"  case that was previously handled by
`array.slice()`.

PR-URL: https://github.com/iojs/io.js/pull/1050
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Julian Duque <julianduquej@gmail.com>
v1.8.0-commit
Brian White 10 years ago
parent
commit
555a7c48cf
  1. 19
      lib/events.js

19
lib/events.js

@ -384,7 +384,7 @@ EventEmitter.prototype.listeners = function listeners(type) {
else if (typeof evlistener === 'function') else if (typeof evlistener === 'function')
ret = [evlistener]; ret = [evlistener];
else else
ret = arrayClone(evlistener); ret = arrayClone(evlistener, evlistener.length);
} }
return ret; return ret;
@ -413,16 +413,9 @@ function spliceOne(list, index) {
list.pop(); list.pop();
} }
function arrayClone(arr, len) { function arrayClone(arr, i) {
var ret; var copy = new Array(i);
if (len === undefined) while (i--)
len = arr.length; copy[i] = arr[i];
if (len >= 50) return copy;
ret = arr.slice();
else {
ret = new Array(len);
for (var i = 0; i < len; i += 1)
ret[i] = arr[i];
}
return ret;
} }

Loading…
Cancel
Save