You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求用JS/jQuery高效搜索指定JSON文件的技术方案

Fastest Way to Search CoinMarketCap's Quick Search JSON with JavaScript

Let's cut to the chase—native JavaScript is your fastest bet here (no jQuery overhead), and we can optimize the search logic to keep things snappy. Here's a tailored solution for your needs:

Step 1: Fetch Data Efficiently

We’ll use the browser’s built-in fetch API (it’s lightweight and faster than jQuery’s $.getJSON since it skips the library’s extra bloat). We’ll also cache the fetched data to avoid re-downloading it for repeated searches.

Step 2: Optimized Search Logic

Our search function will:

  • Do case-insensitive matching for both name and symbol fields
  • Iterate through the data only once (minimizing loop overhead)
  • Stop early as soon as we find 5 matches (no need to process the entire dataset unnecessarily)

Full Working Code

// Cache the fetched JSON to avoid repeated network calls
let cachedCoinData = null;

// Load the coin data once (call this first, or wrap in an async workflow)
async function loadCoinData() {
  try {
    const response = await fetch('https://s2.coinmarketcap.com/generated/search/quick_search.json');
    cachedCoinData = await response.json();
    console.log('Coin data loaded successfully!');
  } catch (error) {
    console.error('Failed to load coin data:', error);
  }
}

// Search function: takes your custom query, returns top 5 matches
function searchCoins(query) {
  if (!cachedCoinData || !query.trim()) return [];
  
  const lowerQuery = query.toLowerCase();
  const matches = [];
  
  // Loop once, stop early when we have 5 results
  for (const coin of cachedCoinData) {
    if (matches.length >= 5) break;
    
    const coinName = coin.name.toLowerCase();
    const coinSymbol = coin.symbol.toLowerCase();
    
    if (coinName.includes(lowerQuery) || coinSymbol.includes(lowerQuery)) {
      matches.push(coin);
    }
  }
  
  return matches;
}

// Example usage: load data, then search and log results
async function runSearch(searchQuery) {
  await loadCoinData();
  const results = searchCoins(searchQuery);
  console.log(`Top 5 matches for "${searchQuery}":`, results);
}

// Replace 'bit' with your custom search string
runSearch('bit');

Key Speed Optimizations

  • Caching: Stores the JSON after the first fetch so subsequent searches skip network requests entirely.
  • Early Termination: The loop stops as soon as we hit 5 matches, avoiding unnecessary iterations.
  • Precomputed Lowercase Values: Converts the query, name, and symbol to lowercase once per item, reducing redundant processing.
  • Native API: fetch eliminates the overhead of jQuery’s abstraction layer.

If you need to use jQuery for legacy reasons, here’s a streamlined version—note it will be slightly slower due to library overhead:

let cachedCoinData = null;

// Load data with jQuery
function loadCoinData() {
  return $.getJSON('https://s2.coinmarketcap.com/generated/search/quick_search.json')
    .done(data => {
      cachedCoinData = data;
      console.log('Coin data loaded successfully!');
    })
    .fail(error => {
      console.error('Failed to load coin data:', error);
    });
}

// Search function
function searchCoins(query) {
  if (!cachedCoinData || !query.trim()) return [];
  
  const lowerQuery = query.toLowerCase();
  return cachedCoinData.filter(coin => {
    return coin.name.toLowerCase().includes(lowerQuery) || coin.symbol.toLowerCase().includes(lowerQuery);
  }).slice(0, 5);
}

// Example usage
loadCoinData().then(() => {
  const results = searchCoins('eth');
  console.log(`Top 5 matches for "eth":`, results);
});

Just run the runSearch (or jQuery equivalent) with your custom search string, and you’ll get the top 5 matches logged to your browser console instantly.

内容的提问来源于stack exchange,提问作者R Records

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:02:23