-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Split the child counting into two functions #32
Conversation
1. tracks add/remove of child nodes and increments/decrements the counter 2. one function is triggered when the counter is deleted and it recounts the child nodes The delta counter in #1 is more efficient than counting all children every time, since it only needs access to the delta. The recounter function in #2 is equally inefficient as before, but is needed a lot less.
Nifty! I like it. Thanks @puf! |
First we must understand that the likes of users should not be putter inside the post, just likes_count should be in the post.
This code is good especially if you are using an external server and not the firebase functions. Why is this code bad? The solution is to actually use "numChildren"
or
|
It is very inefficient to download the whole likes data. You may not pay for data transfer but you will pay for CPU/memory time usage both of which will increase a lot in this scenario. Imagine a popular picture/post which gets 20 Million likes, this means that you will need to count, in average 10 Million likes 20 million times... I haven't done the math but that is going to be very expensive. (yep 200 Trillion likes downloaded...) Also it's true to say that data discrepancy can occur with Functions (some events may be double fired in some rare occasions). but typically these real-time "counts" don't need to be very accurate (I'm pretty sure likes on Facebook/instagram are not perfectly accurate and if you pay attention you will see that Likes and views count in Youtube are only real-time up to around 300 likes/views after which they switch to updating them every 24h or so. |
The multiplication is all wrong, but I understand what you said. It is essential to give preference to perfomace, since the amount of likes can be high. And by weighing that you said, I repeat that the likes can not be inside the post. What about followers and favorites? Is this methodology correct? |
Now it's beautiful. Look:
My question now is: is it better to set applyLocally to true? |
This PR splits the child counting into two functions:
The delta counter in #1 is more efficient than counting all children every time, since it only needs
access to the delta. The recounter function in #2 is equally inefficient as before, but is needed
a lot less.