Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash forEach vs for i loop
(version: 0)
Comparing performance of:
lodash.forEach vs native
Created:
7 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}]
Tests:
lodash.forEach
var count = 0; _.forEach(values, function(v,i) { if (v.a != null) { count++; } })
native
var count = 0; for (var i = 0; i < values.length; i++) { if (values[i].a != null) { count++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash.forEach
native
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash.forEach
39191400.0 Ops/sec
native
61433908.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the JavaScript benchmark and its various components. **Benchmark Overview** MeasureThat.net is a website that allows users to create and run microbenchmarks in JavaScript. The goal of these benchmarks is to compare the performance of different approaches to achieve a specific task, such as iterating over an array of objects. **Script Preparation Code** The script preparation code is a snippet of JavaScript that sets up the environment for the benchmark: ```javascript var values = [{a: 30310}, {b: 100303}, {c: 3040494}]; ``` This code creates an array `values` with three objects, each containing a property `a`. The purpose of this script is to provide a fixed dataset that will be used in the benchmark. **Html Preparation Code** The HTML preparation code includes a reference to the Lodash library: ```html <script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script> ``` Lodash (a popular utility library for JavaScript) provides various functions that can be used in the benchmark, including `forEach`. **Individual Test Cases** There are two test cases: 1. **Lodash `forEach`**: This test case uses the Lodash `forEach` function to iterate over the `values` array: ```javascript _.forEach(values, function(v,i) { if (v.a != null) { count++; } }) ``` The purpose of this test is to measure the performance of using the Lodash `forEach` function. 2. **Native `for` loop**: This test case uses a traditional `for` loop to iterate over the `values` array: ```javascript for (var i = 0; i < values.length; i++) { if (values[i].a != null) { count++; } } ``` The purpose of this test is to measure the performance of using a traditional `for` loop. **Benchmark Results** The latest benchmark results show two tests running in Chrome 128 on a desktop with Windows: 1. **Lodash `forEach`**: This test case executed approximately 7,620,873 times per second. 2. **Native `for` loop**: This test case executed approximately 4,430,573 times per second. **Library: Lodash** Lodash is a popular utility library for JavaScript that provides various functions for tasks such as string manipulation, array manipulation, and more. In this benchmark, Lodash's `forEach` function is used to iterate over the `values` array. **Special JS Feature/ Syntax**: None mentioned in this explanation. **Other Alternatives** If you wanted to test alternative approaches to iterating over an array of objects, some other options might include: * Using a `for...in` loop * Using a `while` loop with an index variable * Using a library like Moment.js (which provides a `forEach` function) * Implementing a custom iteration function using a recursive or iterative approach Keep in mind that the performance of these alternatives may vary depending on the specific use case and JavaScript environment.
Related benchmarks:
lodash.each vs Object.forEach vs Native for
Lodash foreach vs native foreach
lodash forEach vs for i loop modified
lodash .foreach vs native foreach vs native forof
lodash .foreach vs native foreach vs native for loop
Comments
Confirm delete:
Do you really want to delete benchmark?