Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ES6 equivalent to lodash _.mapValues 213
(version: 0)
Comparing performance of:
native approach with reduce vs native approach fromEntries vs lodash mapValues vs myFOR
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)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
native approach with reduce
native approach fromEntries
lodash mapValues
myFOR
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 world of JavaScript microbenchmarks and explain what's being tested on MeasureThat.net. **Benchmark Definition** The benchmark definition is provided in JSON format, which includes: * `Name`: A unique name for the benchmark (in this case, "ES6 equivalent to lodash _.mapValues 213"). * `Description`: An optional description of the benchmark. * `Script Preparation Code`: The code that needs to be executed before running the benchmark. In this case, it's an empty string. * `Html Preparation Code`: A script tag that loads the core JavaScript library of Lodash (a popular utility library for JavaScript). **Test Cases** The benchmark consists of four test cases: 1. **Native approach with reduce**: This test uses the built-in `reduce()` method to iterate over the key-value pairs in an object and create a new object with only the values. 2. **Native approach fromEntries**: This test uses the `Object.fromEntries()` method to create a new object from an array of key-value pairs. 3. **Lodash mapValues**: This test uses the `mapValues()` function from Lodash to achieve the same result as the native approaches. 4. **MyFOR**: This test uses a simple `for` loop (yes, you read that right - JavaScript has a `for` loop!) to iterate over the key-value pairs in an object and create a new object with only the values. **Options Compared** The benchmark is comparing four different approaches to achieve the same result: * **Native approach with reduce**: Uses the built-in `reduce()` method. * **Native approach fromEntries**: Uses the `Object.fromEntries()` method. * **Lodash mapValues**: Uses a function from Lodash to achieve the same result. * **MyFOR**: Uses a simple `for` loop. **Pros and Cons of Each Approach** Here's a brief summary of each approach: 1. **Native approach with reduce**: * Pros: Fast, lightweight, and efficient. * Cons: May not be as readable or maintainable for complex transformations. 2. **Native approach fromEntries**: * Pros: Modern, efficient, and easy to read. * Cons: Only available in modern browsers and Node.js versions. 3. **Lodash mapValues**: * Pros: Highly optimized, stable, and widely supported. * Cons: Requires an external library (Lodash). 4. **MyFOR**: * Pros: Simple, easy to understand, and works in older browsers. * Cons: Slow, heavy-handed, and not very efficient. **Libraries Used** The benchmark uses Lodash, a popular utility library for JavaScript that provides various functions for data manipulation, iteration, and more. **Other Considerations** When choosing an approach, consider the following factors: * Performance: Native approaches (reduce and fromEntries) are generally faster than using an external library like Lodash. * Readability and maintainability: Modern native approaches (fromEntries) are often more readable and easier to maintain than using a `for` loop or an external library. * Browser support: If you need to support older browsers, consider the MyFOR approach. **Alternatives** If you're interested in exploring alternative approaches or testing other JavaScript libraries, some options include: * Using a different utility library (e.g., Moment.js for date manipulation) * Implementing your own custom transformation functions * Testing different optimization techniques (e.g., caching, memoization)
Related benchmarks:
lodash (v4.17.15) map vs Object.keys map
lodash _.map vs native map true version
Ladash map versus ECMA6 map
lodash v4.17.21 map vs Object.keys map
lodash map vs native map wraig0
Comments
Confirm delete:
Do you really want to delete benchmark?