Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Get min and max value from a large collection of objects (native sort vs reduce)
(version: 2)
Comparing performance of:
Array.sortBy vs Array.Reduce
Created:
2 years ago
by:
Registered User
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 arr = []; for (i = 0; i < 1000; i++) { var val = Math.random() * i arr.push({ val }) }
Tests:
Array.sortBy
var sorted = arr.sort((datum1, datum2) => { const val1 = datum1.val const val2 = datum2.val if (typeof val1 !== 'undefined' && typeof val2 !== 'undefined') { if (val1 < val2) { return -1 } else if (val2 > val1) { return 1 } } return 0 }) var max = sorted[sorted.length - 1] var min = sorted[0]
Array.Reduce
var result = arr.reduce( (minMax, d) => { var { min, max } = minMax var dataVal = d.val if (dataVal !== undefined) { return { min: Math.min(min, dataVal), max: Math.max(max, dataVal) } } return minMax }, { min: Infinity, max: -Infinity } )
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.sortBy
Array.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 dive into the benchmarking test. **Benchmark Definition** The benchmark measures the performance of two approaches to find the minimum and maximum values in an array of objects: 1. **Native `sort()` method**: The script first sorts the array using the `sort()` method, which sorts the elements in place based on a comparison function. Then, it finds the first element (minimum value) and last element (maximum value) of the sorted array. 2. **`reduce()` method with callback**: The script uses the `reduce()` method to iterate over the array and find the minimum and maximum values. The callback function takes two arguments: the accumulator (`minMax`) and the current element (`d`). It updates the accumulator by finding the minimum value of the accumulator's min, dataVal, if defined; then finds the max value of the accumulator's max, dataVal, if defined. **Options Compared** The benchmark compares the performance of these two approaches: * Native `sort()` method vs. `reduce()` method with callback **Pros and Cons** 1. **Native `sort()` method**: * Pros: + Simple to implement + Well-supported by most browsers * Cons: + Time-consuming due to the sorting process + May not be suitable for large datasets or datasets with many duplicate values 2. **`reduce()` method with callback**: * Pros: + More efficient than native `sort()` method for large datasets + Can handle datasets with many duplicate values more efficiently * Cons: + Requires more setup and understanding of the callback function + May not be as well-supported by older browsers **Library Usage** The benchmark uses Lodash, a popular JavaScript utility library. In this case, Lodash is used to provide a `lodash.min` function that returns the smallest value from an array. The `lodash.min` function is called in the `reduce()` method with callback. **JavaScript Feature/ Syntax** The benchmark does not explicitly use any special JavaScript features or syntax beyond what's standard in modern browsers (ES6+). However, it relies on the `reduce()` method and callback functions, which are part of the ES6 specification. The `sort()` method is also a built-in method that has been available for a while. **Other Alternatives** If you need to find minimum and maximum values in an array of objects, other alternatives could be: 1. **Using `Math.min()` and `Math.max()` functions**: This approach would involve applying the `min` and `max` functions to the accumulator's min and max values, respectively. 2. **Using `forEach()` method with callback**: This approach would involve using a loop to iterate over the array and find the minimum and maximum values manually. 3. **Using a specialized library or module**: Depending on your specific requirements, you might consider using a library like FastMath or mathjs for efficient numerical computations. Keep in mind that these alternatives may have different performance characteristics compared to the `reduce()` method with callback approach used in this benchmark.
Related benchmarks:
Check speed of lodash sort vs js sort
Lodash sort vs array.prototype.sort - 2
Lodash orderBy() vs array.prototype.sort
Lodash sort vs array.prototype.sort 3
Comments
Confirm delete:
Do you really want to delete benchmark?