Skip to content

Commit 3501115

Browse files
committed
Remove "LRU" inline comment in favour of markdown implementation note
== npm run size == * Before: 2231 * After: 2224 (-7B, -0.3%)
1 parent f846432 commit 3501115

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

CONTRIBUTING.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
## Implementation notes
44

5-
* 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).
5+
* The `cache` Map lets us instantly display any previous result in the same browser tab. We follow the [LRU strategy](https://en.wikipedia.org/wiki/Least_recently_used).
6+
7+
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. It also saves client bandwidth and server load.) Or, if you think another word might yield better results and replace it with `banana`, and return to `apple` because that had a better one, we respond instantly.
8+
9+
We keep up to 100 past results in memory. After that, we prioritize keeping the most recently shown data, and delete 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 do return to the very first after a hundred, you're likely to pass by more recent ones on the way there. All queries 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.
10+
11+
When it is time to delete an old result, we take a key from the "top" of the map, which is either the oldest and never used, or the least recently used.
12+
13+
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.
14+
15+
* 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).
616

717
This is done for two reasons:
818

typesense-minibar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ globalThis.tsminibar = function tsminibar (form, dataset = form.dataset) {
117117
let hits = cache.get(query);
118118
if (hits) {
119119
cache.delete(query);
120-
cache.set(query, hits); // LRU
120+
cache.set(query, hits);
121121
return hits;
122122
}
123123
searchParams.set('q', query);

0 commit comments

Comments
 (0)