Account for optional cache and remove timing code

f4ef9697e983a0bb0b3b0ab6388f5ecbfe37898e

Joe <joenunezb@gmail.com>

2 files changed, +21 -26Showing whitespace changes
public/script.js+0 -1
@@ -1750,7 +1750,6 @@ export async function getCharacters() {
17501750 }
17511751
17521752 await getGroups();
1753- // clearFuzzySearchCaches();
17541753 await printCharacters(true);
17551754 }
17561755}
public/scripts/power-user.js+21 -25
@@ -1832,26 +1832,22 @@ async function loadContextSettings() {
18321832
18331833
18341834/**
18351835 * Common function to perform fuzzy search with optional caching
18361836 * @param {string} type - Type of search from fuzzySearchCategories
18371837 * @param {any[]} data - Data array to search in
18381838 * @param {Array<{name: string, weight: number, getFn?: Function}>} keys - Fuse.js keys configuration
18391839 * @param {string} searchValue - The search term
18401840 * @param {Object.<string, { resultMap: Map<string, any> }>} [fuzzySearchCaches=null] - FuzzyOptional fuzzy search caches
18411841 * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
18421842 */
18431843function performFuzzySearch(type, data, keys, searchValue, fuzzySearchCaches = null) {
1844- let startTime = performance.now();
1844+ // Check cache if provided
1845+ if (fuzzySearchCaches) {
18451846 const cache = fuzzySearchCaches[type];
1846-
1847+ if (cache?.resultMap.has(searchValue)) {
1847- // Check cache for existing results
1848- if (cache.resultMap.has(searchValue)) {
1849- let endTime = performance.now();
1850- if (endTime - startTime > 1.0) {
1851- console.log(`Fuzzy search for ${type} took ${endTime - startTime}ms (cached)`);
1852- }
18531848 return cache.resultMap.get(searchValue);
18541849 }
1850+ }
18551851
18561852 // @ts-ignore
18571853 const fuse = new Fuse(data, {
@@ -1863,10 +1859,10 @@ function performFuzzySearch(type, data, keys, searchValue, fuzzySearchCaches) {
18631859 });
18641860
18651861 const results = fuse.search(searchValue);
1866- cache.resultMap.set(searchValue, results);
1862+
1867- let endTime = performance.now();
1863+ // Store in cache if provided
18681864 if (endTime - startTime > 1.0fuzzySearchCaches) {
1869- console.log(`Fuzzy search for ${type} took ${endTime - startTime}ms`);
1865+ fuzzySearchCaches[type].resultMap.set(searchValue, results);
18701866 }
18711867 return results;
18721868}
@@ -1874,10 +1870,10 @@ function performFuzzySearch(type, data, keys, searchValue, fuzzySearchCaches) {
18741870/**
18751871 * Fuzzy search characters by a search term
18761872 * @param {string} searchValue - The search term
18771873 * @param {Object.<string, { resultMap: Map<string, any> }>} [fuzzySearchCaches=null] - FuzzyOptional fuzzy search caches
18781874 * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
18791875 */
18801876export function fuzzySearchCharacters(searchValue, fuzzySearchCaches = null) {
18811877 const keys = [
18821878 { name: 'data.name', weight: 20 },
18831879 { name: '#tags', weight: 10, getFn: (character) => getTagsList(character.avatar).map(x => x.name).join('||') },
@@ -1899,10 +1895,10 @@ export function fuzzySearchCharacters(searchValue, fuzzySearchCaches) {
18991895 * Fuzzy search world info entries by a search term
19001896 * @param {*[]} data - WI items data array
19011897 * @param {string} searchValue - The search term
19021898 * @param {Object.<string, { resultMap: Map<string, any> }>} [fuzzySearchCaches=null] - FuzzyOptional fuzzy search caches
19031899 * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
19041900 */
19051901export function fuzzySearchWorldInfo(data, searchValue, fuzzySearchCaches = null) {
19061902 const keys = [
19071903 { name: 'key', weight: 20 },
19081904 { name: 'group', weight: 15 },
@@ -1920,10 +1916,10 @@ export function fuzzySearchWorldInfo(data, searchValue, fuzzySearchCaches) {
19201916 * Fuzzy search persona entries by a search term
19211917 * @param {*[]} data - persona data array
19221918 * @param {string} searchValue - The search term
19231919 * @param {Object.<string, { resultMap: Map<string, any> }>} [fuzzySearchCaches=null] - FuzzyOptional fuzzy search caches
19241920 * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
19251921 */
19261922export function fuzzySearchPersonas(data, searchValue, fuzzySearchCaches = null) {
19271923 const mappedData = data.map(x => ({
19281924 key: x,
19291925 name: power_user.personas[x] ?? '',
@@ -1941,10 +1937,10 @@ export function fuzzySearchPersonas(data, searchValue, fuzzySearchCaches) {
19411937/**
19421938 * Fuzzy search tags by a search term
19431939 * @param {string} searchValue - The search term
19441940 * @param {Object.<string, { resultMap: Map<string, any> }>} [fuzzySearchCaches=null] - FuzzyOptional fuzzy search caches
19451941 * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
19461942 */
19471943export function fuzzySearchTags(searchValue, fuzzySearchCaches = null) {
19481944 const keys = [
19491945 { name: 'name', weight: 1 },
19501946 ];
@@ -1955,10 +1951,10 @@ export function fuzzySearchTags(searchValue, fuzzySearchCaches) {
19551951/**
19561952 * Fuzzy search groups by a search term
19571953 * @param {string} searchValue - The search term
19581954 * @param {Object.<string, { resultMap: Map<string, any> }>} [fuzzySearchCaches=null] - FuzzyOptional fuzzy search caches
19591955 * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
19601956 */
19611957export function fuzzySearchGroups(searchValue, fuzzySearchCaches = null) {
19621958 const keys = [
19631959 { name: 'name', weight: 20 },
19641960 { name: 'members', weight: 15 },