Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object filter: fromEntries vs reduce (with keys & entries)
(version: 0)
Comparing performance of:
Object.fromEntries vs Object.keys + reduce vs Object.entries + reduce
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var items = { ...Array.from(Array(10000).keys()) }; var itemsToFilter = Array.from(Array(10000).keys()).filter(v => v % 2 === 0);
Tests:
Object.fromEntries
var filteredObject = Object.entries(items).filter(([key]) => itemsToFilter.includes(key)); Object.fromEntries(filteredObject);
Object.keys + reduce
Object.keys(items).reduce((acc, key) => { if (itemsToFilter.includes(key)) { acc[key] = items[key]; } return acc; }, {});
Object.entries + reduce
Object.entries(items).reduce((acc, [key, entry]) => { if (itemsToFilter.includes(key)) { acc[key] = entry; } return acc; }, {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.fromEntries
Object.keys + reduce
Object.entries + reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that compares three approaches to filter an object using two popular methods: `Object.keys()`, `reduce()`, and `Object.fromEntries()` with or without the `entries` property. The goal is to determine which approach is faster for this specific use case. **Options Compared** Three options are being compared: 1. **Object.keys() + reduce()**: This option uses the `keys()` method to get an array of keys from the object, and then applies the `reduce()` method to filter and transform the data. 2. **Object.entries() + reduce()**: Similar to the previous option, but this time it uses the `entries()` method to get an array of key-value pairs from the object. 3. **Object.fromEntries()**: This option uses the `fromEntries()` method to create a new object from an iterable (in this case, the filtered key-value pairs). **Pros and Cons of Each Approach** Here's a brief summary of the pros and cons of each approach: 1. **Object.keys() + reduce()**: * Pros: Can be more efficient for large datasets since it only iterates over the keys. * Cons: May have performance issues when dealing with sparse objects or objects with many duplicate keys. 2. **Object.entries() + reduce()**: * Pros: Allows for more flexible filtering and transformation, as it directly works with key-value pairs. * Cons: May be slower than `keys()` due to the extra iteration over the key-value pairs. 3. **Object.fromEntries()**: * Pros: Provides a concise and readable way to create an object from an iterable. * Cons: May have performance issues when dealing with large datasets or objects, as it creates a new object copy. **Library** None of these approaches rely on specific libraries beyond the standard JavaScript built-in methods. However, `Object.fromEntries()` is a relatively recent addition to the ECMAScript standard (ES2022), so some older browsers might not support it. **Special JS Feature/Syntax** The benchmark does use some special JavaScript features: * **Spread operator (`...`)**: Used in the `itemsToFilter` variable initialization. * **Template literals (`\r\n`)**: Used for code formatting and readability. * **Arrow functions (`=>`)**: Used in the reduction callback function. These features are widely supported across modern browsers, but older versions might not support them. **Other Alternatives** Some alternative approaches that could be considered: * Using `Array.prototype.filter()` on an array of keys instead of `Object.keys() + reduce()` * Utilizing a library like Lodash or Ramda for filtering and mapping * Employing a different data structure, such as an object with numeric keys, to optimize the filtering process Keep in mind that these alternatives might have their own trade-offs in terms of performance, readability, and maintainability. I hope this explanation helps you understand the benchmark!
Related benchmarks:
Object.fromEntries on array vs reduce on array
Object filter: fromEntries vs reduce
Object.fromEntries vs reduce round 2
Object.fromEntries vs reduce for filtering keys
Comments
Confirm delete:
Do you really want to delete benchmark?