Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Trey - unique array
(version: 0)
Comparing performance of:
Reduce vs Set vs Filter
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
globalThis.nonUnique = ['one', 'two', 'three', 'three', 'four', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
Tests:
Reduce
Object.keys(globalThis.nonUnique.reduce((acc, item) => { if (!acc[item]) { acc[item] = true }; return acc; }, {}));
Set
Array.from(new Set(globalThis.nonUnique));
Filter
globalThis.nonUnique.filter((value, index, array) => { return array.indexOf(value) === index; })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Reduce
Set
Filter
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 world of JavaScript microbenchmarks. The provided JSON represents a benchmark test case on MeasureThat.net, where users can create and run JavaScript benchmarks. The benchmark is designed to measure the performance of three different approaches: reducing an object using `reduce()`, creating a set from an array using `Set` constructor, and filtering an array using `filter()` method. **Options being compared:** 1. **Reduce**: This approach uses the `reduce()` method to create an object with unique keys based on the values in the input array `globalThis.nonUnique`. The `reduce()` method iterates through the array, accumulating a result object where each key is a value from the array and its corresponding value is set to `true`. 2. **Set**: This approach uses the `Set` constructor to create a set from the input array `globalThis.nonUnique`. A set in JavaScript is an unordered collection of unique values. 3. **Filter**: This approach uses the `filter()` method to create a new array containing only the elements that pass the test implemented by the provided function. **Pros and Cons:** * **Reduce**: Pros: + It can be more memory-efficient since it creates an object with references to the original values, rather than creating a new set or array. + It has better cache locality since it iterates through the array in order. * Cons: + It may have slower performance due to the overhead of iterating through the entire array. * **Set**: Pros: + It is often faster than `reduce()` since sets are implemented as hash tables, which provide O(1) lookup time. + It can be more memory-efficient since it doesn't create a large object with references to values. * Cons: + It may not have the same cache locality as `reduce()`. + It requires additional memory for storing the set data structure. * **Filter**: Pros: + It is often faster than `reduce()` since it only iterates through the array once and can stop early if the predicate function returns false. + It can be more memory-efficient since it doesn't create a large object with references to values. * Cons: + It may have slower performance due to the overhead of iterating through the entire array. + It requires additional memory for storing the filtered array. **Library and purpose:** The `Set` constructor is a built-in JavaScript library that provides an efficient way to store unique values. It is implemented as a hash table, which allows for fast lookup, insertion, and deletion of elements. **Special JS feature or syntax:** None mentioned in this benchmark. **Other alternatives:** If you're looking for alternative approaches to these benchmarks, here are some options: * Instead of using `reduce()`, you could use `Array.prototype.forEach()` with a callback function that sets the value at each index. * Instead of using `Set` constructor, you could implement your own set data structure using arrays and hash tables. * Instead of using `filter()`, you could implement your own filtering algorithm using loops and conditional statements. Keep in mind that these alternatives may have different performance characteristics and trade-offs.
Related benchmarks:
unique elements in array using filter - large array
lodash uniq vs set array comprehension
Javascript unique string array
Javascript unique string array with array-valued return type
Array of strings, null, and ints lodash uniq vs set
Comments
Confirm delete:
Do you really want to delete benchmark?