javascript - 加快 UL LI 列表中超过 5000 个对象的 AJAX 搜索速度

标签 javascript jquery ajax search

堆栈社区,

我使用 .addClass 和 .removeClass 进行简单的 Ajax js 搜索,因为我发现它比 .show() 和 .hide() 更快。

问题是,它仍然不够快,我完全迷路了。

搜索可找到here

实际代码如下所示:Edit fiddle - JSFiddle - Code Playground

$(document).ready(function() {

  // Schovat pocet pri nacteni
  $("#vyhledavani_kompatibilit_pocet").hide();

  $("#vyhledavani_kompatibilit_input").keyup(function() {

    // Zobrazit pocet po napsani nejakeho vysledku
    $("#vyhledavani_kompatibilit_pocet").show();
    // Retrieve the input field text and reset the count to zero
    var filter = $(this).val(),
      count = 0;

    var searchKeyword = $(this).val();
    if (searchKeyword.length >= 3) {

      // Loop through the comment list 
      $(".komp").each(function() {

        // If the list item does not contain the text phrase fade it out
        if ($(this).text().search(new RegExp(filter, "i")) < 0) {
          $(this).addClass("schovatDiv");
          if ($(this).siblings(':hidden')) {
            $(this).parent().prev().addClass("schovatDiv");
          }


          // Show the list item if the phrase matches and increase the count by 1
        } else {
          $(this).parent().prev().removeClass("schovatDiv");
          $(this).removeClass("schovatDiv");
          count++;
        }
      });
    } else {
      $("#vyhledavani_kompatibilit_pocet").hide();
      $(".komp").parent().prev().removeClass("schovatDiv");
      $(".komp").removeClass("schovatDiv");

    };

    // Update the count
    var numberItems = count;

    {
      $("#vyhledavani_kompatibilit_pocet").text(count);
    }

  });
});

有什么方法可以加快搜索速度,这样当您在搜索字段中输入“东芝”时,过滤器就不会花费 8 秒的时间?

感谢您的任何提前或提示。 :)

最佳答案

您可以通过不渲染所有内容而仅渲染可见部分来提高性能。 然后它就可以非常快地工作。

关于javascript - 加快 UL LI 列表中超过 5000 个对象的 AJAX 搜索速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44005465/

相关文章:

javascript - CSS 和 Javascript 在 Tomcat 8 或 9 上加载时间过长

javascript - 如何启用跨表格单元格和行的 div 拖动和调整大小

javascript - ngRoute 和 Underscore.js 的 RequireJS shim 配置

javascript - 使用 Ajax 提交表单

php - 访问脚本 1、脚本 2 中设置的 session 变量。 PHP

javascript - 网页在 Firefox 上运行良好,但在 IE 上运行不佳

php - 使用 session 变量时,Ajax 请求后数百个 Apache 进程挂起

javascript - 是否有可能创建一个更动态的新类型?

javascript - key 在 javascript 代码(JavaScript、Firebase)中可见吗?

javascript - 如何组合两个在Javascript中具有相同值的数组