Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash mapValues vs vanilla Object.keys foreach vs lodash reduce
(version: 2)
Comparing performance of:
lodash mapValues vs vanilla object.keys vs lodash reduce
Created:
3 years ago
by:
Registered User
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>
Script Preparation code:
var obj = {} for (let i = 0; i < 1000; ++i) { obj[`a${i}`] = i; } var transform = (n) => n * n + 1;
Tests:
lodash mapValues
const newObj = _.mapValues(obj, (v, k) => transform(v));
vanilla object.keys
const newObj = {}; Object.keys(obj).forEach((key) => { newObj[key] = transform(obj[key]); });
lodash reduce
const newObj = _.reduce(obj, (acc, val, key) => { acc[key] = transform(val); return acc; })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash mapValues
vanilla object.keys
lodash reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash mapValues
8929.6 Ops/sec
vanilla object.keys
9593.5 Ops/sec
lodash reduce
2118.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The benchmark is designed to compare three approaches for transforming data in JavaScript: 1. `lodash mapValues` 2. Vanilla `Object.keys` with a `forEach` loop 3. `lodash reduce` **Options Compared** The benchmark compares the performance of these three approaches on a dataset that consists of 1000 objects, each with an "a" key containing a unique integer value. **Approach 1: Lodash `mapValues`** * Purpose: To create a new object by mapping over the original object's keys and values. * Implementation: The benchmark uses the `_mapValues` function from Lodash, which takes two arguments: the source object (`obj`) and a transform function (`transform`). It returns a new object with transformed values. Pros: * Concise and readable implementation * Optimized for performance by leveraging Lodash's optimized caching mechanism Cons: * Adds an external dependency (Lodash) that may not be present in all environments * May have slower startup times due to the need to load Lodash **Approach 2: Vanilla `Object.keys` with a `forEach` loop** * Purpose: To create a new object by iterating over the keys of the original object and assigning transformed values. Pros: * No external dependencies required * Fast startup times since no additional libraries are loaded Cons: * More verbose implementation compared to `mapValues` * Less efficient than `mapValues` due to the need for an explicit loop **Approach 3: Lodash `reduce`** * Purpose: To create a new object by reducing the original object's values using a transform function. Pros: * Similar to `mapValues`, but with additional flexibility in terms of accumulator and callback functions * Still optimized for performance due to Lodash's caching mechanism Cons: * May have slower performance compared to `mapValues` due to the additional complexity of the reducer function **Library: Lodash** Lodash is a popular JavaScript utility library that provides a collection of high-quality, consistently-tested functions for various tasks. In this benchmark, `_mapValues` and `_reduce` are used to transform data efficiently. **Special JS Feature/Syntax** There doesn't appear to be any special JavaScript features or syntax being tested in this benchmark. However, if there were, it would likely involve something like ES6 arrow functions (`=>`) or template literals (`\${}`), which are not relevant to the implementation details of these three approaches. **Other Alternatives** If you wanted to implement an alternative approach to transforming data, some possible options might include: * Using `Array.prototype.map()` and `Object.fromEntries()` * Utilizing a custom iterative approach with loops and caching * Leveraging a third-party library like Immutable.js Keep in mind that each of these alternatives would have their own trade-offs in terms of performance, readability, and maintainability.
Related benchmarks:
lodash vs for-of vs forEach5453
lodash vs for-of vs forEach vs map v2
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values
Loop over object: lodash.forOwn vs Object.keys().forEach
Comments
Confirm delete:
Do you really want to delete benchmark?