Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Underscore each vs native object 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 = {}; Array(10000).keys().forEach(function (i) { data[i] = 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
11119.5 Ops/sec
Native
21260.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and analyze the provided benchmark. **Benchmark Overview** The benchmark is designed to measure the performance difference between using Underscore.js (a popular JavaScript utility library) versus native JavaScript for iterating over an array using `forEach`. The test creates a large array of 10,000 elements, assigns each element a value, and then measures how long it takes to sum up all these values using both approaches. **Test Cases** There are two test cases: 1. **Underscore**: This test case uses the Underscore.js library's `each` function to iterate over the array and accumulate the sum. 2. **Native**: This test case uses native JavaScript's `forEach` method to iterate over the array and accumulate the sum. **Comparison** The main difference between these two approaches is how they handle the iteration and accumulation of values. * **Underscore**: Underscore.js provides a higher-level interface that abstracts away some low-level details, making it easier to write concise code. In this case, `_.each` iterates over the array and calls a callback function for each element, which accumulates the sum in the `result` variable. * **Native**: Native JavaScript's `forEach` method is more direct and explicit. It iterates over the array and calls a provided callback function for each element, but does not accumulate values by default. In this case, we need to explicitly use `Object.values(data).forEach((i) => { result += i })` to achieve the same result. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Underscore**: Pros: + Easier to write concise code + Higher-level interface abstracts away low-level details + Can be more readable for complex operations * Cons: + Adds an external dependency (Underscore.js) + May introduce performance overhead due to function calls and closures * **Native**: Pros: + No additional dependencies or overhead + More direct and explicit control over iteration and accumulation + Can be more optimized for performance by the JavaScript engine * Cons: + Requires more boilerplate code (using `Object.values()` and `forEach`) + May be less readable for complex operations **Other Considerations** When writing benchmarks, it's essential to consider the following factors: * **External dependencies**: Adding external libraries or modules can introduce performance overhead and inconsistencies across different environments. * **Performance optimization**: Some optimizations, like caching or memoization, might not be immediately apparent but can significantly impact performance. * **Code readability and maintainability**: While concise code is often desirable, it's equally important to ensure that the code remains readable and maintainable. **Alternatives** If you were to rewrite this benchmark without using Underscore.js, you could consider alternative libraries or approaches for iterating over arrays, such as: * Lodash (similar to Underscore) * Ramda (a functional programming library with a different API) * Native JavaScript's `map()` and `reduce()` methods * A custom implementation of the loop and accumulation logic Keep in mind that the choice of alternative will depend on the specific requirements and constraints of your benchmark.
Related benchmarks:
Underscore vs Array functions
Underscore.js _forEach() vs Native for loop for large arrays (rewrite)
Underscore each vs native foreach
Underscore each v native object foreach
Comments
Confirm delete:
Do you really want to delete benchmark?