惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CERT Recently Published Vulnerability Notes
U
Unit 42
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
H
Heimdal Security Blog
Help Net Security
Help Net Security
H
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Palo Alto Networks Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Secure Thoughts
The GitHub Blog
The GitHub Blog
博客园_首页
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Azure Blog
Microsoft Azure Blog
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - 【当耐特】
J
Java Code Geeks
S
SegmentFault 最新的问题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
O
OpenAI News
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
B
Blog RSS Feed
V2EX - 技术
V2EX - 技术
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tenable Blog
PCI Perspectives
PCI Perspectives
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
Schneier on Security
Schneier on Security
Google Online Security Blog
Google Online Security Blog
美团技术团队
G
GRAHAM CLULEY
D
DataBreaches.Net
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
W
WeLiveSecurity
Vercel News
Vercel News
S
Security Affairs
T
Tailwind CSS Blog
V
Vulnerabilities – Threatpost
博客园 - 司徒正美
G
Google Developers Blog
D
Docker
Webroot Blog
Webroot Blog

博客园 - focus

IE浏览器自定义地址协议 -- 通过B/S程序中打开C/S程序 Html编辑器 -- TinyMCE HTML编辑器 -- HtmlArea 用 BindingSource 绑定窗体中控件不失去焦点无法更新数据源的问题 - focus asp.net 服务器控件防止重复提交表单 - focus - 博客园 html 元素的 onpropertychange 事件 c# 操作 Excel : 给单元格增加下拉选项值 很黄的日期格式化。。。 - focus - 博客园 Asp.net 导出 .html,.txt 等格式的文件 ActiveReports 打印条码无法设置纸张大小? ActiveReports 代码控制报表连续打印 《JavaScript高级程序设计》读书笔记之一:几个与原始类型等价的引用类型的常用方法和属性 js:delete 操作符 js:for...in 语句(JavaScript For...In Statement) js:字符串类型快速转化成数字类型和数字类型快速转化为字符串类型 html 的 image button 自动提交 form Internet Explorer Developer Toolbar IE 最大化和最小化 js trim
动态创建大量 html dom 元素时提升性能 - focus
focus · 2008-02-27 · via 博客园 - focus

考虑以下两种做法:
方法1

<html>
    <head>
        <script>
        function createElements()
        {
            var t1 = new Date();
            for(var i = 0; i < 10000; i++)
            {
                var oP = document.createElement("p");
                oP.innerText 
= "hello world";
                document.all(
"div1").appendChild(oP);
            }
            var t2 = new Date();
            document.all("div2").innerText = "time span is:" + (t2 
- t1) + "ms";
        }
        </script>
    </head>
    <body onload="createElements()">
        <div id="div1" />
        <div id="div2" />
    </body>
</html>

输出结果:

time span is:6718ms

方法2

<html>
    <head>
        <script>
        function createElements()
        {
            var t1 = new Date();
            var oFragment = document.createDocumentFragment();
            for(var i = 0; i < 10000; i++)
            {
                var oP = document.createElement("p");
                oP.innerText 
= "hello world";
                oFragment.appendChild(oP);
            
}
            document.all("div1").appendChild(oFragment);
            var t2 = new Date();
            document.all("div2").innerText = "time span is:" + (t2 
- t1) + "ms";
        }
        </script>
    </head>
    <body onload="createElements()">
        <div id="div1" />
        <div id="div2" />
    </body>
</html>

输出结果:

time span is:1344ms

将新创建的元素临时 append 到一个 document fragment 对象上,全部创建完成之后再将此 document fragment 对象 apped 到 document 上,这样的话只是在 append document fragment 的时候刷新一次 dom,而不使用 document  fragment 对象在每次 append 的时候都会刷新 dom。因此方法二比方法一的性能得到了显著的提升!