Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

常用插入DOM节点方法性能分析 #118

Open
LiuYueKai opened this issue Aug 21, 2016 · 0 comments
Open

常用插入DOM节点方法性能分析 #118

LiuYueKai opened this issue Aug 21, 2016 · 0 comments

Comments

@LiuYueKai
Copy link
Contributor

测试内容

将html拼成字符串之后,添加到对应dom节点时通常可以使用如下方法:

  • innerHTML
  • insertAdjacentHTML
  • append
  • html

测试方法

通过如下示例代码比较如下4种方法在插入节点时的性能:

    var data1 = new Date();
    for (var i = 0; i < len; i++) {
        $('#parentid')[0].innerHTML = h;
    }
    var data2 = new Date();
    for (var i = 0; i < len; i++) {
        $('#parentid').append(h);
    }
    var data3 = new Date();
    for (var i = 0; i < len; i++) {
        $('#parentid')[0].insertAdjacentHTML('beforeend', h);
    }
    var data4 = new Date();
    for (var i = 0; i < len; i++) {
        $('#parentid').html(h);
    }
    var data5 = new Date();


    console.log(data2 - data1 + ':innerHTML');
    console.log(data3 - data2 + ':append');
    console.log(data4 - data3 + ':insertAdjacentHTML');
    console.log(data5 - data4 + ':html');

测试结果

在IE8下的测试结果如下:

4922:html
3078:append
1718:innerHTML
1152:insertAdjacentHTML

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant