Throttle vs Debounce in RxSwift

Khuong Pham
Indie Goodies
Published in
2 min readMay 7, 2017

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:

Debounce

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.

Only emit the last tap event in a particular timespan

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

https://indiegoodies.com/breather

--

--