Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Underscore each vs forEach
(version: 0)
Comparing performance of:
Underscore vs Native
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.0/underscore.js"></script>
Script Preparation code:
var data = [ 20, 25, 21, 24, 23, 39, 44, 33, 32, 31, 35, 28, 26, 29, 30, 36, 27, 3, 22, 47, 19, 15, 18, 34, 38, 2, 37, 40, 48, 46, 45, 41, 42, 43, 6, 1, 5, 16, 9, 17, 13, 14, 12, 8, 10, 11, 4, 7, ];
Tests:
Underscore
var result = _.each(data, (item, index) => { console.log(item); });
Native
var result = data.forEach((item, index) => { console.log(item); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Underscore
Native
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Underscore
4542.7 Ops/sec
Native
4637.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the provided JavaScript microbenchmark, MeasureThat.net. **Benchmark Definition and Script Preparation Code** The benchmark measures the performance of two approaches: using `underscore.js` (a popular JavaScript utility library) and the native `forEach` method. The script preparation code provides an array of numbers (`data`) that will be processed by each approach. **Options Compared** Two options are compared: 1. **Underscore Each**: Using the `each` method from `underscore.js`, which iterates over the array and calls a callback function for each element. 2. **Native forEach**: Using the native `forEach` method, which is a built-in method of JavaScript arrays that also iterates over the elements and calls a callback function. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Underscore Each**: + Pros: More concise and expressive code; often used in production code. + Cons: Requires an additional library to be included, which may increase page load times. Also, it only works with arrays, not other data structures like sets or maps. * **Native forEach**: + Pros: Built-in method, no additional dependencies required, and works with most data structures (not just arrays). + Cons: Code is slightly more verbose compared to `underscore.js` versions. **Libraries and Special JS Features** The benchmark uses the `underscore.js` library. This library provides a set of functional programming helpers, including `each`, which is used in this benchmark. Other popular utility libraries like Lodash or Ramda could be used instead, but they would require additional imports and might not offer significant performance improvements. There are no special JavaScript features or syntax used in this benchmark that would affect its execution. **Other Alternatives** If you want to compare other approaches, here are a few alternatives: * **Array.prototype.map()**: This method creates a new array with the results of applying a provided function on every element in this array. * **Array.prototype.reduce()**: This method applies a reduction function against an accumulator and each element in the array (from left to right) to reduce it to a single output value. * **Looping using `for` loop or a `while` loop**: These methods would not be optimized by modern JavaScript engines like V8, which is used by Chrome. Keep in mind that these alternatives might have different performance characteristics and code complexity, depending on the specific use case.
Related benchmarks:
Underscore Each vs Lodash Each vs Native forEach
Underscore.js _forEach() vs Native for loop for large arrays (rewrite)
Underscore each vs native foreach
Underscore each vs native object foreach
Underscore each v native object foreach
Comments
Confirm delete:
Do you really want to delete benchmark?