innerHTML和appendChild的性能

  目测innerHTML比appendChild好了3到4倍, 但是界面渲染还是很慢啊;

            /**
             *chrome浏览器;
             *               innerHTML appendChild
             * 1千条的情况下:3MS       11MS
             * 1万条的情况下:25MS(14MS)      111MS(52MS)
             * 10万的情况下:276MS(145MS)      672MS(480S)
             * 100万界面卡死了
             * */

   chrome结果

  直接点击就可以运行哦, 怎么测试才是对的,感觉不对啊;

<html>
    <head>
        <meta charset="utf-8"/>
    </head>
    <script src="p.js"></script>
    <body>
        <ul id="ul0">

        </ul>
        <ul id="ul1">

        </ul>
        <script>
            window.onlad = function() {
                var liTpl = "<li>{{i}}</li>";
                var ul0 = document.getElementById("ul0");
                var ul1 = document.getElementById("ul1");
                var len = 10000;
                var str = "";
                var d = new Duration();
                d.start("循环使用的时间");
                for(var i=0; i< len; i++) {
                };
                d.end();
                var loopTimes = d.print();

                var d = new Duration("使用innerHTML");
                for(var i=0; i< len; i++) {
                    str += liTpl.replace(/{{i}}/g,i);
                };
                d.start();
                ul0.innerHTML = str;
                d.end();
                d.print();

                var d = new Duration("使用appendChild");

                d.start();
                for(var i=0; i< len; i++) {
                    var li = document.createElement("li");
                    li.innerHTML = i;
                    ul0.appendChild( li );
                };
                d.end();
                d.print();
            }
         </script>
        <script>
            var P = (function(prototype, ownProperty, undefined) {
                return function P(_superclass /* = Object */, definition) {
                    // handle the case where no superclass is given
                    if (definition === undefined) {
                        definition = _superclass;
                        _superclass = Object;
                    }

                    // C is the class to be returned.
                    //
                    // When called, creates and initializes an instance of C, unless
                    // `this` is already an instance of C, then just initializes `this`;
                    // either way, returns the instance of C that was initialized.
                    //
                    //  TODO: the Chrome inspector shows all created objects as `C`
                    //        rather than `Object`.  Setting the .name property seems to
                    //        have no effect.  Is there a way to override this behavior?
                    function C() {
                        var self = this instanceof C ? this : new Bare;
                        self.init.apply(self, arguments);
                        return self;
                    }

                    // C.Bare is a class with a noop constructor.  Its prototype will be
                    // the same as C, so that instances of C.Bare are instances of C.
                    // `new MyClass.Bare` then creates new instances of C without
                    // calling .init().
                    function Bare() {}
                    C.Bare = Bare;

                    // Extend the prototype chain: first use Bare to create an
                    // uninitialized instance of the superclass, then set up Bare
                    // to create instances of this class.
                    var _super = Bare[prototype] = _superclass[prototype];
                    var proto = Bare[prototype] = C[prototype] = C.p = new Bare;

                    // pre-declaring the iteration variable for the loop below to save
                    // a `var` keyword after minification
                    var key;

                    // set the constructor property on the prototype, for convenience
                    proto.constructor = C;

                    C.extend = function(def) { return P(C, def); }

                    return (C.open = function(def) {
                        if (typeof def === ‘function‘) {
                            // call the defining function with all the arguments you need
                            // extensions captures the return value.
                            def = def.call(C, proto, _super, C, _superclass);
                        }

                        // ...and extend it
                        if (typeof def === ‘object‘) {
                            for (key in def) {
                                if (ownProperty.call(def, key)) {
                                    proto[key] = def[key];
                                }
                            }
                        }

                        // if no init, assume we‘re inheriting from a non-Pjs class, so
                        // default to using the superclass constructor.
                        if (!(‘init‘ in proto)) proto.init = _superclass;

                        return C;
                    })(definition);
                }

                // as a minifier optimization, we‘ve closured in a few helper functions
                // and the string ‘prototype‘ (C[p] is much shorter than C.prototype)
            })(‘prototype‘, ({}).hasOwnProperty);
        </script>

        <script>
            "use strict";
            var Duration = P(function(dur) {
                dur.init = function(str) {
                    this.str = str;
                }
                dur.start = function() {
                    this.times = (new Date).valueOf();
                };
                dur.end = function() {
                    this.usedTimes = (new Date).valueOf() - this.times;
                };
                dur.print = function() {
                    var oDiv = document.createElement("div");
                    var bodyDiv = document.createElement("div");
                    oDiv.innerHTML = this.str;
                    bodyDiv.innerHTML = this.usedTimes + "MS";
                    document.body.appendChild( oDiv );
                    document.body.appendChild( bodyDiv );
                };
            });
        </script>
    </body>
</html>
时间: 2024-08-27 10:13:26

innerHTML和appendChild的性能的相关文章

innerHTML与appendChild(newnodeText)的区别

innerHTML和createTextNode都可以把一段内容添加到一个节点中,区别是如果这段内容中有html标签时表现就不同了,在createTextNode中会当作文本处理,不会被浏览器解析,但用innerHTML就会被当作HTML代码处理 newnode.innerHTML="<b>Javascript</b>"; var newnodeText=document.createTextNode("<b>Javascript</

深入理解javascript描述元素内容的5个属性

× 目录 [1]innerHTML [2]outerHTML [3]innerText[4]outerText[5]textContent 前面的话 <p>This is a <i>simple</i> document</p> 上面这行代码中,<p>元素的内容是什么呢?答案一:内容是HTML字符串"This is a <i>simple</i> document":答案二:内容是纯文本字符串"

【转】console.time 简单分析javascript动态添加Dom节点的性能

本文代码约定 1 el: 指的是增加直接点的DOM节点 2 totalNum: 为100000(值越大越能体现差距)指的是循环创建的DOM节点 3 for(var i=0;i<totalNum;i++){}: 我们用for来表示就好了,简写代码 如果叫你用javascript动态增加DOM节点,你有哪几种思路呢? 1 .使用innerHTML和字符串拼接 console.time("time1"); var str = ""; for{ str += &quo

console.time 简单分析javascript动态添加Dom节点的性能

Bullshit 本来想每天都更新下博客的,但是最近要考试,还有就是自己还是停留在暗自窃喜中吧(这种想法要改变).其实最近总在想,自己要怎么去管理自己的数据,每天的生活都是对自己的数据的增删查改.昨天把自己的电脑重装了,确实很多软件的存放要改地方了,之前不知道怎么去管理软件安装,所以放得乱七八糟的.说好一大堆废话之后,我最后再说一遍,管好自己的时间.数据真的比你学习东西重要. Method 本文代码约定 1 el: 指的是增加直接点的DOM节点 2 totalNum: 为100000(值越大越能

JS性能优化 之 文档片段 createDocumentFragment

我们用原生JS进行开发时,经常会用到两种更新DOM节点的方法:innerHTML 和 appendChild() .其中 innerHTML 会完全替换掉原先的节点内容,如果我们是想向元素追加子节点的话,那么 innerHTML 显然满足不了需求. 转而我们就会想到 appendChild() 方法.appendChild方法接收的参数类型为单个的节点类型对象.因此当我们要添加多个子节点时,只能通过循环来实现. 例如: for (var i = Things.length - 1; i >= 0

1.javaScript(JS)常用函数之 appendChild 和removeChild

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1,追加与删除HTML元素</span> 创建HTML元素 var newEle =document.createElement("p"); 创建的元素追加到别的元素之后: A. appendChild(B): 若B是新建的元素,在A元素的所有子元素的末尾

使用r.js来打包模块化的javascript文件

前面的话 r.js(下载)是requireJS的优化(Optimizer)工具,可以实现前端文件的压缩与合并,在requireJS异步按需加载的基础上进一步提供前端优化,减小前端文件大小.减少对服务器的文件请求.本文将详细介绍r.js 简单打包 [项目结构] 以一个简单的例子来说明r.js的使用.该项目名称为'demo',在js目录下包含s1.js和s2.js两个文件,使用requirejs进行模块化,内容如下 //s1.js define(function (){ return 1; }) /

Javascript:DOM动态创建元素实例应用

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Dom:动态创建元素</title> </head> <body> <ul id="demo1"> </ul> <input type="text" id=&quo

oneuijs/You-Dont-Need-jQuery

oneuijs/You-Dont-Need-jQuery https://github.com/oneuijs/You-Dont-Need-jQuery/blob/master/README.zh-CN.md You Don't Need jQuery 前端发展很快,现代浏览器原生 API 已经足够好用.我们并不需要为了操作 DOM.Event 等再学习一下 jQuery 的 API.同时由于 React.Angular.Vue 等框架的流行,直接操作 DOM 不再是好的模式,jQuery 使用