


Recently I encountered an efficiency problem with js, which was about adding new elements to the page.
Suppose we have a page as follows:
<script> Know that you can use the following code when adding an element to a web page: <BR>//Method 1 <BR>div1.innerHTML = '<a href="">Test'; <BR>or : <BR>//Method 2 </script>
a.innertText = 'Test';
div1.appendChild(a);
Search on the Internet I came across an article discussing the efficiency of js. It probably means that method 2 is more efficient. The comparison code is as follows:
Copy code
container.innerHTML = str;
}
alert(new Date - staDate);
}
//Method 2
function init( ){
var staDate = new Date();
var doc = window.document;
for(var i=0;ivar oDiv = doc.createElement( "div");
var oText = doc.createTextNode("text");
oDiv.appendChild(oText);
container.appendChild(oDiv);
oDiv.style.id = " div_" i;
oDiv.style.width = "100px";
oDiv.style.height = "20px";
oDiv.style.backgroundColor = "#eee";
}
alert(new Date - staDate);
}
The page contains:
Judging from the execution effect, option 2 is about 10 times faster than option 1. Is this true? In fact, the above test code is open to question, let’s look at the loop code in method 1:
Copy the code
There are a lot of string operations, and in addition to using In addition to connecting strings, the = operator is also used. This is the problem. This method of operating strings in JavaScript seriously affects efficiency, so use the above method to test the efficiency of createEmenent and innerHTML. innerHTML is unfair. Based on this, it seems that string operations may have eaten up the performance of innerHTML, so I wrote the following test code:
] After testing, it was found: 1. When there are few objects created (0-about 10), innerHTML and createElement are about the same efficiency, and the test values are uncertain; 2. When there are more than 20 objects created, innerHTML is much more efficient than createElement. , the average test time is almost twice as long as createElement is innerHTML. Summary: In fact, efficiency also lies in the code written. After knowing the efficiency of the available APIs, how to write the code is also very important. Otherwise, the execution efficiency cannot be reflected, such as the codes found above from the Internet. , drawing a conclusion that is contrary to the facts.

在当前智能手机市场上,处理器可谓是各大手机厂商争夺的焦点之一。作为手机的“大脑”,处理器的性能直接影响到手机的运行速度、多任务处理能力以及游戏体验等方面。目前,麒麟8000处理器作为华为旗舰手机的配备,备受关注。那么,它与骁龙处理器相比,究竟有何优劣呢?首先,从性能方面来看,麒麟8000处理器采用了最新的台积电5nm工艺制造,拥有更加先进的制程技术,提升了能

vivoX100和vivoX100Pro性能对比分析随着智能手机市场的竞争日益激烈,各大品牌不断推出新款产品来满足消费者需求。vivo作为一家备受关注的手机品牌,近年来推出了众多备受好评的产品,其中vivoX100和vivoX100Pro是备受期待的新品。两款手机在外观设计、性能配置、拍照功能等方面有何不同?今天我们就来对比分析vivoX100

苹果处理器与骁龙8gen2对比分析随着移动智能设备的不断发展,处理器作为设备性能的核心组件,一直备受关注。苹果公司和高通公司一直是移动处理器领域的领军者,分别推出了自家的处理器产品,分别是苹果处理器和骁龙处理器。其中,苹果处理器以其强大的性能和出色的功耗控制而著称,而高通骁龙处理器则以其优秀的通信技术和多功能性而赢得了用户的青睐。本文将对苹果处理器和骁龙8g

今年初,当Microsoft和Canonical发布Windows10Bash和Ubuntu用户空间,我尝试做了一些初步性能测试UbuntuonWindows10对比原生Ubuntu,这次我发布更多的,关于原生纯净的Ubuntu和基于Windows10的基准对比。Windows的Linux子系统测试完成了所有测试,并随着Windows10周年更新放出。默认的Ubuntu用户空间还是Ubuntu14.04,但是已经可以升级到16.04。所以测试首先在14.04测试,完成后将系统升级升级到16.04

天玑8200与骁龙的性能对比:究竟孰强近年来,随着智能手机市场的日益竞争激烈,各大芯片厂商也在不断地推陈出新,力求在性能上取得更大突破。其中,联发科的天玑8200系列和高通的骁龙系列被广泛认为是当前智能手机市场上最为出色的芯片之一。那么,在天玑8200和骁龙之间,究竟孰强呢?今天我们就来对这两款处理器进行性能对比,一探究竟。首先,让我们先来了解一下天玑820

大家期待的win10正式版已经发布啦,对于正在使用win7的同学来说肯定一直在纠结到底要不要将电脑系统升级到win10,升级后会不会比win7系统更好?是否支持常用的所有软件?win7和win10哪个好?他们的区别又是什么?对此,小编给出了win10与win7对比的一些解答。微软推出最新win10系统,很多电脑用户对Win10也甚是关注。大家也疑惑,windows10和win7有什么区别?win7和windows10哪个会比较好用。Win7系统稳定安全是目前使用用户最多,也是最受欢迎的Windo

MySQL与MongoDB:两个数据库系统的性能对比随着互联网的发展和数据量的不断增长,数据库的性能和可伸缩性变得愈发重要。MySQL和MongoDB是两个常用的数据库系统,它们在处理大数据量和高并发请求时有着不同的表现。本文将对MySQL和MongoDB进行性能对比,并通过代码示例来说明它们的差异。MySQL是一种关系型数据库,以其稳定性和成熟的特性而著名

权威基准测试表明,Echo、Gin、Fasthttp、Iris和Buffalo是性能最佳的Go框架。EchoAPI端点响应时间最快,其次是GinWebUI,而Fasthttp定时任务响应时间最短。最佳实践包括使用缓存、并发模式和代码优化。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
