Dom操作-元素删除

Benchmark created on


Preparation HTML

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
</body>
</html>

Setup

function init() {
	document.body.innerHTML = `<div id="content">
  		<div id="text1">Hello World</div>
  		<div id="text2">Hello JavaScript</div>
	</div>`
}

Test runner

Ready to run.

Testing in
TestOps/sec
innerHTML赋空值删除
init()

document.getElementById('content').innerHTML = ''

ready
removeChild方法删除
init()
const content = document.getElementById('content')
while(content.hasChildNodes()){
  content.removeChild(content.firstChild)
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.