Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Underscore each vs custom while
(version: 0)
Comparing performance of:
Underscore each vs While Loop
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.8.3/underscore-min.js'></script>
Script Preparation code:
var primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,97]; function forEach(arr, callback) { let i = 0; while (i < arr.length) { callback(arr[i], i); i++; } }
Tests:
Underscore each
var result = _.each(primes, (item, index) => { console.log(item); });
While Loop
forEach(primes, (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 each
While Loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the provided JavaScript microbenchmark. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches for iterating over an array: using Underscore.js's `each` function and a custom while loop. **Test Cases** There are two test cases: 1. **Underscore each**: This test case uses the `each` function from Underscore.js to iterate over the `primes` array and log each item to the console. 2. **While Loop**: This test case uses a custom while loop to iterate over the `primes` array and log each item to the console. **Library: Underscore.js** Underscore.js is a popular JavaScript utility library that provides a set of functional programming helpers, including the `each` function used in this benchmark. The `each` function takes two arguments: an array and a callback function that will be executed for each element in the array. **Custom While Loop** The custom while loop is implemented as follows: ```javascript function forEach(arr, callback) { let i = 0; while (i < arr.length) { callback(arr[i], i); i++; } } ``` This loop iterates over the `primes` array using a while loop, calling the provided callback function for each element and incrementing the index variable. **Performance Comparison** The benchmark measures the number of executions per second (ExecutionsPerSecond) for each test case. In this case: * **Underscore each**: 10,790.697265625 executions per second * **While Loop**: 8,559.8056640625 executions per second **Pros and Cons** Here are some pros and cons of each approach: * **Underscore each**: + Pros: concise and readable code, easier to maintain and debug. + Cons: depends on the presence of Underscore.js, may have overhead due to library loading. * **While Loop**: + Pros: no dependencies on external libraries, more control over loop variables and logic. + Cons: more verbose code, can be harder to read and maintain. **Other Considerations** Some other factors that could affect the performance of these benchmarks include: * The size of the `primes` array: larger arrays may lead to slower execution times due to increased memory allocation and garbage collection. * The specific hardware and software environment used for running the benchmark: different browsers, operating systems, or architectures may produce varying results. **Alternative Implementations** Other approaches to iterating over an array could include: * Using a `for...of` loop with a `forEach` function * Using a `map` or `filter` function to process the array elements * Using a library like Lodash or Ramda for functional programming helpers
Related benchmarks:
Underscore map vs Array map
Underscore filter vs Array filter
Underscore each vs while
test js includes and some
Comments
Confirm delete:
Do you really want to delete benchmark?