• 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 += `<div>这是第${i + 1}个元素</div>`;
            }
            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

  • 相关阅读:
    第三章 操作符
    exit函数
    详解C++ friend关键字
    放假了,暂告一段落,迎接研究生
    使用const 提高函数的健壮性
    使用断言assert
    对return 语句的正确性和效率进行检查
    函数堆栈
    somethings about QSplitter
    引用和引用参数
  • 原文地址:https://www.cnblogs.com/Brose/p/jqueryApiTest.html
Copyright © 2020-2023  润新知