Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash forEach vs for i loop mhai
(version: 0)
Comparing performance of:
lodash vs native vs native foreach
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
Script Preparation code:
var values = [{ a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }, { a: 30310 }, { b: 100303 }, { c: 3040494 }]
Tests:
lodash
var count = 0; _.forEach(values, function(v,i) { if (v.a != null) { count++; } })
native
var count = 0; const length = values.length for (var i = 0; i < length; ++i) { if (values[i].a != null) { count++; } }
native foreach
var count = 0; values.forEach(function(v,i) { if (v.a != null) { count++; } })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash
native
native 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 dive into the benchmarking results. **Benchmark Overview** The benchmark is designed to compare the performance of three approaches for iterating over an array of objects: 1. **Lodash `forEach`**: Uses the `forEach` method from the Lodash library. 2. **Native `for...of` loop**: Uses a traditional `for...of` loop to iterate over the array. 3. **Native `for` loop with index**: Uses a traditional `for` loop with an indexed iteration. **Options Compared** The benchmark compares the performance of each approach in the following scenarios: * Iterating over a large array of objects (10,000 elements) * Checking if each object's property (`a`) is not null **Pros and Cons of Each Approach** 1. **Lodash `forEach`**: * Pros: Convenient and concise syntax, handles asynchronous iteration. * Cons: Adds overhead due to the library, potentially slower than native loops. 2. **Native `for...of` loop**: * Pros: Fast and efficient, uses optimized JavaScript engine implementation. * Cons: May require more boilerplate code, less readable for some developers. 3. **Native `for` loop with index**: * Pros: Fast and efficient, easy to read and understand. * Cons: Requires manual indexing, which can be error-prone. **Benchmark Results** The latest benchmark results show that: 1. The native `for...of` loop is the fastest approach ( execution rate of 2886093 executions per second). 2. Lodash's `forEach` method is slower than the native loops but still relatively fast (execution rate of 716757 executions per second). **Conclusion** For this specific benchmark, the native `for...of` loop emerges as the winner due to its efficiency and readability. However, Lodash's `forEach` method may be a better choice in other scenarios where convenience and conciseness are more important than raw performance. Keep in mind that these results are specific to this particular benchmark and may vary depending on the specific use case, hardware, and JavaScript engine implementation.
Related benchmarks:
IndexOf vs Includes vs lodash includes 1231
50000 number array(natvie forEach vs lodash forEach)
lodash forEach vs for i loop modified
lodash forEach vs for i loop vs native forEach 123
Comments
Confirm delete:
Do you really want to delete benchmark?