Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Finding the max value of a property in an array of objects
(version: 0)
Comparing performance of:
1 vs 2 vs 3
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = []; for (var i = 0; i < 10000; i++) { array.push({ value: Math.random() * 1000000, }); }
Tests:
1
var max = Math.max.apply(Math, array.map(x => x.value));
2
var max = Math.max(...array.map(x => x.value))
3
var max = array.reduce((prev, current) => (prev && prev.value > current.value) ? prev : current).value;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
1
2
3
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
1
5964.0 Ops/sec
2
6393.9 Ops/sec
3
19646.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to measure how fast different approaches can find the maximum value of a property in an array of objects. **Options Compared** There are three options compared: 1. **`Math.max.apply(Math, array.map(x => x.value))`**: This method uses the `map()` function to create a new array with the values of each object's property, and then applies the `Math.max()` function to find the maximum value. 2. **`Math.max(...array.map(x => x.value))`**: This method is similar to the previous one, but it uses the spread operator (`...`) to pass the mapped array as separate arguments to the `Math.max()` function. 3. **`array.reduce((prev, current) => (prev && prev.value > current.value) ? prev : current).value`**: This method uses the `reduce()` function to iterate over the array and find the maximum value. **Pros and Cons of Each Approach** 1. **`Math.max.apply(Math, array.map(x => x.value))`**: * Pros: Simple and readable syntax. * Cons: Creates a new array with the mapped values, which can be memory-intensive for large datasets. 2. **`Math.max(...array.map(x => x.value))`**: * Pros: More concise syntax than `apply()`, and avoids creating a new array. * Cons: Requires support for the spread operator in older browsers or environments. 3. **`array.reduce((prev, current) => (prev && prev.value > current.value) ? prev : current).value`**: * Pros: More efficient use of memory, as it only processes each element once. * Cons: Less readable syntax compared to the other two options. **Library and Purpose** In none of these examples is a specific library used. However, `Math.max()` is a built-in JavaScript function that can be used for this purpose. **Special JS Features or Syntax** The benchmark uses the following features: 1. **Arrow functions**: Used in options 2 and 3 to define concise functions. 2. **Spread operator (`...`)**: Used in option 2 to pass the mapped array as separate arguments to `Math.max()`. 3. **Reduce method**: Used in option 3 to find the maximum value in the array. **Other Alternatives** If you're looking for alternative approaches, here are a few: 1. **`array.forEach()` + `Math.max()`**: Iterate over the array using `forEach()` and call `Math.max()` on each element. 2. **`map()`, then `reduce()`**: Create a new array with the mapped values, and then use `reduce()` to find the maximum value. Keep in mind that these alternatives may have different performance characteristics or trade-offs compared to the original options.
Related benchmarks:
Get max from an array of numbers (Math.max vs. iteration) V2
array math.max vs for loop
array math.max (3 variants) vs for loop (4 variants)
array math.max (3 variants) vs for loop (5 variants)
Comments
Confirm delete:
Do you really want to delete benchmark?