-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM if results from #14081 are confirmed.
The benchmark results don't really mean anything as |
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
|
@TimothyGu doesn't that invert the order? |
@lpinca Yes. The |
Oh, got it. |
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();
else
spliceOne(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. |
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." |
@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... |
Ooof, I assumed the benchmark exercised the |
It probably makes sense to update the benchmark as per @TimothyGu's diff. |
Yes, and maybe even better, add it as a benchmark in addition to the existing one rather than updating the existing one. |
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