简体   繁体   English

迭代3800条记录,在IE浏览器中花费太多时间

[英]Iterating 3800 records, taking too much time in IE Browser

I am facing issue with iterating list of records, it's taking too much time to load the data on the screen, due to this performance is getting down. 我在迭代记录列表时遇到问题,由于此性能下降,将数据加载到屏幕上花费了太多时间。

I tried the following scenarios. 我尝试了以下方案。

html HTML

 <select class="append"/>

js JS

var listOfrecords = [{"name":"hello"},{},{}......etc];//assume 3800 records 

 using forEach
 =======================
listOfrecords.forEach(function(index,record){
   //I tried both
  $(".append").append('<option>'+record.name+'</option>');
        or
  $(".append").html('<option>'+record.name+'</option>');
})

 using for loop
=====================
for(var int i=0;i<=listOfrecords.length;i++){
  //I tried both
  $(".append").append('<option>'+listOfrecords[i].name+'</option>');
        or
  $(".append").html('<option>'+listOfrecords[i].name+'</option>');
 }

  using while loop
 ===================
  var i = listOfrecords.length;
  while(i--){
   //I tried both
  $(".append").append('<option>'+listOfrecords[i].name+'</option>');
        or
  $(".append").html('<option>'+listOfrecords[i].name+'</option>');
  }

  using for in
  ============
 for(var i in listOfrecords){
  //I tried both
  $(".append").append('<option>'+listOfrecords[i].name+'</option>');
        or
  $(".append").html('<option>'+listOfrecords[i].name+'</option>');

 } 

The data is displayed on the screen, but it takes too much time. 数据显示在屏幕上,但是需要太多时间。 Can anyone suggest me, is there any way to render the data faster. 谁能建议我,有什么方法可以更快地渲染数据。

Accessing the DOM is expensive - you're essentially telling the browser to traverse the DOM tree with every "append", and then insert another element. 访问DOM的成本很高-实际上,您是在告诉浏览器使用每个“附加”遍历DOM树,然后插入另一个元素。 So each time you access the dom, you're making your bit of code exponentially more expensive to run. 因此,每次访问dom时,您的代码运行成本就成倍增加。 Since you're using a loop and accessing the DOM tree 3800 times, I can see why your browser would complain. 由于您使用循环并访问DOM树3800次,因此我可以看到您的浏览器为何会抱怨。

A better thing to do is to build an HTML string or DocumentFragment as you don't need to access the DOM with every item in your array. 更好的方法是构建HTML字符串或DocumentFragment,因为您不需要访问数组中每个项目的DOM。

var string = '';

for ( var i = 0, i < elements.length, i++ ) {

    string += '<option>' + elements.content + '</option>';

}

$('body').append(string);

You can iterate and add values on a string, and then just call .append once. 您可以迭代并在字符串上添加值,然后仅调用.append一次。

It's much quicker, with an approximately 9-10x speed increase 速度更快,速度提高了大约9-10倍

 var listOfrecords = [{"name":"hello"},{},{}......etc];//assume 3800 records var newOptions = ''; listOfrecords.forEach(function(index,record){ newOptions += '<option>'+record.name+'</option>'; }); $(".append").append(newOptions); //only having to call append insert once 
 <select class="append"/> 

Font: http://www.learningjquery.com/2009/03/43439-reasons-to-use-append-correctly 字体: http//www.learningjquery.com/2009/03/43439-reasons-to-use-append-正确地

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM