Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between throttle and debounce in Rxswift3.0?

I have seen a lot of blogs about throttle and debounce. Most of them said they are the same thing. But I get the different result form my example? Here is the example:

let disposeBag = DisposeBag()
Observable.of(1,2,3,4,5)
          .debounce(1, scheduler: MainScheduler.instance)
          .subscribe(onNext: {print($0)})
          .addDisposableTo(disposeBag)

the result was 5. But when I used throttle, the result was 1

let disposeBag = DisposeBag()
Observable.of(1,2,3,4,5)
        .throttle(1, scheduler: MainScheduler.instance)
        .subscribe(onNext: {print($0)})
        .addDisposableTo(disposeBag)

So,I can't understand about the throttle operator?

like image 641
Longshihua Avatar asked May 10 '17 09:05

Longshihua


People also ask

Should I use throttle or debounce?

The major difference between debouncing and throttling is that debounce calls a function when a user hasn't carried out an event in a specific amount of time, while throttle calls a function at intervals of a specified amount of time while the user is carrying out an event.

What is debounce and throttle?

Debouncing is a technique where we can monitor the time delay of user action and once that delay reaches our predetermined threshold we can can make the function call. Throttling is a technique where we make the function call in a predetermined time interval irrespective of continuous user actions.

What is debounce in Rxswift?

Debounce: the original function is called after the caller stops calling the decorated function after a specified period. That's it!

What is Throttle click?

Throttling or sometimes is also called throttle function is a practice used in websites. Throttling is used to call a function after every millisecond or a particular interval of time only the first click is executed immediately.


2 Answers

In earlier versions of RxSwift, throttle and debounce did the same thing, which is why you will see articles stating this. In RxSwift 3.0 they do a similar but opposite thing.

Both debounce and throttle are used to filter items emitted by an observable over time.

  • throttle emits only the first item emitted by the source observable in the time window.

  • debounce only emits an item after the specified time period has passed without another item being emitted by the source observable.

Both can be used to reduce the number of items emitted by an observable; which one you use depends on whether you want the "first" or "last" value emitted in a time period.

The term "debounce" comes from electronics and refers to the tendency of switch contacts to "bounce" between on and off very quickly when a switching action occurs. You won't notice this when you turn on a lightbulb but a microprocessor looking at an input thousands of times a second will see a rapid sequence of "ons" and "offs" before the switch settles into its final state. This is why debounce gives you the value of 5; the final item that was emitted in your time frame (1 ms). If you put a time delay into your code so that the items were emitted more slowly (more than 1ms apart) you would see a number of items emitted by debounce.

In an app you could use debounce for performing a search that is expensive (say it requires a network operation). The user is going to type a number of characters characters for their search string but you don't want to initiate a search as they enter each character, since the search is expensive and the earlier results will be obsolete by the time they return. Using debounce you can ensure that the search string is only emitted once the user stops typing for a period (say 500ms).

You might use throttle where an operation takes some time and you want to ignore further input until that time has elapsed. Say you have a button that initiates an operation. If the user taps the button multiple times in quick succession you only want to initiate the operation once. You can use throttle to ignore the subsequent taps within a specified time window. debounce could also work but would introduce a delay before the action item was emitted, while throttle allows you to react to the first action and ignore the rest.

like image 94
Paulw11 Avatar answered Oct 21 '22 14:10

Paulw11


Simple explanation is here https://medium.com/fantageek/throttle-vs-debounce-in-rxswift-86f8b303d5d4

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.

like image 22
Shashank Mishra Avatar answered Oct 21 '22 15:10

Shashank Mishra