Make images smaller using best-in-class codecs, right in the browser.
JavaScript
Result cache LRU (#165)
Jake Archibaldc591f1f37dd0
diff --git a/src/components/App/result-cache.ts b/src/components/App/result-cache.tsindex 7fa42aaf..c94c35ba 100644--- a/src/components/App/result-cache.ts+++ b/src/components/App/result-cache.ts@@ -21,12 +21,13 @@ const SIZE = 5;export default class ResultCache {private readonly _entries: CacheEntry[] = [];- private _nextIndex: number = 0;add(entry: CacheEntry) {if (entry.encoderState.type === identity.type) throw Error('Cannot cache identity encodes');- this._entries[this._nextIndex] = entry;- this._nextIndex = (this._nextIndex + 1) % SIZE;+ // Add the new entry to the start+ this._entries.unshift(entry);+ // Remove the last entry if we're now bigger than SIZE+ if (this._entries.length > SIZE) this._entries.pop();}match(@@ -34,7 +35,7 @@ export default class ResultCache {preprocessorState: PreprocessorState,encoderState: EncoderState,): CacheResult | undefined {- const matchingEntry = this._entries.find((entry) => {+ const matchingIndex = this._entries.findIndex((entry) => {// Check for quick exits:if (entry.source !== source) return false;if (entry.encoderState.type !== encoderState.type) return false;@@ -55,14 +56,20 @@ export default class ResultCache {return true;});- if (matchingEntry) {- return {- bmp: matchingEntry.bmp,- preprocessed: matchingEntry.preprocessed,- file: matchingEntry.file,- };+ if (matchingIndex === -1) return undefined;++ const matchingEntry = this._entries[matchingIndex];++ if (matchingIndex !== 0) {+ // Move the matched result to 1st position (LRU)+ this._entries.splice(matchingIndex, 1);+ this._entries.unshift(matchingEntry);}- return undefined;+ return {+ bmp: matchingEntry.bmp,+ preprocessed: matchingEntry.preprocessed,+ file: matchingEntry.file,+ };}}