This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Transformation operators: debounce and debounceTime

5:21 RxJS lesson by

Debounce and debounceTime operators are similar to delayWhen and delay, with the difference that they may drop some emissions. This lesson teaches you how debounce works and where is it useful, for instance when building a type-ahead UI.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Debounce and debounceTime operators are similar to delayWhen and delay, with the difference that they may drop some emissions. This lesson teaches you how debounce works and where is it useful, for instance when building a type-ahead UI.

Avatar
Rafael

I think on first diagram example, using debounceTime(1000), 4| should stop at the same time as the 4| from the source stream, since the stream completes there. When the source stream ends, debounceTime will push its values right away.

/*
--0--1--2--3--4|    timer
--------------4|    debounceTime(1000)
*/
In reply to egghead.io
Avatar
Andre

Correct Rafael, that was my mistake.

In reply to Rafael

The next operators we're going to learn are debounce and debounceTime, which are quite similar to delayWhen and Delay. While Delay time shifts each emission by one second, debounce waits for one second of silence, and then it emits the most recent value.

I know those two sentences sounded quite similar, but there's a big difference. DebounceTime also takes the same parameter here, but we're going to highlight one of its properties, which is it waits for that much of silence.

What does silence actually mean? It's these dashes here. This input observable emits every 100 milliseconds, so it means that this much here is 100 milliseconds.

As you can see, these dashes here represent 100 milliseconds of silence, but they're not enough for being 1,000 milliseconds of silence. Let's look at how debounceTime works as a marble diagram. It takes the input, which is zero, the input emission, and then it keeps that in memory. It has a memory inside it. Then it starts seeking for 1,000 milliseconds of event silence, which would be this much.

As you can see, we didn't have enough silence, so it just drops zero in its memory, and it keeps one in its memory. Then it keeps on doing that for one, as well. It tries to look for one second of event silence, but it didn't have enough of event silence. It just keeps on doing that.

Then finally, here with four, nothing happened after four and complete happened. No event happened here. There was no emission of any value, and that's exactly when it will emit four and complete here, because it had enough silence. It had one second of silence.

If we run this, and let's just call debounceTime first, when we run it, we see just four and done. As you can see, debounceTime was dropping all of these values, because they didn't have enough silence. If you would change the parameter to say something like 50, which is much shorter than 100, so it's probably this much of time.

It means that here, in this case, it does have enough event silence for each of these emissions, so debounceTime with 50, we're going to see all of these values. It didn't drop any of those. It actually behaved, in this case, just like Delay.

DebounceTime has this property that it's able to drop things if they happen too fast. That's why we want to use debounceTime. In case things are happening too fast, we don't want all of those events that happened in that sequence of fast emissions. We want only the most recent one, which is four, in that case. DebounceTime is quite useful for cases like building a type ahead, for instance.

Here, we have a case where, whenever I change something on this input field, it will immediately change the label here at the bottom. With debounceTime, you're able to say, "Only show or update the label after one second has passed that the user hasn't done anything." In this case here, I type, and then after one second, it shows what I type, which was quite different to Delay of one second.

In that case, I would get each of these intermediate letters. Let's say you're doing something like a request to the server to do a search. You don't want to send a request for H, then a request for H-E, and then a request for H-E-L, etc. You want the request that means really what the user meant. He meant to send "Hello," and we know that because the user has done nothing for one second. Then we can emit the latest value.

DebounceTime is comparable to Delay, and delayWhen is comparable to just Debounce. The name here is slightly different, but this is what they correspond to. That means that if you want to specify a function here, you could do this.

You could return here an observable that says, "How much of event silence do you want to wait for." You could do something like that and take one, and this will do the same thing as we had before with debounceTime. It shows only four, and it's essentially the same idea as delayWhen.

We use debounceTime and debounce to let you drop some emissions, while at the same time delaying the other emissions in the case emissions are happening too fast.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?