Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Problema de rendimiento de Javascript con big data

Estoy filtrando una lista con javascript. Mi código funciona sin problemas, pero cuando la lista constante supera los 100, el navegador se congela.

 <div id="domList" class="flex flex-col p-8 space-y-4 bg-white shadow rounded-xl"> <?php foreach ($clients as $client): ?> <a href="<?=base_url();?>/clients/<?=$client->id;?>" class="flex justify-between p-4 bg-gray-100 rounded item hover:bg-gray-200"> <div class="flex items-center"><span class="mr-2 text-xs text-gray-400"><?=$client->customernumber;?></span> <?=$client->fn;?> <?=$client->ln;?></div> <div class="ml-2 py-1 px-2 text-xs flex-shrink-0 flex justify-center items-center text-white <?=$client->status_bg;?> rounded"><?=$client->status;?></div> </a> <?php endforeach;?> </div> <script type="text/javascript"> const list = <?=json_encode($clients);?>; const filterEventHandler = (event) => { const filterVal = event.target.value; const domList = document.getElementById('domList'); domList.innerHTML = ''; const newList = list.filter((e) => e.customernumber.toLowerCase().includes(filterVal.toLowerCase()) || (e.fn + " " +e.ln).toLowerCase().includes(filterVal.toLowerCase()) ); newList.forEach(user => domList.innerHTML += buildItem(user)); } const buildItem = (user) => { const item = ` <a href="<?=base_url();?>/clients/${user.id}" class="flex justify-between p-4 bg-gray-100 rounded item hover:bg-gray-200"> <div class="flex items-center"><span class="mr-2 text-xs text-gray-400">${user.customernumber}</span> ${user.fn} ${user.ln}</div> <div class="ml-2 py-1 px-2 text-xs flex-shrink-0 flex justify-center items-center text-white ${user.status_bg} rounded">${user.status}</div> </a>`; return item; } </script>

¿Qué estoy haciendo mal y cómo puedo hacer que este código funcione para una lista de muchos más valores (> 50.000)?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A ver si esto ayuda para empezar:

  • Mueva filterVal en minúsculas fuera del bucle .filter . No hay necesidad de hacer eso para cada elemento de la list .
  • Solo escriba innerHTML una vez (en lugar de borrarlo y luego agregarlo).
 const filterEventHandler = (event) => { const filterVal = event.target.value.toLowerCase(); const htmlFragments = list .filter( (e) => e.customernumber .toLowerCase() .includes(filterVal) || `${e.fn} ${e.ln}`.toLowerCase().includes(filterVal), ) .map(buildItem); const domList = document.getElementById("domList"); domList.innerHTML = htmlFragments.join(""); };

En el futuro, es posible que desee pensar en un marco como React (o algo más liviano como Svelte o Mithril) en lugar de construir su HTML a mano.

Esto es especialmente cierto ya que un cliente llamado <script>alert("hello")</script> actualmente causará estragos en su sitio. :-)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!