Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.map over object vs Object.values.map
(version: 0)
Comparing performance of:
_.map vs Object.values.map
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>
Script Preparation code:
var obj = {}; for (let i = 0; i < 500; i++) { obj[i.toString()] = i; }
Tests:
_.map
var result = _.map(obj, (x) => x * x);
Object.values.map
var result = Object.values(obj).map((x) => x * x);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.map
Object.values.map
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 break down the benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark compares two approaches to iterate over an object: 1. Using `_.map` from Lodash (a JavaScript library for functional programming utilities). 2. Using the `Object.values()` method in combination with the `map()` method. **Lodash `_` namespace** The `_` namespace is a common convention in Node.js and other JavaScript environments to access functions from the Lodash library. It's similar to how you might import a module using a relative path, like `const _ = require('lodash');`. **Options Compared** The two options being compared are: 1. **_.map(obj, (x) => x * x)**: This uses the `_` namespace to access the `map()` function from Lodash. The callback function takes each value `x` and returns its square (`x * x`). The result is stored in the `result` variable. 2. **Object.values(obj).map((x) => x * x)**: This uses the built-in `Object.values()` method to get an array of object values, which are then passed to the `map()` method. Each value is processed by squaring it (`x * x`). The result is also stored in the `result` variable. **Pros and Cons** 1. **_.map(obj, (x) => x * x)**: * Pros: Lodash provides a lightweight and efficient way to perform functional programming operations. * Cons: The benchmark may be skewed by the presence of the `_` namespace, which might not be present in all environments. 2. **Object.values(obj).map((x) => x * x)**: * Pros: This approach is more straightforward and doesn't rely on an external library. * Cons: It requires accessing the `Object.values()` method, which may incur additional overhead due to its implementation. **Other Considerations** 1. **Type checking**: The Lodash implementation uses a more relaxed type system, which might lead to slower performance in some cases. The built-in `map()` method, on the other hand, performs stricter type checking. 2. **Optimization**: The built-in `Object.values()` method is optimized for performance and may use caching or other internal optimizations that aren't present in the Lodash implementation. **Device Platform and Browser** The benchmark was run on a Windows Desktop with Chrome 88 browser, which might not be representative of all possible environments. The results should be interpreted with caution when considering cross-platform compatibility. **Other Alternatives** If you want to explore other approaches or libraries for functional programming in JavaScript, some alternatives include: * Vanilla JavaScript (e.g., using `forEach()`, `reduce()`, and other methods) * Other popular libraries like Ramda, Underscore.js, or ES7+ syntax features (e.g., `for...of` loops, arrow functions)
Related benchmarks:
lodash.each vs Object.forEach
Loop over object: lodash vs Object.entries and Object.keys
Loop over object: lodash vs Object.entries vs Object.values vs Object.keys (lodash 4.17.15)
lodash mapValues vs vanilla Object.keys foreach vs lodash reduce
Comments
Confirm delete:
Do you really want to delete benchmark?