Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.entries reduce vs lodash mapValues
(version: 1)
Comparing performance of:
object.entries reduce vs lodash mapValues
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Tests:
object.entries reduce
var fruits= { 'A': { 'name': 'apple', 'number': 5}, 'O': { 'name': 'orange', 'number': 10 }, 'B': { 'name': 'banana', 'number': 15 }, }; console.log( Object.entries(fruits).reduce( (acc, [key, value]) => { return { ...acc, [key]: value.name }; }, {} ) );
lodash mapValues
var fruits= { 'A': { 'name': 'apple', 'number': 5}, 'O': { 'name': 'orange', 'number': 10 }, 'B': { 'name': 'banana', 'number': 15 }, }; console.log( _.mapValues( fruits, (fruit) => ({ name: fruit.name }) ) );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
object.entries reduce
lodash mapValues
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
object.entries reduce
491362.4 Ops/sec
lodash mapValues
530627.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined here compares two different approaches to transforming an object of fruit data into a new object that maps fruit keys to their respective names. The two methods being tested are: 1. **Using `Object.entries` with `reduce`:** - This method leverages JavaScript's built-in `Object.entries()` function, which converts an object's properties into an array of key-value pairs. It then uses the `reduce()` method to create a new object by iterating over these pairs and building an object with the fruit names as values for each fruit key. **Pros:** - Native to JavaScript: No dependencies are required, making it lightweight and easy to incorporate. - Clear logic: The method utilizes standard JavaScript functions, making it easily understandable for developers familiar with JS. **Cons:** - Potentially less optimized for performance: Manual handling of object accumulation through `reduce` might not be as fast as optimized functions in a library, depending on the JavaScript engine. 2. **Using Lodash's `mapValues`:** - This method uses the Lodash library, a popular utility library for JavaScript that simplifies tasks around collections, arrays, and objects. The `mapValues` function applies a function to each value of the object, returning a new object with the original keys but with the modified values. **Pros:** - Performance optimizations: Lodash functions are often optimized for performance and could potentially execute faster than similar native methods under certain circumstances. - Functionality: Offers additional features and chainable syntax, which can make complex transformations easier to write and understand. **Cons:** - Additional library dependency: Using Lodash requires including the library via a script tag, which adds an overhead in terms of file size and loading time. - Learning curve: Developers may need to familiarize themselves with Lodash's API and syntax, which could add complexity for teams not accustomed to using this library. ### Benchmark Results According to the benchmark results, the `lodash mapValues` function achieved approximately **530,628 executions per second**, while the `Object.entries reduce` method achieved around **491,362 executions per second**. Therefore, `mapValues` performed better in this specific test case in the given environment. ### Other Considerations and Alternatives Developers might consider a few other alternatives when transforming objects in JavaScript: - **Using `for...in` or `forEach`:** Instead of `reduce`, one could use a `for...in` loop or `Object.keys()` with `forEach`. These might provide a clearer semantic meaning in certain situations, though they typically wouldn't have the automatic accumulation feature of `reduce`. - **Native `Object.fromEntries`:** For recent JavaScript environments, the `Object.fromEntries()` method could be another alternative. Combined with `Object.entries()`, it can provide a succinct way of achieving a similar transformation without needing `reduce`. - **Functional programming libraries:** Besides Lodash, other libraries like Ramda might offer similar functionalities, benefiting from currying and better composition, particularly in more complex scenarios. In conclusion, the choice between these methods often depends on the specific performance needs, team familiarity with libraries like Lodash, and the balance between readability and efficiency for the task at hand.
Related benchmarks:
ES6 equivalent to lodash _.mapValues
ES6 equivalent to lodash _.mapValues test
ES6 equivalent to lodash _.mapValues without console.log
ES6 equivalent to lodash _.mapValues 3
lodash reduce vs. mapValues
ES6 equivalent to lodash _.mapValues 123
ES6 equivalent to lodash _.mapValues 213
ES6 equivalent to lodash _.mapValues 2132
ES6 equivalent to lodash _.mapValues 21322
Comments
Confirm delete:
Do you really want to delete benchmark?