Replace the call to Array#splice() with a faster open-coded version
that creates less garbage.
Add a new benchmark to prove it. With the change applied, it scores
a whopping 40% higher.
PR-URL: https://github.com/iojs/io.js/pull/185
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
--- /dev/null
+var common = require('../common.js');
+var events = require('events');
+
+var bench = common.createBenchmark(main, {n: [25e4]});
+
+function main(conf) {
+ var n = conf.n | 0;
+
+ var ee = new events.EventEmitter();
+ var listeners = [];
+
+ for (var k = 0; k < 10; k += 1)
+ listeners.push(function() {});
+
+ bench.start();
+ for (var i = 0; i < n; i += 1) {
+ for (var k = listeners.length; --k >= 0; /* empty */)
+ ee.on('dummy', listeners[k]);
+ for (var k = listeners.length; --k >= 0; /* empty */)
+ ee.removeListener('dummy', listeners[k]);
+ }
+ bench.end(n);
+}
list.length = 0;
delete this._events[type];
} else {
- list.splice(position, 1);
+ spliceOne(list, position);
}
if (this._events.removeListener)
ret = emitter._events[type].length;
return ret;
};
+
+// About 1.5x faster than the two-arg version of Array#splice().
+function spliceOne(list, index) {
+ for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1)
+ list[i] = list[k];
+ list.pop();
+}