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

推荐订阅源

B
Blog RSS Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
GRAHAM CLULEY
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cybersecurity and Infrastructure Security Agency CISA
Simon Willison's Weblog
Simon Willison's Weblog
Latest news
Latest news
C
CERT Recently Published Vulnerability Notes
T
Threatpost
V
Vulnerabilities – Threatpost
AWS News Blog
AWS News Blog
Blog — PlanetScale
Blog — PlanetScale
C
Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
U
Unit 42
The Register - Security
The Register - Security
T
The Blog of Author Tim Ferriss
Stack Overflow Blog
Stack Overflow Blog
The Hacker News
The Hacker News
AI
AI
Project Zero
Project Zero
Scott Helme
Scott Helme
S
Securelist
Vercel News
Vercel News
GbyAI
GbyAI
S
Security @ Cisco Blogs
I
InfoQ
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
Forbes - Security
Forbes - Security
Google Online Security Blog
Google Online Security Blog
W
WeLiveSecurity
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Heimdal Security Blog
G
Google Developers Blog
D
DataBreaches.Net
The Last Watchdog
The Last Watchdog
D
Docker
MyScale Blog
MyScale Blog
T
Tor Project blog
Cyberwarzone
Cyberwarzone
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
T
Tenable Blog
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 聂微东
月光博客
月光博客

博客园 - 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。因此方法二比方法一的性能得到了显著的提升!