各种动态渲染Element方式 的性能探究

简介:

一、性能优化的原则及方法论

树立原则:动态渲染进入一个Dom元素,首先需要保证动态渲染操作必须尽可能少对原有dom树的影响,影响重绘及重排。

确定方法论:必须寻找一个容器来缓存渲染期间生成的dom结构(操作必须尽可能少对原有dom树的影响),然后再进行一次渲染到目标element中。

二、生成期间DOM缓存的选择

DocumentFragment(文档碎片对象,选择原因:脱离于文档流)
临时Element(选择原因:新element脱离于文档流)
createElement,再一步步进行渲染
通过描述Dom的String(下称:DomString),转化为Dom对象
临时Element+innerHTML+cloneNode返回最外层element元素对象,再进行插入appendChild,必要时还需要选择器方法讲某一个Element对象提取出来
XML字符串通过解析生成Element对象(注意,不是HTMLxxxElement对象,是Element对象),然后将该对象appendChild进去
临时字符串(选择原因:借助innerHTML渲染,一次渲染)
三、DocumentFragment的优缺点

基本模式:

var fragment = document.createDocumentFragment();
    fragment.appendChild(
        ...    //生成Element的IIFE
    )
//IIFE示例,根据配置创建元素
var ConfigVar = {
  ELname:"div",
  id:"blablabla",
  name:"balblabla",
  class:"ClassName"
}
(function(Config){
      var el = document.createElement(Config.ELname);
          el.className = (Config.class || "");
    for (let AttrName in Config){
          if (AttrName == "class")continue;
          el.setAttribute(AttrName,Config[AttrName]);
    }
      return el;
})(ConfigVar)

优点

1、脱离于文档流,操作不会对Dom树产生影响

2、在每一次生成临时Element时候就可以将该Element对象的引用保存下来,而不需要多次用选择器再次获取。

缺点

兼容性只是达到IE9+

http://caniuse.com/#search=DocumentFragment

四、createElement的优缺点

基本模式

var el = document.createElement("ElementName");    
    el.className = "";
    el.setAttribute("AttrName",AttrValue);
    el.setAttribute("AttrName",AttrValue);
    ...
    el.appendChild(        
          ... //生成Element的IIFE,见上文
    );

优点

1、新创建的元素脱离于文档流,操作不会对Dom树产生影响

2、兼容性最好

3、在每一次生成临时Element时候就可以将该Element对象的引用保存下来,而不需要多次用选择器再次获取。

缺点

每一次调用setAttribute方法都是一次次对Element进行修改,此处具有潜在的性能损耗。

五、DomString——临时Element+innerHTML+cloneNode的优缺点

基本模式

var domString2Dom = (function(){
    if (window.HTMLTemplateElement){
        var container = document.createElement("template");
        return function(domString){
            container.innerHTML = domString;
            return container.content.firstChild.cloneNode(true)
        }
    }else{
        //对不支持的template 的浏览器还有兼容性方法没写,所以不支持tr,td等些元素inner进div中。
        var container = document.createElement("div");
        return function(domString){
            container.innerHTML = domString;
            return container.firstChild.cloneNode(true)
        }        
    }
})();
var template = domString2Dom('<div class="TestClass" Arg="TestArg"></div>');
for (var index = 0; index < 80; index++) {    
  template.appendChild(
    (function(){
      var el = domString2Dom("<div>M</div>");
      return el
    })()
  )                
}

优点

创建Dom之后不需要多次进行setAttribute

缺点

1、临时元素不能包裹一些特定的元素(不能在所有浏览器的所有 HTML 元素上设置 innerHTML 属性)

2、解析的过程进行了很多其余的操作。此处具有潜在的性能损耗。

3、插入的字符串第一层Node只允许有一个元素

六、DomString——XML解析的优缺点

基本模式

var XMLParser = function () {
    var $DOMParser = new DOMParser();
    return function (domString) {
        if (domString[0] == "<") {
            var doc = $DOMParser.parseFromString(domString, "application/xhtml+xml");
            return doc.firstChild;
        }
        else {
            return document.createTextNode(domString);
        }
    };
}();
var template = XMLParser('<div class="TestClass" Arg="TestArg"></div>');
for (var index = 0; index < 80; index++) {
  template.appendChild((function () {
    var el = XMLParser("<div>M</div>");
    return el;
  })());
}

优点

DomString方法中通用性最强的,虽然IE10+才支持DOMParser,但是IE9以下的有替代方法

缺点

1、解析的过程本身就具有潜在的性能损耗。

2、只能得到刚刚创建最外层元素的克隆。子元素的引用还需要用选择器。

3、插入的字符串第一层Node只允许有一个元素

七、临时字符串的优缺点

基本模式:

var template = document.createElement("div");
template.innerHTML = `<div class="TestClass" Arg="TestArg">
                        Test TextNode
                        ${(function(){
                          var temp = new Array(8);
                          for (var index = 0; index < 80; index++) {
                            temp[index]="<div>M</div>"
                          }
                          return temp.join()
                        }())}
                      </div>` //需要增加的一大段Element

优点

1、通用性最强,不需要逐步创建一大堆无用的Element对象引用

2、运用es6模板字符串编码优雅,不需要字符串用加号进行链接

缺点

1、如果是直接给出配置Config进行渲染需要进行字符串的生成

2、只能得到刚刚创建最外层元素的引用。子元素的引用还需要用选择器。

八、Template元素

由于HTML5中新增了template元素

其特点就是有一个content属性是HTMLDocumentFragment对象,所以可以包容任何元素

基本范式是:

var template = document.createElement("template");
template.innerHTML = `<div class="TestClass" Arg="TestArg">
                        Test TextNode
                        ${(function(){
                          var temp = new Array(8);
                          for (var index = 0; index < 80; index++) {
                            temp[index]="<div>M</div>"
                          }
                          return temp.join()
                        }())}
                      </div>` //需要增加的一大段Element
// template.content 是HTMLDocumentFragment

优点

比div要好很多,作为临时元素容器的包容性更强

缺点

兼容性不好:http://caniuse.com/#search=HTML%20templates 在不支持的浏览器中表示为HTMLUnknownElement

九、各种方法的效率对比

​ 测试代码:(由于笔者不太熟悉各种浏览器性能的BUG,这里的代码如果有不足请指正),代码由typescript进行编写,也可以用babel进行编译。

/**
 * @param Count:渲染DOM结构的次数
 */
var DateCount = {
    TimeList : {},
    time:function(Str){
        console.time(Str);
    },
    timeEnd:function(Str){
        console.timeEnd(Str);
    }
};
//==================工具函数======================
var domString2Dom = (function () {
    var container;
    if (window.HTMLTemplateElement) {
        container = document.createElement("template");
        return function (domString) {
            container.innerHTML = domString;
            return container.content.firstChild.cloneNode(true);
        };
    }
    else {
        //对不支持的template 的浏览器还有兼容性方法没写,所以不支持tr,td等些元素inner进div中。
        container = document.createElement("div");
        return function (domString) {
            container.innerHTML = domString;
            return container.firstChild.cloneNode(true);
        };
    }
})();
var XMLParser = (function () {
    var $DOMParser;
    if (window.DOMParser) {
        $DOMParser = new DOMParser();
        return function (domString) {
            if (domString[0] == "<") {
                var doc = $DOMParser.parseFromString(domString, "application/xhtml+xml");
                return doc.firstChild;
            }
            else {
                return document.createTextNode(domString);
            }
        };
    }else{
        $DOMParser = new ActiveXObject("Microsoft.XMLDOM");
        return function (domString) {
            if (domString[0] == "<") {
                $DOMParser.async = false;
                $DOMParser.loadXML(domString);   
                return $DOMParser
            }
            else {
                return document.createTextNode(domString);
            }                
        }

    }

})();
//===============================================

var Test = function(Count){
    //保留这种写法,能够在移动端平台中不依靠控制台进行效率测试
    // var DateCount = {
    //     TimeList : {},
    //     time:function(Str){
    //         this.TimeList[Str] = Date.now();
    //     },
    //     timeEnd:function(Str){
    //         alert(Str+(Date.now() - this.TimeList[Str]));
    //     }
    // }

        //基准测试1:
    DateCount.time("无临时div + 不需要字符串拼接 + innerHTML:")
    for (let index = 0; index < Count; index++) {
        (function(){
            var template = document.createElement("div");
                template.className = "TestClass";
                template.setAttribute("Arg","TestArg")
                template.innerHTML = ` Test TextNode
<div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div>
<div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div>
<div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div>
<div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div>
<div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div>
<div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div>
<div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div>
<div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div>
<div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div>
<div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div>
<div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div>
<div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div><div child="true">M</div>
` //需要增加的一大段Element,共100个子级div
            return template
        }())  
    }
    DateCount.timeEnd("无临时div + 不需要字符串拼接 + innerHTML:")

    //基准测试2:
    DateCount.time("createElement+appendChild写法:")
    for (let index = 0; index < Count; index++) {
        (function(){
            var template = document.createElement("div");
                template.className = "TestClass";
                template.setAttribute("Arg","TestArg")

                template.appendChild(document.createTextNode('Test TextNode'));
                for (let index = 0; index < 100; index++) {
                    let element = document.createElement("div");
                        element.setAttribute("child","true");
                        element.appendChild(document.createTextNode("M"))
                        template.appendChild(element)
                }
            return template
        }())  
    }
    DateCount.timeEnd("createElement+appendChild写法:")    

    //DocumentFragment 
    DateCount.time("DocumentFragment+ createElement+appendChild 写法:")
    for (let index = 0; index < Count; index++) {
        (function(){
            var fragment = document.createDocumentFragment();
                fragment.appendChild(function(){
                    var template = document.createElement("div");
                        template.className = "TestClass";
                        template.setAttribute("Arg","TestArg")

                        template.appendChild(document.createTextNode('Test TextNode'));
                        for (let index = 0; index < 100; index++) {
                            let element = document.createElement("div");
                                element.setAttribute("child","true");
                                template.appendChild(element)
                        }
                    return template;
                }());

            return fragment
        }())  
    }
    DateCount.timeEnd("DocumentFragment+ createElement+appendChild 写法:")    

    //DomString——临时Element+innerHTML+cloneNode
    // DateCount.time("DomString——临时Element+innerHTML+cloneNode:")
    // for (let index = 0; index < Count; index++) {
    //     (function(){
    //         var template = domString2Dom('<div class="TestClass" Arg="TestArg"></div>');
    //         for (let index = 0; index < 100; index++) {    
    //             template.appendChild(
    //                 (function(){
    //                     var el = domString2Dom("<div child='true'>M</div>");
    //                     return el
    //                 })()
    //             )                
    //         }
    //         return template;
    //     }())  
    // }
    // DateCount.timeEnd("DomString——临时Element+innerHTML+cloneNode:")    

    //DomString——XML解析
    // DateCount.time("DomString——XML解析:")
    // for (let index = 0; index < Count; index++) {
    //     (function(){
    //         var template = XMLParser('<div class="TestClass" Arg="TestArg"></div>');
    //         for (let index = 0; index < 100; index++) {
    //             template.appendChild((function () {
    //                 var el = XMLParser("<div child='true'>M</div>");
    //                 return el;
    //             })());
    //         }
    //     }())  
    // }
    // DateCount.timeEnd("DomString——XML解析:")   

    //临时div + 临时字符串拼接:
    DateCount.time("临时div + 字符串拼接:")
    for (let index = 0; index < Count; index++) {
        (function(){
            let template = document.createElement("div");
            template.innerHTML = `<div class="TestClass" Arg="TestArg">
                                    Test TextNode
                                    ${(function(){
                                        let temp = "";
                                        for (let index = 0; index < 100; index++) {
                                            temp+="<div child='true'>M</div>"
                                        }
                                        return temp
                                    }())}
                                </div>` //需要增加的一大段Element
            return template.firstChild;
         }())  
    }
    DateCount.timeEnd("临时div + 字符串拼接:")

    //临时template + 临时字符串拼接:
    DateCount.time("临时template + 字符串拼接:")
    for (let index = 0; index < Count; index++) {
        (function(){
            var template = document.createElement("template");
            template.innerHTML = `<div class="TestClass" Arg="TestArg">
                                    Test TextNode
                                    ${(function(){
                                        let temp = "";
                                        for (let index = 0; index < 100; index++) {
                                            temp+="<div child='true'>M</div>"
                                        }
                                        return temp
                                    }())}
                                </div>` //需要增加的一大段Element
            return template.content;
         }())  
    }
    DateCount.timeEnd("临时template + 字符串拼接:")

    //临时template + createElement+appendChild 写法
    DateCount.time("template + createElement+appendChild 写法:")
    for (let index = 0; index < Count; index++) {
        (function(){
            var template = document.createElement("template");
                template.appendChild(function(){
                    var template = document.createElement("div");
                        template.className = "TestClass";
                        template.setAttribute("Arg","TestArg")

                        template.appendChild(document.createTextNode('Test TextNode'));
                        for (let index = 0; index < 100; index++) {
                            let element = document.createElement("div");
                                element.setAttribute("child","true");
                                template.appendChild(element)
                        }
                    return template;
                }());
            return template.content
         }())  
    }
    DateCount.timeEnd("template + createElement+appendChild 写法:")

};

for (var key of [1,10,100,1000]) {
    console.log("Start"+key);
    Test(key);
}

十、结论

经过笔者基本依据手上平台进行测试,

无临时div + 不需要字符串拼接 + innerHTML // createElement+appendChild写法:性能低,无论在桌面端还是移动端,在IE/Edge系还是 Webkit系都是同样的表现
domString 方法:性能最差
DocumentFragment+ createElement+appendChild 写法:性能在桌面WebKit端表现最好,移动端也有不错的表现
字符串拼接:临时div + 字符串拼接/临时template + 字符串拼接:性能表现基本一致,桌面端WebKit上:比DocumentFragment(或tmplate) + createElement+appendChild性能差多了;与之相反,在IE系inneHTML的性能最高,是前者的x十倍。在移动端上两者性能相近,innerHTML略差一点点。
template + createElement+appendChild 写法:与DocumentFragment+ createElement+appendChild 写法效率相仿。
具体数据测试之后再补充。

(待续)

© 著作权归作者所有

文章转载自 开源中国社区 [http://www.oschina.net]

相关文章
|
4月前
|
数据采集 人工智能 安全
国内大模型GEO优化实战FAQ
国内GEO(生成式引擎优化)正成为内容可见性新战场:当2.49亿用户习惯“问AI”,内容能否被文心、豆包、Kimi等主流大模型引用,决定品牌在AI答案中的存在感。本文由Geo专家于磊一线实践提炼,聚焦国内大模型特有的检索逻辑、权威信号与生态规则,以25个高频问题为纲,每问均含机制解析、可验证数据及即用模板/清单/代码,直击“怎么写才被引”这一核心痛点。
379 1
|
5月前
|
存储 前端开发 算法
PNG、JPG、WebP 有什么区别?浏览器端图片格式转换原理与实践
本文介绍一款纯浏览器端的在线图片格式转换工具(支持PNG/JPG/WebP/AVIF),详解各格式差异与适用场景,并分享如何利用Canvas、File API和toBlob()实现零上传、保隐私、高效率的本地转换方案。
|
7月前
|
缓存 自然语言处理 安全
游戏安全运营中,如何用IP代理识别服务快速检测作弊网络出口?操作指南来了
某MMO开服即遭工作室刷榜,IP 3小时轮换200+网段。依托IP数据云离线库,基于net_type、risk_score等20+维度,毫秒级识别作弊网络出口,误判率<0.5%,单机QPS超250万,助力游戏安全从“封IP”升级为实时出口风控。(239字)
503 0
游戏安全运营中,如何用IP代理识别服务快速检测作弊网络出口?操作指南来了
|
7月前
|
人工智能 供应链 架构师
未来2-3年,智能体架构师是中国最赚钱的职业
智能体架构师卢成(Agent Architect),深耕AI落地实战多年,专注为企业提供轻量化、可量化的AI工作流解决方案。他定义该职业为“智能体时代的API接口”, bridging 技术与商业,不堆砌概念,只做降本增效、能变现的真落地。行业黄金期已至,轻服务、标准化、强适配是其核心方法论。(239字)
|
10月前
|
机器学习/深度学习 人工智能 自然语言处理
PPO 应用 —— 大模型偏好优化的核心场景与落地思路
本文详解PPO算法在大模型RLHF落地中的核心应用:聚焦对话风格、客服话术、内容生成、安全合规、垂直领域及多模态六大场景,强调“偏好定制化”价值。全程无代码,提供4步落地法与3大实操要点,助力企业高效实现大模型优化。(239字)
|
11月前
|
人工智能 资源调度 调度
全国超算互联网平台实操:20 + 智算中心并网,跨架构算力按需调取
全国超算互联网平台已并网20+智算中心,构建覆盖X86/ARM/GPU等多架构的一体化算力网络,集成15万+加速卡、200万核心算力池。依托国家标准与“算力+调度+生态”三位一体架构,支持开发者按需调取跨区域、跨架构算力,分钟级完成任务部署,显著降低使用门槛与研发成本。
1164 0
|
安全 Linux iOS开发
Burp Suite Professional 2025.10 发布 - Web 应用安全、测试和扫描
Burp Suite Professional 2025.10 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
873 3
|
存储 弹性计算 安全
蚂蚁数科 MAPPIC 密态计算云平台入驻阿里云计算巢,打造云上密态计算服务
蚂蚁数科 MAPPIC 密态计算云平台入驻阿里云计算巢,打造云上密态计算服务
525 1
|
供应链 API 开发者
解锁电商数据的无限可能:探秘京东商品SKU信息API接口
京东商品SKU信息API接口是电商开发与运营中的重要工具,帮助开发者获取商品的详细属性,如库存、价格、规格等。通过该接口,电商平台可以丰富商品展示页面,提升用户体验;商家能实时掌握库存动态,优化销售策略;数据分析人员可深入洞察市场趋势,实现精准营销。使用前需注册京东开放平台账号、创建应用并获取API权限,同时仔细阅读API文档以确保正确调用。代码示例展示了如何用Python调用该接口,并处理返回数据。未来,该接口将在个性化推荐、智能库存管理和数据分析等领域发挥更大作用,助力电商业务创新与发展。
1407 14
|
安全 网络安全 开发工具
解决Ascend上vllm运行时出现urllib3.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED]
在使用vllm模型时,运行示例代码下载模型文件时遇到了SSL安全校验问题。临时解决方案是修改`requests/sessions.py`文件中`request`函数的`verify`参数默认值为`False`。