Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce / new set
(version: 0)
Comparing performance of:
reduce vs new set
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var mcus = [{'series':'a'},{'series':'b'},{'series':'c'}]
Tests:
reduce
const res = [...mcus.reduce( (acc, mcu) => acc.add(mcu.series.toString()), new Set() ), ];
new set
const res2 = new Set(mcus.map((mcu) => mcu.series.toString()))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
new set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
2440934.2 Ops/sec
new set
2858717.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The test measures the performance difference between two approaches: using the `Array.prototype.reduce()` method to create a set of strings, and using the `Set` constructor to create a set of strings from an array. **Script Preparation Code** The script preparation code defines an array `mcus` with three objects, each containing a `series` property. This array will be used as input for both test cases. **Individual Test Cases** There are two individual test cases: 1. **"reduce"`**: This test case uses the `Array.prototype.reduce()` method to create a set of strings. The code creates an accumulator variable `acc`, initializes it with an empty string, and then iterates over the `mcus` array, adding each `series` property as a string to the accumulator using the `add()` method. 2. **"new set"`**: This test case uses the `Set` constructor to create a set of strings from the `mcus` array. The code maps over the `mcus` array, converting each `series` property to a string using the `toString()` method, and then passes this array of strings to the `Set` constructor. **Pros and Cons** **Reduce Approach:** Pros: * Can be more efficient for large datasets since it avoids the overhead of creating multiple sets. * Can be optimized by using a custom accumulator function that returns a single value. Cons: * May not be as intuitive or readable, especially for those who are unfamiliar with the `reduce()` method. * Can lead to memory issues if the accumulator grows too large. **New Set Approach:** Pros: * Is more explicit and easier to understand, as it clearly separates creation of an array and conversion to a set. * Does not require creating a temporary accumulator variable. Cons: * May be less efficient for very large datasets due to the overhead of creating multiple sets. * Requires additional memory allocation for the intermediate arrays. **Other Considerations** * Both approaches assume that the `series` property is a string. If it's not, additional processing may be required to convert it to a string. * The `reduce()` approach assumes that the accumulator variable can hold arbitrary values; if it needs to hold specific types of data, the implementation will need to adjust accordingly. **Library and Special JS Features** There are no external libraries used in this benchmark. However, both approaches use JavaScript features that are commonly supported across modern browsers: * `Array.prototype.reduce()`: This method is a standard part of the ECMAScript specification since ECMAScript 2006. * `Set` constructor: This constructor has been part of the ECMAScript specification since ECMAScript 2015. **Alternatives** Other approaches to achieve similar results could include: * Using `map()` and `concat()` instead of `reduce()`. * Creating a set from an array using a loop (not shown in this benchmark). * Using other data structures, such as a custom-built data structure or a third-party library. Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to the approaches tested in this benchmark.
Related benchmarks:
Loop perf
Mutable object reducer vs immutable object reducer
Spread vs object assign
reduce vs map and filter
Destructuring vs Dot notation
Comments
Confirm delete:
Do you really want to delete benchmark?