let V=[document.createElement('div'),document.createElement('p'),document.createElement('span'),document.createElement('a')]
let x=[0,0,0,0,1,2,3,3,2,1,0,0,1,2, 0,0,0,0,1,2,3,3,2,1,0,0,1,2, 0,0,0,0,1,2,3,3,2,1,0,0,1,2, 0,0,0,0,1,2,3,3,2,1,0,0,1,2, 0,0,0,0,1,2,3,3,2,1,0,0,1,2, 0,0,0,0,1,2,3,3,2,1,0,0,1,2]
let cn=['a','b','c','a','b','c','a','b','c','a','b','c','a','b']
let y=[[0,'a'],[0,'a'],[0,'a'],[0,'a'],[0,'a'],[0,'a'],[0,'a'],[0,'a'],[0,'a'],[0,'a'],[0,'a'],[0,'a'],[0,'a'],[0,'a']]
let z=[0,'a',0,'a',0,'a',0,'a',0,'a',0,'a',0,'a',0,'a',0,'a',0,'a',0,'a',0,'a',0,'a',0,'a']
let w=[0,0,0,0,1,2,3,3,2,1,0,0,1,2,'a','b','c','a','b','c','a','b','c','a','b','c','a','b']
let B=(a,b)=>{
let d=V[a].cloneNode();
d.className=b;
return d;
}
let z = x.map(i => V[i].cloneNode());
let z = y.map(i => B(i[0],i[1]));
let z=Array(14);for(let i=0;i<14;++i){z[i]=V[x[i]].cloneNode();z[i].className=cn[i];}
let z=Array(14);for(let i=0;i<14;++i){z[i]=V[w[i]].cloneNode();z[i].className=w[i+14];}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for loop | |
combined map | |
looped together | |
looped together double list |
Test name | Executions per second |
---|---|
for loop | 110815.0 Ops/sec |
combined map | 414596.1 Ops/sec |
looped together | 400928.3 Ops/sec |
looped together double list | 398348.7 Ops/sec |
The benchmark "createElement vs C" compares two different approaches for creating DOM elements in a web browser using JavaScript. Below, I’ll describe the two test cases, their pros and cons, along with other considerations.
createElement
Case
document.createElement
multiple times.createElement
let V=[document.createElement('div'), document.createElement('p'), document.createElement('span'),
document.createElement('a'), document.createElement('div'), document.createElement('p'),
document.createElement('span'), document.createElement('a'),
document.createElement('div'), document.createElement('p')]
C
Case
C(a)
that wraps document.createElement
.C
let C=(a)=>{return document.createElement(a)};
let V=[C('div'), C('p'), C('span'), C('a'),
C('div'), C('p'), C('span'), C('a'),
C('div'), C('p')]
The benchmark results show that C
has a slightly higher execution rate with 750,839.25 executions per second compared to createElement
at 742,170.5 executions per second. This indicates that invoking the function C
for creating elements is marginally more efficient in this test setup.
Direct createElement
(Test Name: createElement
):
createElement
can lead to verbose code.Function Wrapper C
(Test Name: C
):
Readability: Using a function like C
may enhance code readability when element creation is spread out across different locations in the codebase. For instance, if the element creation logic needs to be modified, one would only need to change it in one place.
Function Overhead: The slight performance difference indicates that for very high-performance critical applications, the choice of using direct createElement
or a wrapper function should be benchmarked as seen in this test.
Other approaches can include:
<template>
elements) can provide a way to define element structures that can be cloned multiple times, potentially improving performance.In conclusion, this benchmark serves to highlight performance differences between two common patterns in DOM manipulation, particularly useful for developers focusing on optimizing web applications.