yourFlatObject = {
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"created": "2021-11-07T15:18:53.215Z",
"edited": "2021-11-07T15:18:53.215Z",
"name": "Luke Skywalker",
"homeworld": "https://www.swapi.tech/api/planets/1",
"url": "https://www.swapi.tech/api/people/1",
"2height2": "172",
"2mass": "77",
"2hair_color": "blond",
"2skin_color": "fair",
"2eye_color": "blue",
"2birth_year": "19BBY",
"2gender": "male",
"2created": "2021-11-07T15:18:53.215Z",
"2edited": "2021-11-07T15:18:53.215Z",
"2name": "Luke Skywalker",
"2homeworld": "https://www.swapi.tech/api/planets/1",
"2url": "https://www.swapi.tech/api/people/1"
}
let html = '';
for ( let item in yourFlatObject )
if (yourFlatObject[item] ) //!= null
html+=`<p>${yourFlatObject[item]}</p>`;
html = html?`<div class="info">${html}</div>`:'';
let html = '';
const filteredData = Object.entries(yourFlatObject)
.map(([key, value]) => ({ key, value }))
.filter(({ value }) => value != null);
if ( filteredData.length ) {
filteredData.forEach( (item)=>{
html+=`<p>${item.value}</p>`;
});
html = `<div class="info">${html}</div>`;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 | |
3 |
Test name | Executions per second |
---|---|
1 | 92725.4 Ops/sec |
3 | 410243.9 Ops/sec |
I'll provide an in-depth explanation of the provided benchmark.
Benchmark Definition
The benchmark is testing two different approaches to generate HTML content from a JavaScript object using the yourFlatObject
variable. The benchmark consists of three test cases, but we will focus on the two individual test cases: "1" and "3".
Test Case 1:
let html = '';
for (let item in yourFlatObject) {
if (yourFlatObject[item] !== null) { //!= null
html += `<p>${yourFlatObject[item]}</p>`;
}
}
html = html ? `<div class="info">${html}</div>` : '';
This test case uses a simple for...in
loop to iterate over the properties of the yourFlatObject
object. It checks if each property value is not null (using the loose equality operator !==
) and appends it to the HTML string. Finally, it wraps the entire HTML content in a <div>
element with a class of "info".
Test Case 3:
let html = '';
const filteredData = Object.entries(yourFlatObject)
.map(([key, value]) => ({ key, value }))
.filter(({ value }) => value !== null);
if (filteredData.length) {
filteredData.forEach((item) => {
html += `<p>${item.value}</p>`;
});
}
html = `<div class="info">${html}</div>`;
This test case uses the Object.entries()
method to convert the yourFlatObject
object into an array of key-value pairs. It then filters out any null values using the filter()
method and maps each pair to a simple object with only the key and value properties.
Comparison
The two test cases differ in their approach:
for...in
loop, which can be slower due to the overhead of iterating over the object's properties.Object.entries()
method, which is likely faster since it uses an optimized internal implementation.Pros and Cons
Object.entries()
and its implementation details.Other Considerations
yourFlatObject
variable is already created and contains only non-null values.Library Usage
The Object.entries()
method is a built-in JavaScript function introduced in ES6 (ECMAScript 2015). It provides an efficient way to convert objects into arrays of key-value pairs.
Special JS Features/Syntax
None mentioned in the provided benchmark definition.