window.NewEle = function(id, tag, cssText, properties, wrap) { //创建新元素
const ele = document.createElement(tag);
ele.id = id;
ele.setAttribute('style', cssText);
Object.assign(ele, properties);
wrap && wrap.appendChild(ele);
return ele;
};
'use strict';
const app = document.createElement('div');
for (let i = 0; i < 10000; i++) {
const mydiv = NewEle('Z_0.5840766942292237', 'div', 'width: 216px; height: 164px; position: absolute; display: block; left: 619.3px; top: 206px; z-index: 370;', null);
const myimg = NewEle(null, `img`, `position:absolute;clip:rect(0,auto,auto,0);top:0px`, {
src: 'images/Zombies/Zombie/Zombie.webp?&ts=0.6870581495010164'
}, mydiv);
const myshadow = NewEle(null, 'div', 'left:75px;top:139px;', {
className: 'Shadow',
}, mydiv);
app.append(mydiv);
}
'use strict';
const app = document.createElement('div');
const frag = document.createElement('template');
for (let i = 0; i < 10000; i++) {
frag.innerHTML = `<div id="Z_0.5840766942292237" style="width: 216px; height: 164px; position: absolute; display: block; left: 619.3px; top: 206px; z-index: 370;"><div class="Shadow" style="left:75px;top:139px;"></div><img style="position:absolute;clip:rect(0,auto,auto,0);top:0px" src="images/Zombies/Zombie/Zombie.webp?&ts=0.6870581495010164"></div>`
app.append(frag.content);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
NewEle | |
innerHTML |
Test name | Executions per second |
---|---|
NewEle | 5.8 Ops/sec |
innerHTML | 4.3 Ops/sec |
Let's break down the provided benchmark JSON and explain what's being tested, compared options, pros and cons, and other considerations.
Benchmark Definition
The benchmark is comparing two approaches to create and render HTML elements: NewEle
(a custom function) and innerHTML
(using a template element).
NewEle Function
The NewEle
function creates a new HTML element with the specified ID, tag, CSS text, properties, and wrapping element. The purpose of this function is to create a self-contained HTML element that can be appended to another container.
Options Compared
document.createElement()
and sets its attributes manually.app.append(frag.content)
.Pros and Cons of Each Approach
Library and Its Purpose
There is no specific library mentioned in the benchmark JSON. The template
element is a native HTML5 feature used to render dynamic content.
Special JS Feature or Syntax
There are no special JavaScript features or syntaxes being tested in this benchmark, as both approaches rely on standard JavaScript APIs.
Other Alternatives
const frag = document.createElement('fragment')
) to render HTML content. This approach is similar to innerHTML
but may be faster for smaller content blocks.Benchmark Considerations
When choosing between these approaches, consider the trade-off between control, flexibility, and performance. If you need more control over your elements' attributes or want to reuse code snippets, NewEle
might be a better choice. However, if you're rendering complex HTML content and prioritize speed, innerHTML
could be the way to go.
Keep in mind that this benchmark only compares two approaches, and there may be other factors to consider when choosing between them.