-
Notifications
You must be signed in to change notification settings - Fork 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
perf(ReplaySubject): slightly improved performance #2677
Conversation
Generated by 🚫 dangerJS |
lol I need to update size diff output |
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.
Please verify that the loop with shift
is faster than the splice
src/ReplaySubject.ts
Outdated
if (spliceCount > 0) { | ||
_events.splice(0, spliceCount); | ||
for (let i = 0; i < spliceCount; i++) { | ||
_events.shift(); | ||
} |
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.
I'm actually shocked this would be faster. this section was O(1) now it's O(n).
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.
After reviewing perf, i think we should return it to splice
because of it's consistent performance, even if shift
is faster on smaller arrays.
@martinsik the change looks fantastic, just one thing I'd like you to verify if possible. I suspect that |
@benlesh I made this test to see which one is faster: https://jsperf.com/splice-vs-shift-2
is faster than this:
I don't know if there're any other aspects to consider. I guess size of the array shouldn't matter but maybe there are some specific cases when it does? |
Yeah, it looks like what I suspected. Which is that for small arrays While there's some serious wins for the smaller arrays, I think they die off quickly enough that it's worth looking at the more consistent (and better for a larger range of sizes) performance of I think we should move it back to |
@benlesh I understand, I switched back to I'm not sure about the performance test I made. It shows that RxJS 4 is faster than RxJS 5 but is this even possible? |
@benlesh I just want to ask if this PR is still relevant or it needs updates for 5.5 or 6.0 :). |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I was going through the
ReplaySubject
class and I think in some situations it can be simplified. Most importantly when I don't set thetimeWindow
parameter the buffer trimming method can be avoided completely and I can store just the values without wrapping them withReplayEvent
.Also, I tried some performance tests to see whether
splice()
orshift()
would be better. It seems that runningshift()
in a for loop is faster thansplice()
when removing< 25
items. However,splice()
is more consistent even when removing a large portion of an array. See https://jsperf.com/splice-vs-shift-2There were no benchmarks for Subjects in
perf
directory so I made a few myself but I'm not sure I'm testing it the right way. It shows that RxJS 4 implementation was faster than RxJS 5...Before:
After: