Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Loop over object: lodash vs Object.entries fork by d9k 2
(version: 0)
Comparing performance of:
lodash.each vs lodash.map vs Object.entries.map vs Object.entries + for vs Object.entries + for of vs foreach vs for..in
Created:
6 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 = 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]) {})
Object.entries + for
const entries = Object.entries(obj); for (let i = 0; i < entries.length; i++) { const [k, v] = entries[i]; }
Object.entries + for of
for (let [k, v] of Object.entries(obj)) { // }
foreach
Object.keys(obj).forEach(function (k) { let v = obj[k]; // });
for..in
for (const k in obj) { if (obj.hasOwnProperty(k)) { let v = obj[k]; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
lodash.each
lodash.map
Object.entries.map
Object.entries + for
Object.entries + for of
foreach
for..in
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 its various components. **Benchmark Overview** The test is designed to compare the performance of different approaches for iterating over an object in JavaScript. The object in question is created using `Array.from` and populated with 10,000 properties. **Test Cases** There are six test cases: 1. **lodash.each**: Uses Lodash's `each` function to iterate over the object. 2. **lodash.map**: Uses Lodash's `map` function to transform each value in the object. 3. **Object.entries + for**: Iterates over the object using `Object.entries` and a traditional `for` loop. 4. **Object.entries + for of**: Iterates over the object using `Object.entries` and the `for...of` loop. 5. **foreach**: Uses the `forEach` method to iterate over the object. 6. **for..in**: Iterates over the object using the `for...in` loop. **Library Used** The test case "lodash.each" uses Lodash, a popular JavaScript utility library that provides various functions for iterating and manipulating data. **Special Features or Syntax** None of the test cases use any special features or syntax beyond standard JavaScript. **Approach Comparison** Here's a brief overview of each approach: * **Lodash**: Uses its `each` and `map` functions, which are optimized for performance. These functions work by creating an intermediate array and then iterating over it. * **Object.entries + for**: Iterates directly over the object's properties using `Object.entries`. This approach is more efficient than using a traditional `for` loop or `forEach`. * **Object.entries + for of**: Similar to the previous approach, but uses the `for...of` loop instead. This approach is also optimized and provides good performance. * **foreach**: Uses the `forEach` method, which creates an intermediate array (in this case, the object itself). While not as efficient as the other approaches, it still provides decent performance. * **for..in**: Iterates directly over the object's properties using the `for...in` loop. This approach is less efficient than the others because it doesn't skip over deleted or undefined properties. **Pros and Cons** Here are some pros and cons for each approach: * **Lodash**: + Pros: Optimized functions, easy to use. + Cons: Adds external dependency (Lodash). * **Object.entries + for**: Pros: Fast, efficient, doesn't create intermediate arrays. Cons: May be less readable than traditional loops. * **Object.entries + for of**: Similar pros and cons to the previous approach. * **foreach**: Pros: Easy to use, no dependencies required. Cons: Creates an intermediate array, may not be as fast as other approaches. * **for..in**: Pros: Simple, minimal dependencies. Cons: Less efficient than other approaches. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * **Using `forEach` with a custom function**: Instead of using the `forEach` method, you can create a custom function that iterates over the object's properties. * **Using a traditional `for` loop**: You can use a traditional `for` loop to iterate over the object's properties, skipping deleted or undefined ones. * **Using `Array.prototype.forEach.call()`:** This approach is similar to using `forEach`, but it uses a different method to achieve the same result. I hope this helps you understand the benchmark and its various components!
Related benchmarks:
Loop over object: lodash vs Object.entries
Loop over object: lodash vs Object.entries fork by d9k 3
Loop over object: lodash vs Object.entries and Object.keys
Loop over object: lodash vs Object.entries [2]
Comments
Confirm delete:
Do you really want to delete benchmark?