Uh oh!
There was an error while loading. Please reload this page.
events: remove internal spliceOne() - #14082
Conversation
spliceOne() was added as a faster alternative to Array#splice in the particular use case in events.js. However, current master shows no difference in performance with or without spliceOne(): improvement confidence p.value events/ee-add-remove.js n=250000 0.14 % 0.7573913
TimothyGu
commented
Jul 5, 2017
The benchmark results don't really mean anything as |
TimothyGu
commented
Jul 5, 2017
With diff --git a/benchmark/events/ee-add-remove.js b/benchmark/events/ee-add-remove.js
index 99d85367cb..1f560b34cd 100644
--- a/benchmark/events/ee-add-remove.js+++ b/benchmark/events/ee-add-remove.js@@ -16,7 +16,7 @@ function main(conf) {
bench.start();
for (var i = 0; i < n; i += 1) {
- for (k = listeners.length; --k >= 0; /* empty */)+ for (k = 0; k < listeners.length; k++)
ee.on('dummy', listeners[k]);
for (k = listeners.length; --k >= 0; /* empty */)
ee.removeListener('dummy', listeners[k]);which is about as synthetic as the original benchmark, I got |
lpinca
commented
Jul 5, 2017
@TimothyGu doesn't that invert the order? |
TimothyGu
commented
Jul 5, 2017
@lpinca Yes. The |
lpinca
commented
Jul 5, 2017
Oh, got it. |
refack
commented
Jul 5, 2017
Also the original's p-value is way too high (which makes sense if the code paths are the same) |
if(position===0)list.shift();elsespliceOne(list,position);the benchmark script will never call spliceOne。 But I'm still curious about why Array.splice is slower than spliceOne when run benchmark(script change to the same with @TimothyGu ), but quicker when I use console.time. |
Sunqinying
commented
Jul 5, 2017
I find the commit with comment is that "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." |
refack
commented
Jul 5, 2017
@Sunqinying it is interesting... probably comes from the fact your code does a 1000 splices on a single array, and node's benchmark probably does less. Might also be because your array if filled with numbers, and here the array has function pointers... |
Trott
commented
Jul 5, 2017
Ooof, I assumed the benchmark exercised the |
lpinca
commented
Jul 5, 2017
It probably makes sense to update the benchmark as per @TimothyGu's diff. |
Trott
commented
Jul 5, 2017
Yes, and maybe even better, add it as a benchmark in addition to the existing one rather than updating the existing one. |
bnoordhuis
commented
Jul 6, 2017
That's correct. V8 wasn't (and presumably isn't) smart enough to figure out the return value is unused and can be omitted.
For posterity, that's commit d3f8db1. At least I was honest about what I was doing. :-) |
spliceOne() was added as a faster alternative to Array#splice in the
particular use case in events.js. However, current master shows no
difference in performance with or without spliceOne():
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)
events