Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Underscore.js _forEach() vs Native for loop for large arrays
(version: 0)
This will compare the native for loop speed vs the underscore _forEach() on a relatively large array.
Comparing performance of:
Underscore.js _forEach() vs native for loop
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/underscore@1.13.6/underscore-umd-min.js"></script>
Script Preparation code:
var values = [{a: 30310}, {b: 100303}, {c: 3040494}]
Tests:
Underscore.js _forEach()
var count = 0; _.forEach(values, function(v,i) { if (v.a != null) { count++; } })
native for loop
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
Underscore.js _forEach()
native for loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Underscore.js _forEach()
63327216.0 Ops/sec
native for loop
132282104.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its results. **Benchmark Definition** The benchmark compares the performance of two approaches: 1. **Underscore.js `_forEach()`**: This test uses the `_.forEach` function from the Underscore.js library to iterate over an array of objects. The purpose of this function is to execute a callback function once for each element in the array. 2. **Native For Loop**: This test uses a traditional for loop to iterate over an array of objects. **Options Compared** The benchmark compares two options: * Using the Underscore.js `_forEach()` function * Using a native for loop **Pros and Cons of Each Approach** * **Underscore.js `_forEach()`**: + Pros: - Concise and readable code - Easy to use and maintain + Cons: - Adds extra overhead due to the library's function call - May not be optimized for performance in all browsers * **Native For Loop**: + Pros: - Highly optimized for performance - Low overhead compared to using a library function + Cons: - More verbose and less readable code - Requires manual indexing of array elements **Library: Underscore.js** Underscore.js is a popular JavaScript utility library that provides various functional programming helpers. The `_forEach` function is one of its most commonly used functions, allowing developers to iterate over arrays and execute callback functions for each element. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax mentioned in this benchmark. Both tests use standard JavaScript features (loops) and do not rely on any experimental or deprecated syntax. **Other Alternatives** If you need to compare performance of other array iteration methods, consider the following alternatives: * **`Array.prototype.forEach()`**: This method is similar to Underscore.js `_forEach()`, but it's a built-in JavaScript function. * **`Array.prototype.map()` and `Array.prototype.filter()`**: These methods are also part of the Array prototype and can be used for array iteration, but they may have different performance characteristics compared to native for loops or `_forEach()`. * **`for...of` Loop**: This is a newer type of loop that's more concise and readable than traditional for loops. However, its performance characteristics may vary depending on the browser implementation. In summary, this benchmark compares two common approaches for iterating over arrays in JavaScript: using the Underscore.js `_forEach()` function versus a native for loop. The results indicate that the native for loop approach is generally faster than using `_forEach()`, but both approaches have their own trade-offs in terms of code readability and maintainability.
Related benchmarks:
Comparisons of performance for finding a value in an array
Foreach lodash vs native
Underscore.js _forEach() vs Native for loop for large arrays (rewrite)
Underscore each vs native object foreach
Comments
Confirm delete:
Do you really want to delete benchmark?