Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Loop over object: lodash vs Object.entries [2]
(version: 0)
Comparing performance of:
lodash.each vs lodash.map vs Object.entries.map vs vanilla for-loop w/ Object.entries vs lodash.forEach
Created:
4 years ago
by:
Guest
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 = Array.from({ length: 10000 }).map((value, i) => i).reduce((val, v) => { val[v] = v; return val; }, {})
Tests:
lodash.each
_.each(obj, function(v, k) {})
lodash.map
_.map(obj, function(v, k) {})
Object.entries.map
Object.entries(obj).map(function([k, v]) {})
vanilla for-loop w/ Object.entries
const entries = Object.entries(obj); for (let i = 0; i < entries.length; i++) { const [k, v] = entries[i]; }
lodash.forEach
_.forEach(obj, function(v, k) {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
lodash.each
lodash.map
Object.entries.map
vanilla for-loop w/ Object.entries
lodash.forEach
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 provided benchmark and its test cases. **Benchmark Definition** The benchmark is testing the performance of different approaches to iterate over an object in JavaScript. The benchmark is comparing four methods: 1. `lodash.each` 2. `lodash.map` 3. `vanilla for-loop with Object.entries` (using a traditional for loop with `Object.entries`) 4. `Object.entries.map` **Benchmark Preparation Code** The script preparation code creates an array of 10,000 objects and reduces it to create an object with the same properties. The resulting object is then assigned to the `obj` variable. **Html Preparation Code** The HTML preparation code includes a script tag that loads the Lodash library (version 4.17.5) from a CDN. **Test Cases** Each test case represents a single benchmarking iteration: 1. **`lodash.each`**: Calls the `each` method on the `obj` object, passing a callback function that takes two arguments: `v` and `k`. 2. **`lodash.map`**: Calls the `map` method on the `obj` object, passing a callback function that returns a new value. 3. **`vanilla for-loop with Object.entries`**: Uses a traditional for loop to iterate over the properties of the `obj` object using `Object.entries`. 4. **`Object.entries.map`**: Calls the `map` method on the result of `Object.entries(obj)`, passing a callback function that takes two arguments: `[k, v]`. **Library** Lodash is a popular JavaScript library that provides functional programming utilities. In this benchmark, Lodash is used to provide `each`, `map`, and other utility functions. **Pros and Cons of Different Approaches** 1. **`lodash.each`**: Pros: * Optimized for iteration over objects. * Provides a simple and concise API. Cons: * Requires loading an additional library (Lodash). 2. **`lodash.map`**: Pros: * Returns a new array with mapped values. * Can be used to transform objects. Cons: * May not be suitable for iteration over large objects due to memory usage. 3. **`vanilla for-loop with Object.entries`**: Pros: * No additional library is required. * Allows for fine-grained control over loop logic. Cons: * Can be verbose and error-prone. 4. **`Object.entries.map`**: Pros: * Combines the benefits of `lodash.each` and `map`. Cons: * May still require loading an additional library (Lodash) if not already loaded. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax used in this benchmark. It relies solely on standard JavaScript features like objects, arrays, loops, and functions. **Other Alternatives** If you don't want to use Lodash or any other library for iteration over objects, you could consider using: * `Array.prototype.forEach()`: A built-in method that iterates over an array. * `for...of` loop: A new syntax for looping over arrays and objects introduced in ECMAScript 2015. Keep in mind that these alternatives may have different performance characteristics compared to the benchmarked methods.
Related benchmarks:
Loop over object: lodash vs Object.entries
Loop over object: lodash vs Object.entries 2
Loop over object: lodash vs Object.entries and Object.keys
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?