Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash v native for/each
(version: 6)
Comparing performance of:
lodash each vs native forEach vs Native for in Object.entries vs Native for of Object.entries
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Script Preparation code:
var obj = [{a: 3120}, {b: 133303}, {c: 3022455}, {d: 6542322}, {e: 12443521}]
Tests:
lodash each
_.each(obj, function(a,b) {console.log(a)})
native forEach
obj.forEach(function(a,b) {console.log(a)})
Native for in Object.entries
for (const [a, b] in Object.entries(obj)) { console.log(`${a}: ${b}`); }
Native for of Object.entries
for (const [a, b] of Object.entries(obj)) { console.log(`${a}: ${b}`); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash each
native forEach
Native for in Object.entries
Native for of Object.entries
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'll break down the provided benchmark and explain what's being tested, the different approaches compared, their pros and cons, and other considerations. **Benchmark Context** The `MeasureThat.net` website allows users to create and run JavaScript microbenchmarks. In this case, we have a benchmark that compares the performance of `lodash` (a popular utility library) with native JavaScript implementations for `forEach` and `for...of` loops. **Script Preparation Code** ```javascript var obj = [{a: 3120}, {b: 133303}, {c: 3022455}, {d: 6542322}, {e: 12443521}]; ``` This code defines an array `obj` containing five objects with different properties. **Html Preparation Code** ```html <script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script> ``` This line includes the `lodash` library from a CDN, which will be used in the benchmark tests. **Test Cases** There are four test cases: 1. **Lodash `forEach`**: `_.each(obj, function(a,b) {console.log(a)})` 2. **Native `forEach`**: `obj.forEach(function(a,b) {console.log(a)})` 3. **Native `for...in` loop**: `for (const [a, b] in Object.entries(obj)) {\r\nconsole.log(`${a}: ${b}`);\r\n}` 4. **Native `for...of` loop**: `for (const [a, b] of Object.entries(obj)) {\r\n console.log(`${a}: ${b}`);\r\n}`` Each test case uses a different approach to iterate over the `obj` array and log each property. **Library and Syntax** In this benchmark, `lodash` is used as a utility library for its `each` function. The `forEach` method is also native in JavaScript, so there's no need for a third-party library. There are no special JavaScript features or syntax being tested; the focus is on comparing performance of different iteration approaches. **Performance Comparison** The benchmark measures the execution speed (in executions per second) of each test case using different browsers and devices. The results show that: * Native `forEach` loop outperforms both `lodash` and native `for...of` loops. * Native `for...of` loop is slower than native `forEach` loop but still faster than `lodash`'s `each`. * Native `for...in` loop is the slowest of all. **Other Alternatives** If you were to implement a benchmark similar to this, here are some alternatives: 1. **Use a testing framework**: Instead of using JavaScript's built-in timing functions, consider using a testing framework like Jest or Mocha, which provide more robust and reliable ways to write benchmarks. 2. **Add more test cases**: Consider adding more test cases to compare other iteration approaches, such as `for...of` loops with different array iterators (e.g., `Map`, `Set`, or custom iterators). 3. **Use a more comprehensive benchmarking library**: Libraries like `benchmark.js` provide more features and options for writing high-quality benchmarks. 4. **Consider parallelization**: If you're running your benchmark on multiple CPU cores, consider using parallelization techniques to speed up the benchmarking process. I hope this explanation helps!
Related benchmarks:
lodash for-in vs native for-in (lodash version: 4.17.10)
lodash .forEach vs JS forEach
lodash each & lodash map & native forEach
Lodash vs Native v3,0,0
JS ForEach Tests
Comments
Confirm delete:
Do you really want to delete benchmark?