Dynamic creation of large html table in javascript performance

前端 未结 7 1338
轻奢々
轻奢々 2020-12-02 23:30

I have an application which is used for data analysis and I\'m having a few performance issues with the creation of the table. The data is extracted from documents and it is

7条回答
  •  伪装坚强ぢ
    2020-12-03 00:32

    joinHi,

    The rendering is a problem, but there is also a problem with concatenating so many strings inside the loop, especially once the string gets very large. It would probably be best to put the strings into individual elements of an array then finally use "join" to create the huge string in one fell swoop. e.g.

    var r = new Array();
    var j = -1, recordId;
    r[++j] =  ''; 
    for (var i in data){
        var d = data[i];
        recordId = d.id;
        r[++j] = '';
    }
    r[++j] = '
    IDStatusNameActionsOrigin
    '; r[++j] = recordId; r[++j] = ''; r[++j] = d.status; r[++j] = ''; r[++j] = d.name; r[++j] = ''; r[++j] = d.origin; r[++j] = '
    '; $('#documentRows').html(r.join(''));

    Also, I would use the array indexing method shown here, rather than using "push" since, for all browsers except Google Chrome it is faster, according to this article.

提交回复
热议问题