From 83c0cc56d37282f5a8211d5bb62ec030060d0595 Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 30 Jun 2024 16:52:39 +0200 Subject: [PATCH 1/2] search: improve performance --- demo/flake.lock | 108 +++++++++------------------------ demo/flake.nix | 1 + src/app/app.component.html | 16 +++-- src/app/app.component.scss | 5 -- src/app/app.component.ts | 19 +++--- src/app/core/search.service.ts | 35 ++++++++++- 6 files changed, 83 insertions(+), 101 deletions(-) diff --git a/demo/flake.lock b/demo/flake.lock index 525dd9c..49218bf 100644 --- a/demo/flake.lock +++ b/demo/flake.lock @@ -2,7 +2,7 @@ "nodes": { "devshell": { "inputs": { - "flake-utils": "flake-utils_3", + "flake-utils": "flake-utils_2", "nixpkgs": [ "nixvim", "nixpkgs" @@ -60,11 +60,11 @@ ] }, "locked": { - "lastModified": 1717285511, - "narHash": "sha256-iKzJcpdXih14qYVcZ9QC9XuZYnPc6T8YImb6dX166kw=", + "lastModified": 1719745305, + "narHash": "sha256-xwgjVUpqSviudEkpQnioeez1Uo2wzrsMaJKJClh+Bls=", "owner": "hercules-ci", "repo": "flake-parts", - "rev": "2a55567fcf15b1b1c7ed712a2c6fadaec7412ea8", + "rev": "c3c5ecc05edc7dafba779c6c1a61cd08ac6583e9", "type": "github" }, "original": { @@ -95,24 +95,6 @@ "inputs": { "systems": "systems_2" }, - "locked": { - "lastModified": 1710146030, - "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, - "flake-utils_3": { - "inputs": { - "systems": "systems_3" - }, "locked": { "lastModified": 1701680307, "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=", @@ -184,11 +166,11 @@ ] }, "locked": { - "lastModified": 1719588253, - "narHash": "sha256-A03i8xiVgP14DCmV5P7VUv37eodCjY4e1iai0b2EuuM=", + "lastModified": 1719677234, + "narHash": "sha256-qO9WZsj/0E6zcK4Ht1y/iJ8XfwbBzq7xdqhBh44OP/M=", "owner": "nix-community", "repo": "home-manager", - "rev": "7e68e55d2e16d3a1e92a679430728c35a30fd24e", + "rev": "36317d4d38887f7629876b0e43c8d9593c5cc48d", "type": "github" }, "original": { @@ -220,18 +202,22 @@ }, "nixos-modules": { "inputs": { - "flake-utils": "flake-utils_2", + "flake-utils": [ + "flake-utils" + ], "nixpkgs": [ "nixpkgs" ], - "search": "search" + "search": [ + "search" + ] }, "locked": { - "lastModified": 1719615856, - "narHash": "sha256-MEMUsSLfzhMTGmYMGj4G3yQK7Gh4FVwb62oJFRZysjM=", + "lastModified": 1719848777, + "narHash": "sha256-TxaV5eO+8CB9m7hknJ3eiP6isZAcu6uL2F21a5R9yBI=", "owner": "nuschtos", "repo": "nixos-modules", - "rev": "ee8686daa5510d79e6d8134d56a9e477bcb5e3ce", + "rev": "1f61073cb4d9dfbd3432ba8c0cd85b84cddf8ec4", "type": "github" }, "original": { @@ -242,11 +228,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1719506693, - "narHash": "sha256-C8e9S7RzshSdHB7L+v9I51af1gDM5unhJ2xO1ywxNH8=", + "lastModified": 1719690277, + "narHash": "sha256-0xSej1g7eP2kaUF+JQp8jdyNmpmCJKRpO12mKl/36Kc=", "owner": "nixos", "repo": "nixpkgs", - "rev": "b2852eb9365c6de48ffb0dc2c9562591f652242a", + "rev": "2741b4b489b55df32afac57bc4bfd220e8bf617e", "type": "github" }, "original": { @@ -270,11 +256,11 @@ "treefmt-nix": "treefmt-nix" }, "locked": { - "lastModified": 1719695745, - "narHash": "sha256-4i71R8xofnwmlhb84swOLQro63pHvC37NVIipbz1I04=", + "lastModified": 1719860300, + "narHash": "sha256-ZeF+zI+/53HeS567/mXS2Gw+w8k9FsjRC/TzoVQOpi4=", "owner": "nix-community", "repo": "nixvim", - "rev": "c062b976eff9f13597c7c23d77a6b3ac677b7fd5", + "rev": "079c2c479b5707adf0b03f817be30945c92c15cf", "type": "github" }, "original": { @@ -289,35 +275,10 @@ "nixos-modules": "nixos-modules", "nixpkgs": "nixpkgs", "nixvim": "nixvim", - "search": "search_2" + "search": "search" } }, "search": { - "inputs": { - "flake-utils": [ - "nixos-modules", - "flake-utils" - ], - "nixpkgs": [ - "nixos-modules", - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1719609822, - "narHash": "sha256-zc6zlMx/x/LFTAbipkDBsWBE99SX1HDqry1Bxi5Sqqg=", - "owner": "nuschtos", - "repo": "search", - "rev": "4e13cf6dbd5d1f193a852af35f2f684eb538c8b8", - "type": "github" - }, - "original": { - "owner": "nuschtos", - "repo": "search", - "type": "github" - } - }, - "search_2": { "inputs": { "flake-utils": [ "flake-utils" @@ -328,7 +289,7 @@ }, "locked": { "lastModified": 0, - "narHash": "sha256-LncZXiCmV8yd0TPqI/p/kSNsF8FG4BKRfAuiHTWBAoA=", + "narHash": "sha256-luIwvWFa6upGhv1TglMbpI5rdhSXVNMCvoY/XJyoCKY=", "path": "../.", "type": "path" }, @@ -367,21 +328,6 @@ "type": "github" } }, - "systems_3": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" - } - }, "treefmt-nix": { "inputs": { "nixpkgs": [ @@ -390,11 +336,11 @@ ] }, "locked": { - "lastModified": 1719243788, - "narHash": "sha256-9T9mSY35EZSM1KAwb7K9zwQ78qTlLjosZgtUGnw4rn4=", + "lastModified": 1719749022, + "narHash": "sha256-ddPKHcqaKCIFSFc/cvxS14goUhCOAwsM1PbMr0ZtHMg=", "owner": "numtide", "repo": "treefmt-nix", - "rev": "065a23edceff48f948816b795ea8cc6c0dee7cdf", + "rev": "8df5ff62195d4e67e2264df0b7f5e8c9995fd0bd", "type": "github" }, "original": { diff --git a/demo/flake.nix b/demo/flake.nix index 61fabc1..5847365 100644 --- a/demo/flake.nix +++ b/demo/flake.nix @@ -15,6 +15,7 @@ inputs = { nixpkgs.follows = "nixpkgs"; search.follows = "search"; + flake-utils.follows = "flake-utils"; }; }; nixvim = { diff --git a/src/app/app.component.html b/src/app/app.component.html index 103eae2..b0349fe 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -6,11 +6,17 @@

NüschtOS Search

- + + +

Only showing the first 500 results. Make your search term more concise.

+
diff --git a/src/app/app.component.scss b/src/app/app.component.scss index d7656cc..e04472b 100644 --- a/src/app/app.component.scss +++ b/src/app/app.component.scss @@ -26,11 +26,6 @@ li { list-style-type:none; -/* display: block; - - &:not(:first-child) { - border-top: 0.1rem solid $c-secondary-l; - }*/ a { display: block; diff --git a/src/app/app.component.ts b/src/app/app.component.ts index ea916cd..3b5c74e 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,16 +1,16 @@ -import { AfterViewInit, ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; +import { AfterViewInit, ChangeDetectionStrategy, Component } from '@angular/core'; import { FormControl, ReactiveFormsModule } from '@angular/forms'; import { Router, RouterLink, RouterOutlet } from '@angular/router'; -import { SearchService } from './core/search.service'; -import { of, switchMap } from 'rxjs'; -import { AsyncPipe, NgFor } from '@angular/common'; +import { Option, SearchService } from './core/search.service'; +import { switchMap } from 'rxjs'; +import { AsyncPipe, NgFor, NgIf } from '@angular/common'; import { TextFieldComponent } from "@feel/form"; import { OptionComponent } from './pages/option/option.component'; @Component({ selector: 'app-root', standalone: true, - imports: [RouterOutlet, AsyncPipe, NgFor, ReactiveFormsModule, TextFieldComponent, RouterLink, OptionComponent], + imports: [RouterOutlet, AsyncPipe, NgFor, ReactiveFormsModule, TextFieldComponent, RouterLink, OptionComponent, NgIf], templateUrl: './app.component.html', styleUrl: './app.component.scss', changeDetection: ChangeDetectionStrategy.OnPush, @@ -25,7 +25,7 @@ export class AppComponent implements AfterViewInit { if (q && q.length > 0) { return this.searchService.search(q); } else { - return of([]); + return this.searchService.all(); } }), ); @@ -36,10 +36,15 @@ export class AppComponent implements AfterViewInit { ) { } - ngAfterViewInit(): void { + public ngAfterViewInit(): void { this.search.setValue(AppComponent.getQuery()); } + private static getQuery(): string | null { return new URL(location.href).searchParams.get("query"); } + + protected trackBy(_idx: number, { name }: Option): string { + return name; + } } diff --git a/src/app/core/search.service.ts b/src/app/core/search.service.ts index f84ccc0..5e4f50d 100644 --- a/src/app/core/search.service.ts +++ b/src/app/core/search.service.ts @@ -36,12 +36,41 @@ export class SearchService { public search(query: string): Observable { this.update(); - return this.data.pipe(map(options => options.filter(option => { - return option.name.includes(query) - }))); + return this.data.pipe(map(options => { + const result = []; + let i = 0; + for (const option of options) { + if (option.name.includes(query)) { + result.push(option); + i++; + // TODO: pagination + if (i === 500) { + return result; + } + } + } + return result; + })); } + public getByName(name: string): Observable