Rxjs search in Angular I created an rxjs search for a hiring company, but I got rejected for the following reason:
-performance issue on a bigger data set.
-bugs
My question is how can I make this code better for the next time if I have similar task?
brands =  These should be populated from the brand property from the collection of products.

The search should match using the name, description and brand
properties.

The search should not fire unless a minimum of three characters have been typed.
The search should only fire a maximum of once per 400 milliseconds.

If searching for “xxx” with the brand selected as “xxx” and In Stock, then only products matching all three filters

const stackoverflowFilter = (filter: ISearch) =&gt; {
  return (product: IProducts): boolean =&gt; {
    return ((
      (
        (product.name.toLowerCase().indexOf(filter.input.toLowerCase()) !== -1) ||
        product.description.toLowerCase().indexOf(filter.input.toLowerCase()) !== -1 ||
        product.brand.toLowerCase().indexOf(filter.input.toLowerCase()) !== -1) &amp;&amp;
      product.brand.toLowerCase().indexOf(filter.select.toLowerCase()) !== -1 &amp;&amp;
      (filter.radio === 'all' ? true : filter.radio === 'in-stock' ? product.quantity &gt; 0 : product.quantity === 0)
    )
    )
  };
};

export interface ISearch { input: string; select: string; radio: string }



&gt; public getStackoverflow(): Observable&lt;Stackoverflow[]&gt; {
&gt;         return of([
&gt;           {
&gt;             id: &quot;1&quot;,
&gt;             name: &quot;This is just test data&quot;,
&gt;             price: &quot;89.9&quot;,
&gt;             description: &quot;test data&quot;,
&gt;             brand: &quot;stackoverflow&quot;,
&gt;             quantity: 4
&gt;           }....])}



 this.stackoverflow$ = this.service.getStackoverflow();

    this.stackoverflow$.pipe(
      tap((prod) =&gt; {
        this.stackoverflow = prod;
        this.brands = this.stackoverflow.map(({ brand }) =&gt; brand).reduce((prev, cur) =&gt; {
          if (prev.indexOf(cur) &lt; 0) prev.push(cur);
          return prev;
        }, []);
      }),
      concatMap(() =&gt;
        this.search$.pipe(
          debounceTime(400),
          distinctUntilChanged(),
          switchMap((search: string) =&gt;
            iif(() =&gt; search.length &gt;= 3,
              this.stackoverflow$.pipe(
                map(stackoverflow=&gt; stackoverflow.filter(stackoverflowFilter(this.filter))),
                distinctUntilChanged((curr, prev) =&gt; curr === prev),
              ),
              this.stackoverflow$.pipe(
                filter(() =&gt; search.length === 0),
              )
            )
          )
        ))
    ).subscribe(
      stackoverflow=&gt; { this.stackoverflow= stackoverflow; },
      (err) =&gt; throwError(err)
    );
If there are 1 million datasets, and i enter the first three letters, it will have to filter all 1 millionen values (let us asume the result will be 200 hits). When i now add a fourth letter, it will have to filter AGAIN all 1 million values and not just the 200 hits.
Therefor, if it is absolutly necessary to do the filtering codewise, then you could implement a kind of memory effect.
When the first filtering is executed (after the first 3 letters), you store the searchphrase and the filter result. When now the fourth letter is added, you just check if the first three letters are still the same. If yes, then you can shorten your filtering by just filtering the reduced list from the filtering before.
That way you swap runtime against memory consumption (a typical trade off).
My question would be if it is absolutly necessary to do the filtering codewise. When this search should be applied mostly on big datasets, then i would move the filtering to the backend. Then you have a lot more of requests to the backend, but the backend (the database to be precise) could handle such filtering much more efficiently.
But as always, it depends. For small sets of data, the roundtrip time (FE -&gt; BE -&gt; FE) for each search request will eat all performance gains that you have aquired with the more efficient filtering. But for large datasets, the roundtrip time will be less bad, in comparision to the filtering performance (and the memory consumption) on the client.
warm regards
Jan