05 Mar 2019
1 min

How to debounce an input while skipping the first entry

I was recently working on a task requiring filtering and sorting data on the frontend side. I came across an issue that I wanted to debounce user input in order to decrease a number of filter and sort operations, however initial operation should be performed as soon as possible (without debounce).

Let’s see how RxJS library helped me out!

Debounce every entry

Without the need for performing initial operation immediately, the solution might look like this:

import { of, BehaviorSubject, combineLatest } from 'rxjs';
import { debounceTime, delay, tap, map } from 'rxjs/operators';

class FrameworksService {
  private querySubject = new BehaviorSubject<string>('');

  private query$ = this.querySubject.pipe(
    debounceTime(2000)
  );

  private collection$ = of([
    'Angular',
    'React',
    'Vue'
  ]).pipe(
    delay(10)
  );

  filteredCollection$ = combineLatest(
    this.query$,
    this.collection$
  ).pipe(
    tap(() => console.log('filtering')),
    map(([query, collection]) => collection.filter(item => item.includes(query)))
  );

  filter(query: string) {
    this.querySubject.next(query);
  }
}
    
const frameworksService = new FrameworksService();

frameworksService.filter('a');

frameworksService.filteredCollection$.subscribe(console.log);

setTimeout(() => {
  frameworksService.filter('Angular');
  frameworksService.filter('React');
  frameworksService.filter('Vue');
  frameworksService.filter('React');
}, 5000);

Note that, each entry is debounced, hence you need to wait 2s (debounce time) for the first console.log (a sign of performing filtering action) to appear.

However, I wanted to perform the initial operation ASAP.

Debounce every entry except for the first one

Here comes the power of RxJS operators.

I simply created two streams representing query, namely:

private querySubject = new BehaviorSubject<string>('');

private initialQuery$ = this.querySubject.pipe(
  first()
);

private debouncedQuery$ = this.querySubject.pipe(
  skip(1),
  debounceTime(2000)
);

and combined them as follows:

private query$ = merge(
    this.initialQuery$,
    this.debouncedQuery$
);

The rest of the FrameworksService class remained unchanged.

With the above solution you will see the first console.log immediately, while further queries will be debounced.

Frequently asked questions

How do I debounce input in RxJS but emit the first value immediately?

Use first() for the initial emission, then skip(1) and debounceTime() for later emissions, and combine the streams with merge(). For example, merge querySubject.pipe(first()) with querySubject.pipe(skip(1), debounceTime(2000)) to emit the first query without waiting and debounce subsequent queries.

What does skip(1) do when debouncing a BehaviorSubject in RxJS?

skip(1) ignores the first value emitted by the BehaviorSubject subscription, so that value is not sent through debounceTime(). In this pattern, first() handles that initial emission separately, while skip(1) ensures only later query values are debounced. A BehaviorSubject emits its current value to a new subscriber.

Why use merge() with first() and debounceTime() for RxJS input filtering?

merge() combines the immediate initial-query stream and the debounced later-query stream into one query stream. The initial value comes from first(), while subsequent values pass through skip(1) and debounceTime(). That combined stream can then be used with combineLatest() to filter the collection whenever a query is emitted.

Does debouncing the initial query delay the first filter operation?

Yes, if the query stream applies debounceTime() to every value, the first filtering operation waits for the debounce interval. To avoid that delay, emit the first query through first() and debounce only later values. In the example, the collection also emits after a short delay(10), so filtering begins once both streams have emitted.

How can I debounce later search queries without losing the initial value?

Split the query stream so the initial emission is handled by first() and later emissions by skip(1) followed by debounceTime(), then recombine them with merge(). This preserves the initial query and delays filtering for subsequent rapid changes until the debounce interval passes.

Share this post

Sign up for our newsletter

Stay up-to-date with the trends and be a part of a thriving community.