Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object filter mappingv2
(version: 0)
benchmark comparing the perf of creating a new object by filter out entries
Comparing performance of:
object.entries vs object.keys
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function makeid() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i < 5; i++) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } window.parentObj = {}; for (let i = 0; i < 100; i++) { window.parentObj[makeid()] = makeid(); }
Tests:
object.entries
const entries = Object.entries(window.parentObj).filter(([k, v], i) => ((i % 2) === 0)); if(entries.length > 0) Object.fromEntries(entries);
object.keys
const keys = Object.keys(window.parentObj).reduce((acc, k, i) => { if ((i % 2) === 0) { acc[k] = window.parentObj[k]; } return acc }, {}); if(Object.keys(keys).length > 0) true;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
object.entries
object.keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
object.entries
104901.9 Ops/sec
object.keys
169235.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Benchmark Definition** The benchmark is called "Object filter mappingv2" and it compares the performance of two approaches to create a new object by filtering out entries from an existing object: `Object.fromEntries` and `Object.keys.reduce`. **Approaches Compared** There are two main approaches being compared: 1. **`Object.entries`**: This approach uses the `entries()` method to get an array of key-value pairs from the object, filters out every other entry using a callback function, and then uses `Object.fromEntries` to create a new object from the filtered entries. 2. **`Object.keys.reduce`**: This approach uses the `keys()` method to get an array of keys from the object, reduces it to a new object using a callback function, and filters out every other entry. **Pros and Cons** 1. **`Object.entries`**: * Pros: concise and readable code. * Cons: might be slower due to the overhead of creating an array from key-value pairs. 2. **`Object.keys.reduce`**: * Pros: can be more efficient if the object is large, as it avoids creating an array. * Cons: might be less readable due to the use of `reduce`. **Library and Special JS Features** There are no external libraries used in this benchmark. **Special JS Features (Not Applicable)** None of the test cases use any special JavaScript features or syntax that would require additional explanation. **Other Considerations** The benchmark uses a custom object `window.parentObj` to create entries for the filter operation. This object is populated with random keys and values before running the benchmark. **Alternative Approaches** There are other ways to achieve the same result as the benchmark: 1. **Using `Object.fromEntries` with an array comprehension**: `const filteredEntries = Object.entries(window.parentObj).filter(([k, v], i) => (i % 2 === 0)); const result = Object.fromEntries(filteredEntries);` 2. **Using `Array.prototype.filter()` on the key-value pairs**: `const keys = Object.keys(window.parentObj).map(k => [k, window.parentObj[k]]); const filteredKeys = keys.filter(([k, v], i) => (i % 2 === 0)); const result = Object.fromEntries(filteredKeys.map(([k, v]) => [k, v]));` These alternative approaches might have different performance characteristics and are not included in the benchmark.
Related benchmarks:
Object values: Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS Object values: Object.entries loop for
Object filter mapping
Object filter mappingv3
Object.entries VS Object.keys VS Object.values length check
Comments
Confirm delete:
Do you really want to delete benchmark?