Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object filter: fromEntries vs reduce
(version: 0)
Comparing performance of:
Object.fromEntries vs 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);
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 (2)
Previous results
Fork
Test case name
Result
Object.fromEntries
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 analyze what is being tested. **Benchmark Purpose:** The benchmark compares two approaches for filtering an object using JavaScript: 1. `Object.fromEntries(filteredObject)` 2. `reduce()` **Options Compared:** * **`Object.fromEntries(filteredObject)`**: This approach uses the `fromEntries()` method, which creates a new object from an array of key-value pairs. In this case, it's used to filter and create a new object from the filtered entries. * **`reduce()```**: This approach uses the `reduce()` method, a common way to iterate over arrays in JavaScript. Here, it's used to iterate over the entries and create a new object with only the desired keys. **Pros and Cons of Each Approach:** * **`Object.fromEntries(filteredObject)`**: * Pros: * More concise and readable code * Easier to understand for those familiar with `fromEntries()` * Cons: * Might have performance issues if the filtered object is large, as it creates a new object. * **`reduce()`**: * Pros: * Performance-friendly approach, suitable for large datasets * Can be more efficient due to its built-in optimization * Cons: * Might require more lines of code, making it less readable **Library Used:** There is no specific library used in this benchmark. The `Object.entries()` and `reduce()` methods are part of the standard JavaScript API. **Special JS Features/Syntax:** * None mentioned explicitly. However, keep in mind that using `Object.fromEntries()` is a relatively new feature introduced in ECMAScript 2019. **Benchmark Preparation Code:** The preparation code creates an array of keys (`itemsToFilter`) and another array with the same keys but filtered to only include even indices (`items`). The original object (`items`) contains these key-value pairs. This setup allows for a controlled environment to compare the two filtering approaches. **Alternatives:** * Other ways to filter an object or create a new one from entries could be explored, such as using `forEach()` or other iteration methods. * Performance-critical applications might also consider using alternative data structures or libraries optimized for efficient filtering and aggregation.
Related benchmarks:
Object.fromEntries on array vs reduce on array
Filter and Map vs Reduce
Object filter: fromEntries vs reduce (with keys & entries)
Object.fromEntries vs reduce round 2
Comments
Confirm delete:
Do you really want to delete benchmark?