Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object Translate Comparison
(version: 0)
Mutation vs. reduce vs. fromEntries
Comparing performance of:
Mutate vs Reduce vs fromEntries
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var source = [...(new Array(1000))].map((v, i) => [`a${i}`, i]);
Tests:
Mutate
const output = {}; for (let i = 0; i < source.length; i++) { output[source[i][0]] = source[i][1]; } // Perform some work to prevent test optimizations Object.keys(output).length
Reduce
const output = source.reduce((acc, [key, val]) => ({ ...acc, [key]: val, }),{}); // Perform some work to prevent test optimizations Object.keys(output).length
fromEntries
const output = Object.fromEntries(source); // Perform some work to prevent test optimizations Object.keys(output).length
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Mutate
Reduce
fromEntries
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 provided JSON benchmark and explain what's being tested, compared, and its pros/cons. **Benchmark Definition** The test compares three different approaches to create an object from an array of key-value pairs: 1. **Mutation**: Creating an empty object (`output`) and then using a `for` loop to add key-value pairs to it. 2. **Reduce**: Using the `reduce()` method to accumulate an object from an array of key-value pairs. 3. **fromEntries**: Using the `Object.fromEntries()` method to create an object from an array of key-value pairs. **Options Compared** The three options are compared in terms of execution time, measured by the number of executions per second (`ExecutionsPerSecond`). **Pros and Cons** * **Mutation**: + Pros: Can be more intuitive for some developers, as it's a familiar pattern from working with objects. + Cons: May result in slower performance due to the need to update the object's properties. * **Reduce**: + Pros: Can be faster than Mutation, especially for larger arrays, since it uses a built-in method that can take advantage of optimizations. + Cons: Requires knowledge of the `reduce()` method and its behavior. * **fromEntries**: + Pros: Fast and efficient, as it's implemented in native code. + Cons: May require more mental overhead for developers unfamiliar with this specific method. **Library** There is no explicit library mentioned in the benchmark definition. However, `Object.fromEntries()` is a modern JavaScript method introduced in ECMAScript 2017 (ES7). It uses the Web API to create an object from an array of key-value pairs. **Special JS Feature or Syntax** None are explicitly used in this benchmark. **Other Alternatives** Other alternatives for creating objects from arrays of key-value pairs might include: * Using a library like Lodash's `fromPairs()` function * Implementing a custom solution using a `for...of` loop and object literal syntax For the Mutation approach, other alternatives might be: * Using a library like Underscore.js's `object.fromArrays()` function * Implementing a custom solution using a `for` loop and object literal syntax Keep in mind that these alternatives may have their own performance trade-offs or require additional dependencies. The benchmark test provides a useful way to compare the performance of different approaches to this common task, helping developers choose the most efficient method for their specific use case.
Related benchmarks:
reduce (immutable) vs map + fromEntries
Mutation vs non-mutation in reduce
Map & Object.fromEntries vs reduce
Object.fromEntries on array vs reduce on array
Comments
Confirm delete:
Do you really want to delete benchmark?