jquery 操作dom效率测试------html和append插入文档

 $(function () {
        var htmlResult = createHtmlContent(100);
        console.log(htmlResult)
        insertHtml.call($("#left"), "html()耗时:", htmlResult);
        insertHtml.call($("#right"), "append()耗时:", htmlResult);
    });

    function createHtmlContent(count) {
        var htmlContent = "";
        for (var i = 0; i < count; i++) {
            htmlContent += `
这是第${i + 1}个元素
`; } return htmlContent; } function insertHtml(type, htmlContent) { console.time(type); if (type === "html()耗时:") { this.html(htmlContent); } else if (type === "append()耗时:") { this.empty().append(htmlContent); } console.timeEnd(type); }

100条数据集合测试结果:

html()耗时:: 0.69921875ms
jqueryApiTest.html:46 append()耗时:: 1.571044921875ms

1000条数据集合测试结果:

html()耗时:: 3.281982421875ms
jqueryApiTest.html:46 append()耗时:: 9.39111328125ms

10000条数据集合测试结果

html()耗时:: 26.7099609375ms
jqueryApiTest.html:46 append()耗时:: 51.161865234375ms

 

转载于:https://www.cnblogs.com/Brose/p/jqueryApiTest.html

你可能感兴趣的:(jquery 操作dom效率测试------html和append插入文档)