Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
object.entries vs reduce
(version: 0)
Benchmark.js
Comparing performance of:
Entries vs reduce
Created:
2 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var data = { ...Array.from(Array(10000).keys()) };
Tests:
Entries
const entries = [] for (const key of Object.keys(data)) { if (data[key] % 2 === 0) { entries.push([key, key.toString()]) } } Object.fromEntries(entries)
reduce
Object.keys(data) .reduce((ret, key) => { if (data[key] % 2 === 0) { ret[key] = key.toString(); } return ret }, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Entries
reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
reduce
715/s
Entries
535/s
View exact numbers
Test name
Executions per second
✓
reduce
715 Ops/sec
Entries
535 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches to iterate over an object's entries: using `Object.entries()` with `Array.from()` and `reduce()`, and using `Object.keys()` with `reduce()`. **Options Compared** Two options are compared: 1. **`Object.entries()` with `Array.from()` and `reduce()`**: This approach uses the `entries()` method to get an array of key-value pairs, converts it to an array using `Array.from()`, and then uses `reduce()` to iterate over the values. 2. **`Object.keys()` with `reduce()`**: This approach uses the `keys()` method to get an array of keys, and then uses `reduce()` to iterate over the keys. **Pros and Cons** * **`Object.entries()` with `Array.from()` and `reduce()`**: + Pros: more explicit and readable code; avoids the need for manual iteration. + Cons: may be slower due to the overhead of converting an array to a new array using `Array.from()`. * **`Object.keys()` with `reduce()`**: + Pros: potentially faster, as it avoids the conversion step. + Cons: less explicit and readable code; requires manual iteration. **Library** None. **Special JS Feature or Syntax** The benchmark does not use any special JavaScript features or syntax. **Test Users Special Case** None. **Other Considerations** The benchmark is likely designed to measure the performance of these two approaches in a general case, without considering edge cases or specific requirements. The test data is a simple object with 10,000 keys, and the benchmark measures the number of executions per second for each approach. **Alternatives** Some alternative approaches could be: * Using `for...in` to iterate over the object's entries. * Using a library like Lodash to simplify the iteration code. * Using a specialized iterator or generator function to iterate over the object's entries. However, these alternatives are not part of the current benchmark.
Related benchmarks
Object.fromEntries vs create temp object vs Array.reduce
Map & Object.fromEntries vs reduce
Object.fromEntries on array vs reduce on array
Object.fromEntries vs reduce v21
Object.fromEntries vs reduce round 2
Comments
Confirm delete:
Do you really want to delete benchmark?