Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
mapvalues reduce
(version: 0)
Comparing performance of:
_.mapValues(collection, () => "a") vs Object.keys(collection).reduce( (acc, item) => ({ ...acc, [item]: "a", }), {} )
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
Script Preparation code:
var collection = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, ]
Tests:
_.mapValues(collection, () => "a")
_.mapValues(collection, () => "a")
Object.keys(collection).reduce( (acc, item) => ({ ...acc, [item]: "a", }), {} )
Object.keys(collection).reduce( (acc, item) => ({ ...acc, [item]: "a", }), {} )
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.mapValues(collection, () => "a")
Object.keys(collection).reduce( (acc, item) => ({ ...acc, [item]: "a", }), {} )
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):
I'd be happy to explain the benchmark and its options. **Overview** The provided JSON represents a JavaScript microbenchmark test case for the `mapValues` function from the Lodash library, as well as a custom implementation using the `reduce()` method with object destructuring. The test measures the execution speed of these two approaches on a large dataset. **Benchmark Definition** The benchmark definition is divided into two parts: 1. **Lodash `mapValues`**: The first part defines a test case for the Lodash `mapValues()` function, which applies a given value to each key-value pair in an object. In this case, the value is a function that returns the string `"a"`. 2. **Custom implementation using reduce() with object destructuring**: The second part defines a test case for a custom implementation of the same logic using the `reduce()` method and object destructuring. **Options Compared** The two options being compared are: 1. **Lodash `mapValues`**: This is the built-in function from the Lodash library, which provides a efficient and reliable way to map values in an object. 2. **Custom implementation using reduce() with object destructuring**: This is a custom implementation of the same logic, but using the `reduce()` method and object destructuring. **Pros and Cons** Here are some pros and cons of each option: **Lodash `mapValues`** Pros: * Fast execution speed * Efficient use of memory * Reliable and stable Cons: None mentioned in the benchmark definition. **Custom implementation using reduce() with object destructuring** Pros: * Portable across different environments (not limited to browsers) * Easy to understand and maintain Cons: * Potential performance overhead due to the use of `reduce()` and object destructuring * May not be as efficient as Lodash's internal implementation **Other Considerations** The benchmark definition mentions that the test uses a large dataset, which can impact execution speed. The choice of language and environment (in this case, JavaScript) also affects the results. It's worth noting that the custom implementation using `reduce()` with object destructuring is not a conventional way to achieve this result in JavaScript. The Lodash library provides an optimized implementation that takes care of many details, making it a reliable choice for performance-critical applications. **Other Alternatives** If you were to implement a similar benchmark for other functions or libraries, here are some alternatives to consider: * For mapping values in objects, you could use the `forEach()` method or a library like Ramda. * For performing aggregations or reducing data, you could use the `reduce()` method, but also explore libraries like Lodash or Underscore.js. Keep in mind that each implementation has its own trade-offs and advantages. It's essential to evaluate the specific requirements of your project and choose the best approach for performance, readability, and maintainability.
Related benchmarks:
unique elements in array using filter v2
unique elements in array using filter v2.3
unique elements in array using filter - large array
lodash flatmap long
Comments
Confirm delete:
Do you really want to delete benchmark?