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 Throttle operator?
Even thought 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. In RxSwift, the operator above is actually Throttle. It’s only emit the last item in 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 time 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 be called at most once per specified period.
Debounce: the original function be 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.