mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-11-17 14:28:25 +00:00
perf(site): cache related icon names and reuse search indexes (#5123)
* perf(docs): cache name parts when generating related icons * perf(site): reuse icon search indexes between queries --------- Co-authored-by: Eric Fennis <eric.fennis@gmail.com>
This commit is contained in:
co-authored by
Eric Fennis
parent
dbd4b9e5d3
commit
86d3238758
@@ -29,6 +29,9 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
- name: Test search composable
|
||||
run: pnpm --filter @lucide/docs exec node --test .vitepress/theme/composables/useSearch.test.mts
|
||||
|
||||
- name: Build docs
|
||||
run: pnpm --filter @lucide/docs build
|
||||
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { test } from 'node:test';
|
||||
import { computed, ref, shallowRef } from 'vue';
|
||||
import useSearch from './useSearch.ts';
|
||||
|
||||
test('reuses the search index across query changes and clearing the query', () => {
|
||||
const collection = shallowRef([{ name: 'alpha' }, { name: 'beta' }]);
|
||||
const query = ref('=alpha');
|
||||
let reads = 0;
|
||||
const results = useSearch(query, collection, [
|
||||
{
|
||||
name: 'name',
|
||||
getFn: (item) => {
|
||||
reads++;
|
||||
return item.name;
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
assert.deepEqual(results.value, [collection.value[0]]);
|
||||
const initialReads = reads;
|
||||
assert.ok(initialReads > 0);
|
||||
|
||||
query.value = '=beta';
|
||||
assert.deepEqual(results.value, [collection.value[1]]);
|
||||
query.value = '=missing';
|
||||
assert.deepEqual(results.value, []);
|
||||
query.value = '';
|
||||
assert.equal(results.value, collection.value);
|
||||
assert.equal(reads, initialReads);
|
||||
|
||||
collection.value = [{ name: 'gamma' }];
|
||||
assert.equal(results.value, collection.value);
|
||||
query.value = '=gamma';
|
||||
assert.deepEqual(results.value, collection.value);
|
||||
assert.ok(reads > initialReads);
|
||||
});
|
||||
|
||||
test('updates an active query when metadata arrives or the icon collection changes', () => {
|
||||
const icons = shallowRef([{ name: 'alpha' }]);
|
||||
const tags = ref<Record<string, string[]>>({});
|
||||
const collection = computed(() =>
|
||||
icons.value.map((icon) => ({ ...icon, tags: tags.value[icon.name] ?? [] })),
|
||||
);
|
||||
const query = ref('=newtag');
|
||||
const results = useSearch(query, collection, [{ name: 'name' }, { name: 'tags' }]);
|
||||
|
||||
assert.deepEqual(results.value, []);
|
||||
tags.value = { alpha: ['newtag'] };
|
||||
assert.deepEqual(results.value, collection.value);
|
||||
icons.value = [{ name: 'beta' }];
|
||||
assert.deepEqual(results.value, []);
|
||||
tags.value = { beta: ['newtag'] };
|
||||
assert.deepEqual(results.value, collection.value);
|
||||
});
|
||||
|
||||
test('updates the index after reactive item and array mutations', () => {
|
||||
const collection = ref([
|
||||
{ name: 'alpha', aliases: [] as string[], tags: [] as string[], categories: [] as string[] },
|
||||
]);
|
||||
const query = ref('=renamed');
|
||||
const results = useSearch(query, collection, [
|
||||
{ name: 'name' },
|
||||
{ name: 'aliases' },
|
||||
{ name: 'tags' },
|
||||
{ name: 'categories' },
|
||||
]);
|
||||
|
||||
assert.deepEqual(results.value, []);
|
||||
collection.value[0].name = 'renamed';
|
||||
assert.deepEqual(results.value, collection.value);
|
||||
query.value = '=alpha';
|
||||
assert.deepEqual(results.value, []);
|
||||
|
||||
for (const field of ['aliases', 'tags', 'categories'] as const) {
|
||||
query.value = `=new${field}`;
|
||||
assert.deepEqual(results.value, []);
|
||||
collection.value[0][field].push(`new${field}`);
|
||||
assert.deepEqual(results.value, collection.value);
|
||||
}
|
||||
|
||||
query.value = '=beta';
|
||||
assert.deepEqual(results.value, []);
|
||||
collection.value.push({ name: 'beta', aliases: [], tags: [], categories: [] });
|
||||
assert.deepEqual(results.value, [collection.value[1]]);
|
||||
});
|
||||
|
||||
test('keeps separate search instances independent', () => {
|
||||
const first = shallowRef([{ name: 'alpha' }]);
|
||||
const second = shallowRef([{ name: 'beta' }]);
|
||||
const firstQuery = ref('=alpha');
|
||||
const secondQuery = ref('=alpha');
|
||||
const firstResults = useSearch(firstQuery, first, [{ name: 'name' }]);
|
||||
const secondResults = useSearch(secondQuery, second, [{ name: 'name' }]);
|
||||
|
||||
assert.deepEqual(firstResults.value, first.value);
|
||||
assert.deepEqual(secondResults.value, []);
|
||||
first.value = [{ name: 'gamma' }];
|
||||
firstQuery.value = '=gamma';
|
||||
assert.deepEqual(firstResults.value, first.value);
|
||||
assert.deepEqual(secondResults.value, []);
|
||||
secondQuery.value = '=beta';
|
||||
assert.deepEqual(secondResults.value, second.value);
|
||||
assert.deepEqual(firstResults.value, first.value);
|
||||
});
|
||||
@@ -1,22 +1,21 @@
|
||||
import Fuse, { type FuseOptionKeyObject } from 'fuse.js';
|
||||
import { shallowRef, computed, Ref } from 'vue';
|
||||
import { computed, type Ref } from 'vue';
|
||||
|
||||
const useSearch = <T>(
|
||||
query: Ref<string>,
|
||||
collection: Ref<T[]>,
|
||||
keys: FuseOptionKeyObject<T>[] = [],
|
||||
) => {
|
||||
const index = shallowRef(
|
||||
new Fuse(collection.value, {
|
||||
threshold: 0.2,
|
||||
useExtendedSearch: true,
|
||||
keys,
|
||||
}),
|
||||
const index = computed(
|
||||
() =>
|
||||
new Fuse(collection.value, {
|
||||
threshold: 0.2,
|
||||
useExtendedSearch: true,
|
||||
keys,
|
||||
}),
|
||||
);
|
||||
|
||||
const results = computed(() => {
|
||||
index.value.setCollection(collection.value);
|
||||
|
||||
if (query.value) {
|
||||
return index.value
|
||||
.search({ $and: query.value.split(' ').filter((t) => !!t) })
|
||||
|
||||
@@ -28,7 +28,7 @@ const nameParts = (icon) =>
|
||||
|
||||
const getRelatedIcons = (currentIcon, icons) => {
|
||||
const iconSimilarity = (item) =>
|
||||
nameWeight * arrayMatches(nameParts(item), nameParts(currentIcon)) +
|
||||
nameWeight * arrayMatches(iconNameParts.get(item), iconNameParts.get(currentIcon)) +
|
||||
categoryWeight * arrayMatches(item.categories ?? [], currentIcon.categories ?? []) +
|
||||
tagWeight * arrayMatches(item.tags ?? [], currentIcon.tags ?? []);
|
||||
|
||||
@@ -54,6 +54,7 @@ const iconsMetaDataPromises = svgFiles.map(async (iconName) => {
|
||||
});
|
||||
|
||||
const iconsMetaData = await Promise.all(iconsMetaDataPromises);
|
||||
const iconNameParts = new Map(iconsMetaData.map((icon) => [icon, nameParts(icon)]));
|
||||
|
||||
const relatedIcons = iconsMetaData.map((icon) => {
|
||||
const iconRelatedIcons = getRelatedIcons(icon, iconsMetaData);
|
||||
|
||||
Reference in New Issue
Block a user