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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tor Project blog
K
Kaspersky official blog
S
Security Affairs
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 最新话题
S
Securelist
Google DeepMind News
Google DeepMind News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
The Last Watchdog
The Last Watchdog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
小众软件
小众软件
Vercel News
Vercel News
博客园 - 叶小钗
量子位
Help Net Security
Help Net Security
C
Cybersecurity and Infrastructure Security Agency CISA
P
Privacy & Cybersecurity Law Blog
T
The Exploit Database - CXSecurity.com
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
美团技术团队
AI
AI
V
V2EX
T
Troy Hunt's Blog
aimingoo的专栏
aimingoo的专栏
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
D
DataBreaches.Net
H
Help Net Security
V2EX - 技术
V2EX - 技术
T
Tailwind CSS Blog
宝玉的分享
宝玉的分享
MongoDB | Blog
MongoDB | Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
T
Threatpost
J
Java Code Geeks
Recent Announcements
Recent Announcements
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Webroot Blog
Webroot Blog
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
I
InfoQ
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
Security Latest
Security Latest

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