Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object reduction
(version: 0)
Comparing performance of:
Object.entries.reduce vs Spread/delete vs Object.entries.filter
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = { ...Array.from(Array(10000).keys()) }; var idsToRemove = [1, 47, 420, 421, 69, 1000, 2000, 2001, 8765, 10000];
Tests:
Object.entries.reduce
Object.entries(data).reduce((acc, [k, v]) => { if (idsToRemove.includes(k)) { acc[k] = v; } return acc; }, {});
Spread/delete
var newObjs = { ...data }; idsToRemove.forEach((id) => { delete newObjs[id]; });
Object.entries.filter
Object.entries(data).filter(([k, v]) => { !idsToRemove.includes(k); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.entries.reduce
Spread/delete
Object.entries.filter
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; rv:129.0) Gecko/20100101 Firefox/129.0
Browser/OS:
Firefox 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.entries.reduce
3020.4 Ops/sec
Spread/delete
1861.1 Ops/sec
Object.entries.filter
3023.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its options. **Benchmark Definition** The benchmark measures the performance of three different approaches to filter an object: 1. `Object.entries(data).filter(([k, v]) => { !idsToRemove.includes(k); });` 2. `var newObjs = { ...data };;` followed by `idsToRemove.forEach((id) => { delete newObjs[id]; };` 3. `Object.entries(data).reduce((acc, [k, v]) => { if (idsToRemove.includes(k)) { acc[k] = v; } return acc; }, {});` **Options Compared** The benchmark compares the performance of three different approaches: * **Filter**: Using the `filter()` method to create a new array with only the elements that pass the test. * **Spread and delete**: Using the spread operator (`{ ...data }`) to create a new object, followed by iterating over the `idsToRemove` array using `forEach()` and deleting each ID from the new object. * **Reduce**: Using the `reduce()` method to iterate over the object entries and keep only the elements that are not in the `idsToRemove` array. **Pros and Cons of Each Approach** 1. **Filter**: * Pros: Easy to read, concise, and well-supported by most browsers. * Cons: Creates a new array, which can be memory-intensive for large datasets. 2. **Spread and delete**: * Pros: Does not create a new object or array, making it potentially more efficient in terms of memory usage. * Cons: More verbose than the filter approach, and requires manual iteration over the `idsToRemove` array. 3. **Reduce**: * Pros: Can be more memory-efficient than filter, as it only creates a new accumulator object. * Cons: Less intuitive for some developers, especially those not familiar with reduce(). **Library Used** There is no specific library used in this benchmark. The approaches use built-in JavaScript methods and operators. **Special JS Feature or Syntax** None of the approaches in this benchmark rely on any special JavaScript features or syntax beyond what is considered standard in modern JavaScript. **Other Alternatives** For filtering an object, you could also consider using: * `Object.fromEntries()`: A newer method that creates a new object from an array of key-value pairs. * `Array.prototype.filter.call()`: This can be used to filter an object's entries by casting the object as an array. Keep in mind that the performance differences between these approaches will depend on the specific use case and requirements.
Related benchmarks:
Map convert
Object.fromEntries vs reduce round 2
Object reduction with conversion back
Object reduction with conversion back (smol)
Comments
Confirm delete:
Do you really want to delete benchmark?