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 ) {
for ( let item in filteredData ) html+=`<p>${filteredData[item]}</p>`;
html = `<div class="info">${html}</div>`;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 | |
3 |
Test name | Executions per second |
---|---|
1 | 94001.4 Ops/sec |
3 | 248530.3 Ops/sec |
I'll break down the provided benchmark definition and explain what's being tested, compared options, pros and cons of those approaches, library usage, special JavaScript features, and other considerations.
Benchmark Definition
The benchmark is comparing two different ways to generate HTML strings from an object: one using a traditional for
loop (Test Case 1) and the other using Object.entries()
and filter()
methods (Test Case 3).
Script Preparation Code
The script preparation code defines an object called yourFlatObject
, which contains several properties with values that are used in the benchmark. The object is defined as:
{
"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",
// ...
}
The properties have different names and values, but they are used in the benchmark as if they were a single object.
Html Preparation Code
The html preparation code is where the actual comparison happens. There are two test cases:
let html = '';
for (let item in yourFlatObject) {
if (yourFlatObject[item] != null)
html += `<p>${yourFlatObject[item]}</p>`;
}
html = html ? `<div class="info">${html}</div>` : '';
This code iterates over the properties of the object, concatenates them to an HTML string, and then wraps the string in a div
element if it's not empty.
let html = '';
const filteredData = Object.entries(yourFlatObject)
.map(([key, value]) => ({ key, value }))
.filter(({ value }) => value != null);
if (filteredData.length) {
for (let item in filteredData) {
html += `<p>${filteredData[item]}</p>`;
}
html = `<div class="info">${html}</div>`;
}
This code uses Object.entries()
to get an array of key-value pairs, filters out the properties with null values, and then iterates over the remaining data to concatenate it to an HTML string. If the filtered data is not empty, it wraps the string in a div
element.
Comparison
The benchmark compares the execution time of both test cases on different browsers and devices:
Browser | Executions Per Second (Test Case 1) | Executions Per Second (Test Case 3) |
---|---|---|
Opera 80 | 248530.296875 | 94001.390625 |
Pros and Cons of Each Approach
for
loop, which can be slower than modern array methods.Object.entries()
and filter()
methods.Library Usage
The benchmark uses the following library:
https://www.swapi.tech/api/planets/1
: This is a hypothetical API endpoint that returns information about the planet "1" in the Star Wars universe. It's used as part of the object definition to make it more interesting and challenging.Object.entries()
and filter()
: These are built-in JavaScript methods for working with arrays.Special JavaScript Features
There are no special JavaScript features or syntax used in this benchmark beyond what's normally available in modern browsers.
Other Considerations
The benchmark could benefit from additional considerations, such as: