Skip to content

Commit

Permalink
Remove "LRU" inline comment in favour of markdown implementation note
Browse files Browse the repository at this point in the history
== npm run size ==

* Before:  2231
* After:   2224 (-7B, -0.3%)
  • Loading branch information
Krinkle committed Aug 3, 2024
1 parent f846432 commit a254b7a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

## Implementation notes

* The styles for `typesense-minibar` as web component, and `.tsmb-form` class name are kept independent (that is, the web component does not auto-add the class name).
* The `cache` Map follows the [LRU strategy](https://en.wikipedia.org/wiki/Least_recently_used) and allows the widge to instantly display any result you've already seen previously in the same browser tab.

For example, if you type `a`, `app`, `apple`, `apples`, and backspace to a shorter previous query, we instantly show those previous results (no time wasted waiting for re-download of the same results, also saving client bandwidth and server load). Similarly, if you clear the input field because you think another word might yield better results, and tried `banana`, but then type `apple` again because that returned a better result, we similarly display those results instantly.

We keep up to 100 past results in memory. After that, we prioritize keeping data that was most recently added or re-used, and start deleting older unused results. We assume that you're most likely to return to what you've seen most recently. (Maybe not within the last 10, but within 100. Even if you di return to the very first after a hundred, you're likely to pass by more recent ones on the way there. We consider all queries to have equal cost.) When we store a new result, or when we re-use an old result, we delete it and re-set it, so that it goes to the "bottom" of the map. Then, when it is time to delete an old result, we delete it from the "top" of the map, which is either the oldest and never used, or the least recently used.

If we only add new results and reuse results as-is ([FIFO strategy](https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics))), that may delete very recently used data.

* The styles for `typesense-minibar` as web component, and `.tsmb-form` class name are kept independent (that is, the web component does not auto-add the class name, nor does it otherwise rely on styles for the class name, and vice versa).

This is done for two reasons:

Expand Down
2 changes: 1 addition & 1 deletion typesense-minibar.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ globalThis.tsminibar = function tsminibar (form, dataset = form.dataset) {
let hits = cache.get(query);
if (hits) {
cache.delete(query);
cache.set(query, hits); // LRU
cache.set(query, hits);
return hits;
}
searchParams.set('q', query);
Expand Down

0 comments on commit a254b7a

Please sign in to comment.