perf(bulk-api-tester): optimize API docs import search to sub-millisecond with precomputed haystacks and rAF DOM toggling

This commit is contained in:
2026-10-26 18:27:17 +05:30
parent 9d0aaec6ec
commit e9cbc3743e
+84 -35
View File
@@ -2165,15 +2165,29 @@ const App = {
return;
}
// Build index of APIs
// Build index of APIs with precomputed search haystack
const allApis = [];
(docsData.sections || []).forEach(s => {
(s.eps || []).forEach(e => {
const haystack = [
e.api_key || '',
e.title || '',
e.summary || '',
e.desc || '',
e.full_url || '',
e.path || '',
s.title || '',
...(e.params || []).map(p => (p.n || '') + ' ' + (p.ty || '')),
...(e.chips || []),
...(e.endpoints || []).map(sub => (sub.title || '') + ' ' + (sub.subpath || '') + ' ' + (sub.path || ''))
].join(' ').toLowerCase();
allApis.push({
...e,
secId: s.id,
secTitle: s.title,
secHue: s.hue
secHue: s.hue,
_searchHaystack: haystack
});
});
});
@@ -2181,7 +2195,7 @@ const App = {
this._docModalSections = docsData.sections || [];
this._renderDocModalCategories();
this._renderDocModalList();
this._buildDocModalCards();
setTimeout(() => {
const inp = document.getElementById('doc-api-search-input');
@@ -2216,58 +2230,82 @@ const App = {
_selectDocModalCategory(catId) {
this._docModalActiveCategory = catId;
this._renderDocModalCategories();
this._renderDocModalList();
this._applyDocModalFilter();
},
_filterDocModal(query) {
this._docModalSearch = (query || '').toLowerCase().trim();
const clearBtn = document.getElementById('doc-api-search-clear');
if (clearBtn) clearBtn.style.display = this._docModalSearch ? 'block' : 'none';
this._renderDocModalList();
if (this._filterDocRaf) cancelAnimationFrame(this._filterDocRaf);
this._filterDocRaf = requestAnimationFrame(() => {
this._applyDocModalFilter();
});
},
_renderDocModalList() {
const listEl = document.getElementById('doc-modal-list');
this._applyDocModalFilter();
},
_applyDocModalFilter() {
const countEl = document.getElementById('doc-modal-count');
if (!listEl || !this._docModalApis) return;
const emptyEl = document.getElementById('doc-modal-empty');
const emptyQueryEl = document.getElementById('doc-modal-empty-query');
if (!this._docModalApis) return;
const q = this._docModalSearch || '';
const cat = this._docModalActiveCategory || 'all';
const tokens = q ? q.split(/\s+/).filter(Boolean) : [];
const filtered = this._docModalApis.filter(api => {
if (cat !== 'all' && api.secId !== cat) return false;
if (!q) return true;
let visibleCount = 0;
const total = this._docModalApis.length;
const cards = this._docModalCardElements || (document.getElementById('doc-modal-list') ? document.getElementById('doc-modal-list').querySelectorAll('.doc-import-card') : []);
const haystack = [
api.api_key || '',
api.title || '',
api.summary || '',
api.desc || '',
api.full_url || '',
api.path || '',
...(api.params || []).map(p => p.n || ''),
...(api.chips || []),
...(api.endpoints || []).map(sub => (sub.title || '') + ' ' + (sub.subpath || '') + ' ' + (sub.path || ''))
].join(' ').toLowerCase();
for (let i = 0; i < total; i++) {
const api = this._docModalApis[i];
const cardEl = cards[i];
if (!cardEl) continue;
return haystack.includes(q);
});
let matches = true;
if (cat !== 'all' && api.secId !== cat) {
matches = false;
} else if (tokens.length > 0) {
const hay = api._searchHaystack;
for (let j = 0; j < tokens.length; j++) {
if (!hay.includes(tokens[j])) {
matches = false;
break;
}
}
}
if (matches) {
if (cardEl.style.display === 'none') cardEl.style.display = '';
visibleCount++;
} else {
if (cardEl.style.display !== 'none') cardEl.style.display = 'none';
}
}
if (countEl) {
countEl.textContent = `Showing ${filtered.length} of ${this._docModalApis.length} APIs`;
countEl.textContent = `Showing ${visibleCount} of ${total} APIs`;
}
if (filtered.length === 0) {
listEl.innerHTML = `
<div style="text-align:center; padding:50px 20px; color:var(--text-3);">
<i class="fa-solid fa-magnifying-glass" style="font-size:24px; opacity:0.3; margin-bottom:10px; display:block;"></i>
No APIs found matching "<b>${this.escapeHTML(q)}</b>"
</div>
`;
return;
if (emptyEl) {
if (visibleCount === 0) {
if (emptyQueryEl) emptyQueryEl.textContent = q;
emptyEl.style.display = 'block';
} else {
emptyEl.style.display = 'none';
}
}
},
listEl.innerHTML = filtered.map(api => {
_buildDocModalCards() {
const listEl = document.getElementById('doc-modal-list');
if (!listEl || !this._docModalApis) return;
let html = this._docModalApis.map((api, idx) => {
const subEps = (api.endpoints && api.endpoints.length) ? api.endpoints : [api];
const hasMultiple = subEps.length > 1;
const targetUrl = api.full_url || ('https://apigp0006.creinnovations.in' + (api.path || ''));
@@ -2275,7 +2313,7 @@ const App = {
const chips = api.chips || [];
return `
<div class="doc-import-card" style="flex-shrink:0; min-height:fit-content;" onclick="App.importFromDoc('${api.id}', 0)">
<div class="doc-import-card" data-idx="${idx}" data-api-id="${api.id}" data-sec-id="${api.secId}" style="flex-shrink:0; min-height:fit-content;" onclick="App.importFromDoc('${api.id}', 0)">
<div style="display:flex; align-items:center; justify-content:space-between; gap:10px;">
<div style="display:flex; align-items:center; gap:8px; flex:1; min-width:0;">
<span class="method-badge m-${api.method === 'MULTI' ? 'POST' : api.method}" style="flex-shrink:0;">${api.method}</span>
@@ -2350,6 +2388,17 @@ const App = {
</div>
`;
}).join('');
html += `
<div id="doc-modal-empty" style="display:none; text-align:center; padding:50px 20px; color:var(--text-3); flex-shrink:0;">
<i class="fa-solid fa-magnifying-glass" style="font-size:24px; opacity:0.3; margin-bottom:10px; display:block;"></i>
No APIs found matching "<b id="doc-modal-empty-query"></b>"
</div>
`;
listEl.innerHTML = html;
this._docModalCardElements = listEl.querySelectorAll('.doc-import-card');
this._applyDocModalFilter();
},
async importFromDoc(apiId, subEpIdx = 0, targetColId = null, customColName = null, importAll = false) {