Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Underscore each vs native foreach
(version: 0)
Comparing performance of:
Underscore vs Native
Created:
2 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 = _.range(10000).map(function(i) { return { counter: i } });
Tests:
Underscore
var result = 0 _.each(data, (i) => {result += i})
Native
var result = Object.values(data).forEach((i) => {result += i})
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:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 OPR/104.0.0.0
Browser/OS:
Opera 104 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Underscore
2694.1 Ops/sec
Native
2777.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON. The benchmark compares two approaches to iterate over an array and sum its elements: using Underscore.js (a JavaScript library) and native JavaScript `foreach` loop. **Underscore.js Approach** * The "Benchmark Definition" JSON shows how to use Underscore.js to iterate over the `data` array. The code uses `_each`, a function provided by Underscore, to execute a callback function for each element in the array. * In this case, the callback function simply adds the current value (`i`) to a running total (`result`). **Native JavaScript `foreach` Loop Approach** * The second "Benchmark Definition" JSON shows how to iterate over the `data` array using native JavaScript. It uses `Object.values` to get an array of values from the `data` object, and then applies `forEach`, another built-in function, to execute a callback function for each value. * In this case, the callback function still adds the current value (`i`) to the running total (`result`). **What's Being Tested?** The benchmark is measuring which approach (Underscore.js or native JavaScript) is faster. **Pros and Cons of Each Approach:** 1. **Underscore.js**: * Pros: + Encapsulates iteration logic, making it easier to write and maintain code. + Provides additional features like event handling and data manipulation. * Cons: + Adds an external dependency (Underscore.js), which may not be present in all environments. + May introduce overhead due to the library's functionality. 2. **Native JavaScript `foreach` Loop**: * Pros: + Lightweight, as it only relies on built-in functions. + No dependencies or overhead from external libraries. * Cons: + Requires manual handling of iteration and callback logic. **Library: Underscore.js** Underscore.js is a popular JavaScript utility library that provides a set of functional programming helpers. In this benchmark, it's used for its `_each` function, which simplifies iterating over arrays and objects. **Special JS Feature/Syntax: None mentioned** There's no special JavaScript feature or syntax being tested in this benchmark. **Other Alternatives** If you were to implement a custom iteration loop without relying on Underscore.js or native `foreach`, you might consider using: * A custom implementation using a `for` loop and array indexing. * A library like Lodash (another popular utility library) that provides similar functionality to Underscore.js. Keep in mind that these alternatives would likely be slower than the optimized versions provided by Underscore.js or native JavaScript.
Related benchmarks:
Array loop vs foreach vs map vs every
Array loop vs foreach vs map proper
JS Array loop vs foreach vs map vs for of
Array loop vs foreach vs map vs for...in
Array map vs forEach
Comments
Confirm delete:
Do you really want to delete benchmark?