From d0c238cc81a7bc4d67453ae2952d7d9368069f89 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 20 Dec 2014 00:03:10 +0100 Subject: [PATCH 1/2] benchmark: fix printing of large numbers Don't use Number#toPrecision(), it switches to scientific notation for numbers with more digits than the precision; use Number#toFixed(). PR-URL: https://github.com/iojs/io.js/pull/185 Reviewed-By: Chris Dickinson --- benchmark/common.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/common.js b/benchmark/common.js index ff54f2ea495ef5..7d1730c373d8ae 100644 --- a/benchmark/common.js +++ b/benchmark/common.js @@ -187,7 +187,7 @@ Benchmark.prototype.end = function(operations) { Benchmark.prototype.report = function(value) { var heading = this.getHeading(); if (!silent) - console.log('%s: %s', heading, value.toPrecision(5)); + console.log('%s: %s', heading, value.toFixed(0)); process.exit(0); }; From d3f8db124463e478420d8bba6fa5cf13af7b4ecb Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 20 Dec 2014 00:04:35 +0100 Subject: [PATCH 2/2] lib: micro-optimize EventEmitter#removeListener() 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 --- benchmark/events/ee-add-remove.js | 23 +++++++++++++++++++++++ lib/events.js | 9 ++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 benchmark/events/ee-add-remove.js diff --git a/benchmark/events/ee-add-remove.js b/benchmark/events/ee-add-remove.js new file mode 100644 index 00000000000000..cd9fe3cb7de057 --- /dev/null +++ b/benchmark/events/ee-add-remove.js @@ -0,0 +1,23 @@ +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); +} diff --git a/lib/events.js b/lib/events.js index 3ee16791dd808f..49b49e6f1198b1 100644 --- a/lib/events.js +++ b/lib/events.js @@ -246,7 +246,7 @@ EventEmitter.prototype.removeListener = list.length = 0; delete this._events[type]; } else { - list.splice(position, 1); + spliceOne(list, position); } if (this._events.removeListener) @@ -318,3 +318,10 @@ EventEmitter.listenerCount = function(emitter, type) { 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(); +}