Throttle vs Debounce in RxSwift
When I went to reactivex.io, I got a whole lot of confusion between the terms throttle and debounce in the reactive programming world. I only see debounce operator. Wait, what? Where is the Throttle operator?
Even though they define the debounce operator like this:
In fact, these two operators do nearly the same thing, but not the same at all, for me, a big difference. The throttle only emits the last item in a particular timespan. It's useful when you want to filter multiple events like tapping on the button.
Let’s do a simple example to see the difference between them. I will press continually the button on the screen and we see the console how many times it prints “Tap!”
Ok, it works perfectly like I said above, right :)
Now I will change throttle to debounce
Build again, and what will happen.
With debounce, it will reset the timer immediately if I press the button. After 2 seconds I don’t press it, “Tap!” is printed.
I think you’ve already understood the difference between these two operators. So the summary is
Throttle: the original function is called at most once per specified period.
Debounce: the original function is called after the caller stops calling the decorated function after a specified period.
That’s it! I hope you enjoy this article, thanks for reading and I will see you next time.
Follow me on GitHub (https://github.com/khuong291)
Support my apps