Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash vs for-of vs forEach j
(version: 2)
Comparing performance of:
lodash.each vs native for-of w/ entries vs native forEach w/ entries vs vanilla for-loop w/ Object.keys vs vanilla for-loop w/ Object.entries vs native for-in
Created:
4 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) {const a = v + 'todo'})
native for-of w/ entries
for (const [k, v] of Object.entries(obj)) {}
native forEach w/ entries
Object.entries(obj).forEach(function(v, k) {})
vanilla for-loop w/ Object.keys
const keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) { const v = obj[keys[i]] + 'todo' }
vanilla for-loop w/ Object.entries
const entries = Object.entries(obj); for (let i = 0; i < entries.length; i++) { const [k, v] = entries[i] + 'todo' }
native for-in
for (const prop in obj) { if (obj.hasOwnProperty(prop)) { const v = obj[prop] + 'todo' } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
lodash.each
native for-of w/ entries
native forEach w/ entries
vanilla for-loop w/ Object.keys
vanilla for-loop w/ Object.entries
native 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 dive into the benchmark! **Benchmark Definition** The test case measures the performance of four different approaches to iterate over an object: 1. **Lodash `_.each()`**: Uses Lodash's `each` function to iterate over the object, modifying each value by appending `'todo'`. 2. **Native `for-of` with `Object.entries()`**: Iterates over the object using a `for-of` loop and accessing the values via `Object.entries()`, modifying each value by appending `'todo'`. 3. **Native `forEach` with `Object.entries()`**: Similar to the previous one, but uses the `forEach` method to iterate over the entries. 4. **Vanilla `for-loop` with `Object.keys()`**: Iterates over the object using a traditional `for` loop and accessing the values via `Object.keys()`, modifying each value by appending `'todo'`. 5. **Native `for-in`**: Iterates over the object's properties using the `for-in` loop, modifying each value by appending `'todo'`. **Library** The benchmark uses Lodash (version 4.17.5) as a library. Lodash is a popular utility library for JavaScript that provides a lot of functional programming helpers, such as array and object manipulation functions. In this case, the `_.each` function is used to iterate over the object. **Special JS features or syntax** None mentioned in the benchmark definition. **Options being compared** The benchmark compares the performance of five different approaches: 1. Lodash `_.each()` 2. Native `for-of` with `Object.entries()` 3. Native `forEach` with `Object.entries()` 4. Vanilla `for-loop` with `Object.keys()` 5. Native `for-in` **Pros and cons** Here's a brief summary of the pros and cons of each approach: 1. **Lodash `_.each()`**: * Pros: Simple to use, well-documented. * Cons: Requires Lodash library, might not be optimized for performance. 2. **Native `for-of` with `Object.entries()`**: * Pros: Fast and efficient, uses native JavaScript features. * Cons: Might require more complex code, not as widely supported as other approaches. 3. **Native `forEach` with `Object.entries()`**: * Pros: Similar to `for-of`, but might be slightly slower. * Cons: Same limitations as `for-of`. 4. **Vanilla `for-loop` with `Object.keys()`**: * Pros: Widely supported, easy to understand. * Cons: Might not be the fastest option, uses more memory (due to the `Object.keys()` lookup). 5. **Native `for-in`**: * Pros: Simple and efficient. * Cons: Not as widely supported as other approaches, might have issues with symbol properties. **Other alternatives** If you need to iterate over an object, here are some alternative approaches: 1. Use a `Map` instead of an object if you need to store key-value pairs. 2. Use the `Array.prototype.forEach()` method or the `Array.prototype.entries()` method on an array version of your data structure. 3. Consider using a library like Ramda or Immutable.js for functional programming. Remember, performance is often a trade-off between simplicity and optimization. The best approach depends on your specific use case and requirements.
Related benchmarks:
lodash vs for-of vs forEach
Loop over object: lodash vs Object.entries
lodash vs for-of vs forEach es6
lodash foreach vs for-of vs forEach
Loop over object: lodash vs Object.entries [2]
Comments
Confirm delete:
Do you really want to delete benchmark?