Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ES6 equivalent to lodash _.mapValues 2132
(version: 0)
Comparing performance of:
native approach with reduce vs native approach fromEntries vs lodash mapValues vs myFOR vs myFOR2
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://raw.githubusercontent.com/lodash/lodash/4.17.15-npm/core.js"></script>
Tests:
native approach with reduce
var fruits= { 'apple': { 'name': 'apple', 'number': 5}, 'orange': { 'name': 'orange', 'number': 10 } }; console.log(Object.entries(fruits) .reduce((a, [key, { number }]) => { a[key] = number; return a; }, {} ));
native approach fromEntries
var fruits= { 'apple': { 'name': 'apple', 'number': 5}, 'orange': { 'name': 'orange', 'number': 10 } }; console.log(Object.fromEntries( Object.entries(fruits).map(([key, { number }]) => [key, number]) ));
lodash mapValues
var fruits= { 'apple': { 'name': 'apple', 'number': 5}, 'orange': { 'name': 'orange', 'number': 10 } }; console.log(_.mapValues(fruits, 'number'))
myFOR
var fruits= { 'apple': { 'name': 'apple', 'number': 5}, 'orange': { 'name': 'orange', 'number': 10 } }; var result = {} for (key in fruits) { result[key] = fruits[key].number } console.log(result)
myFOR2
var fruits= { 'apple': { 'name': 'apple', 'number': 5}, 'orange': { 'name': 'orange', 'number': 10 } }; var result = {} for (key in fruits) { fruits[key] = fruits[key].number } console.log(fruits)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
native approach with reduce
native approach fromEntries
lodash mapValues
myFOR
myFOR2
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 dive into the explanation. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark test case, which measures the performance of different approaches to achieve a specific task: transforming an object with nested values from a key-value pair format to an object where each value is mapped to its corresponding key. **Options Compared** There are four options compared in this benchmark: 1. **Native Approach with Reduce**: This approach uses the `reduce()` method, which applies a function against an accumulator and each element in the array (in this case, `Object.entries(fruits)`), accumulating a result. 2. **Native Approach fromEntries**: This approach uses the `fromEntries()` method, which creates a new object from an iterable of key-value pairs generated by `Object.entries(fruits)`. 3. **Lodash `mapValues`**: This approach uses the `mapValues()` function from the Lodash library to transform the input object. 4. **Custom Approach with FOR loop (`myFOR` and `myFOR2`)**: These two approaches use a traditional FOR loop to iterate over the key-value pairs of the input object. **Pros and Cons** Here's a brief summary of each approach: 1. **Native Approach with Reduce**: * Pros: Native JavaScript functions, no external dependencies. * Cons: Can be less readable for complex transformations, may not perform as well as native array methods in some cases. 2. **Native Approach fromEntries**: * Pros: More concise and expressive than `reduce()`, can be faster due to optimized implementations. * Cons: Requires modern JavaScript features (ES6+), may not work in older browsers or environments. 3. **Lodash `mapValues`**: * Pros: Convenient, readable, and maintainable, especially for complex transformations. * Cons: External dependency on Lodash library, may add overhead due to function call and object creation. 4. **Custom Approach with FOR loop (`myFOR` and `myFOR2`)**: * Pros: No external dependencies, straightforward implementation. * Cons: Can be verbose and error-prone, may not perform as well as native array methods. **Library: Lodash** Lodash is a popular JavaScript library that provides a collection of reusable functions for common tasks, such as array manipulation, object transformation, and utility functions. In this benchmark, the `mapValues()` function from Lodash is used to transform the input object. **Special JS Feature/Syntax (None)** There are no special JavaScript features or syntax mentioned in the provided code snippets that would require specific handling or explanations. **Other Alternatives** If you want to explore alternative approaches or optimize your own implementation, consider: * Using `forEach()` or `for...of` loops instead of traditional FOR loops. * Utilizing modern JavaScript features like `Promise.all()`, `async/await`, or `Web Workers` for parallel processing. * Leveraging third-party libraries or frameworks that provide optimized array and object manipulation functions (e.g., Ramda, Underscore.js). Feel free to ask if you have any specific questions about this explanation!
Related benchmarks:
lodash (v4.17.15) map vs Object.keys map
lodash map performanc vs es6
lodash _.map vs native map true version
Ladash map versus ECMA6 map
lodash v4.17.21 map vs Object.keys map
Comments
Confirm delete:
Do you really want to delete benchmark?