Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce vs simple assignment vs Object.fromEntries
(version: 0)
Comparing performance of:
Reduce vs Simple assignment vs From Entries
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function createArray(propsCount) { const arr = [] for (let i = 0; i < propsCount; i += 1) { arr.push({ active: i % 5 === 0 }) } return arr } var users = createArray(1000)
Tests:
Reduce
users.reduce((acc, curr) => { if (curr.active) { return acc } return { ...acc, [curr.id]: curr.name } })
Simple assignment
let result = {} users.forEach((user, i) => result[user.id] = user.name)
From Entries
const a = users.filter((user) => !user.active).map((user) => [user.id, user.name]) Object.fromEntries(a)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Reduce
Simple assignment
From Entries
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/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Reduce
24859.7 Ops/sec
Simple assignment
215184.8 Ops/sec
From Entries
28617.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark tests three different approaches to create an object with user data: 1. `Object.fromEntries`: This method creates a new object from an array of key-value pairs. 2. Simple assignment: Assigning properties to an empty object using the dot notation (`result[user.id] = user.name`). 3. `reduce()`: A built-in JavaScript function that reduces an array to a single value, creating an object in this case. **Options Compared** The benchmark compares the performance of these three approaches: * `Object.fromEntries` * Simple assignment * `reduce()` **Pros and Cons of Each Approach** 1. **`Object.fromEntries`**: * Pros: Efficient, concise, and readable. * Cons: May have overhead due to its creation, especially for large datasets. 2. **Simple Assignment**: * Pros: Fast, straightforward, and easy to understand. * Cons: Can be slower than `Object.fromEntries`, as it involves property lookup and assignment, which can lead to performance issues in some cases. 3. **`reduce()`**: * Pros: Flexible, allows for more complex transformations, and can be more efficient for large datasets. * Cons: May have a higher overhead due to its creation, especially if the accumulator is not properly optimized. **Library Used** None, as this benchmark only relies on built-in JavaScript features. **Special JS Feature or Syntax** None mentioned in the provided information. However, it's worth noting that `Object.fromEntries()` is a relatively new feature introduced in ECMAScript 2019 (ES10). If you're targeting older browsers or environments, you may need to use alternative methods. **Other Alternatives** If you prefer not to use `Object.fromEntries()`, you can create an object with key-value pairs using other methods: * `Array.prototype.reduce()` without the accumulator (e.g., `users.forEach((user) => result[user.id] = user.name)`): This approach is similar to simple assignment but uses a more functional programming style. * Using a library like Lodash or Underscore.js, which provide utilities for creating objects from arrays. Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to the original approaches being tested.
Related benchmarks:
Different ways of writing Array.reduce
Array.reduce2
Object.fromEntries vs create temp object vs Array.reduce
JavaScript reduce vs Object.assign performance
JavaScript reduce vs Object.assign performance v2
Comments
Confirm delete:
Do you really want to delete benchmark?