Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash reduce with native reduce
(version: 0)
Comparing performance of:
Lodash reduce vs Native reduce
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var array = []; var max = 10000; for (let i = 1; i <= max; ++i) { array.push(i); }
Tests:
Lodash reduce
_.reduce(array, (sum, number) => sum += number, 0);
Native reduce
array.reduce((sum, number) => sum += number, 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash reduce
Native reduce
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):
Let's break down the benchmark and explain what is being tested. **Benchmark Overview** The benchmark compares two approaches to reduce an array of numbers: using the Lodash library's `reduce` function and implementing a native JavaScript implementation of `reduce`. **Script Preparation Code** The script preparation code generates an array of 10,000 random numbers and assigns it to the variable `array`. This code is executed before running each benchmark test. **Html Preparation Code** The HTML preparation code includes a script tag that loads the Lodash library version 4.17.5 from a CDN. This ensures that the Lodash library is available for use in the benchmark tests. **Individual Test Cases** There are two individual test cases: 1. **Lodash reduce**: The benchmark definition uses the Lodash `reduce` function with the following arguments: * `array`: the array to be reduced * `(sum, number) => sum += number, 0`: a callback function that takes two arguments: `sum` (the accumulator) and `number` (the current element being processed). The callback returns the updated `sum` by adding `number`. 2. **Native reduce**: This test case uses the native JavaScript `reduce` method with the same arguments as above. **Comparison** The benchmark compares the execution performance of both approaches on a desktop device running Chrome 76 browser. The results are: | Test Name | Browser | Device Platform | Operating System | Executions Per Second | | --- | --- | --- | --- | --- | | Native reduce | Chrome 76 | Desktop | Windows | 29518.626953125 | | Lodash reduce | Chrome 76 | Desktop | Windows | 10130.212890625 | **Pros and Cons** * **Native reduce**: This approach is implemented in pure JavaScript, which means it doesn't rely on any external libraries. + Pros: Lightweight, easy to implement, and optimized for performance. + Cons: May require more manual memory management and handling of edge cases. * **Lodash reduce**: This approach uses an external library, Lodash. + Pros: Simplifies the implementation by providing a well-tested and optimized `reduce` function. + Cons: Adds overhead due to the library's initialization and garbage collection. **Library - Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as data manipulation, string manipulation, and more. The `reduce` function is one of its core exports, providing a concise way to iterate over arrays and reduce them to a single value. **Special JS Feature - Closures** The callback function passed to both `reduce` methods uses closures, which are functions that have access to their own scope and can capture variables from the surrounding context. In this case, the closure is used to accumulate the sum of numbers in the array. Other alternatives to consider: * Using a different JavaScript library or framework that provides a similar reduction function. * Implementing a custom reduction function using only built-in JavaScript features. * Using a parallelization or multi-threading approach to reduce the execution time for large arrays.
Related benchmarks:
reduce native vs lodash
Lodash reduce vs native
test123_123
lodash - reduce vs forEach with large array to object
Comments
Confirm delete:
Do you really want to delete benchmark?