Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unique values in array x8924t
(version: 0)
Comparing performance of:
reduce includes vs set spread vs set filter vs set Array.from vs filter
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = ["grinning", "smiley", "smile", "grin", "laughing", "sweat_smile", "rolling_on_the_floor_laughing", "joy", "slightly_smiling_face", "grinning", "smiley", "smile", "grin", "laughing", "sweat_smile", "rolling_on_the_floor_laughing", "joy", "slightly_smiling_face", "grinning", "smiley", "smile", "grin", "laughing", "sweat_smile", "rolling_on_the_floor_laughing", "joy", "slightly_smiling_face", "grinning", "smiley", "smile", "grin", "laughing", "sweat_smile", "rolling_on_the_floor_laughing", "joy", "slightly_smiling_face", "grinning", "smiley", "smile", "grin", "laughing", "sweat_smile", "rolling_on_the_floor_laughing", "joy", "slightly_smiling_face", "grinning", "smiley", "smile", "grin", "laughing", "sweat_smile", "rolling_on_the_floor_laughing", "joy", "slightly_smiling_face"];
Tests:
reduce includes
data.reduce((acc, item) => { if (!acc.includes(item)) acc.push(item); return acc; }, []);
set spread
[...new Set(data)];
set filter
data.reduce((acc, item) => { acc.add(item); return acc; }, new Set());
set Array.from
Array.from(new Set(data))
filter
data.filter((v, i, a) => a.indexOf(v) === i)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
reduce includes
set spread
set filter
set Array.from
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 break down the benchmark and its various components. **Benchmark Definition** The benchmark is designed to measure the performance of different approaches for finding unique values in an array. The benchmark definition uses a JSON string `data` that contains repeated values, which will be used as input for each test case. **Test Cases** There are four test cases: 1. **"reduce includes"`: This test case measures the performance of using the `reduce()` method with an `if` statement to check if an element is already included in the accumulator array. 2. **"set spread"`: This test case measures the performance of spreading a set over an array using the `Set` object and the spread operator (`[...new Set(data)]`). 3. **"set filter"`: This test case measures the performance of filtering an array to remove duplicates by checking if each element's index is equal to its value in the original array. 4. **"filter"`: This test case measures the performance of using the `filter()` method with a callback function that checks if an element's index is equal to its value. **Library Used** All test cases use built-in JavaScript libraries and methods, such as `Set`, `Array.prototype.reduce()`, `Array.prototype.filter()`, and `Array.from()`. These libraries are part of the standard JavaScript API. **Pros and Cons** Here are some pros and cons for each approach: 1. **"reduce includes"`: * Pros: Can be more efficient than other approaches since it only iterates over the array once. * Cons: Requires an initial value for the accumulator, which can lead to slower performance if not initialized correctly. 2. **"set spread"`: * Pros: Can be faster and more memory-efficient than other approaches since it uses a set data structure internally. * Cons: Requires creating a new set object, which can incur additional overhead. 3. **"set filter"`: * Pros: Can be more efficient than `reduce()` or `filter()` since it only iterates over the array once and uses a simple conditional statement. * Cons: The condition `a.indexOf(v) === i` is slower than a simple `!acc.includes(item)` check because of the additional lookup in the array. 4. **"filter"`: * Pros: Can be more efficient than `reduce()` since it only iterates over the array once and uses a simple conditional statement. * Cons: Requires creating a new array object, which can incur additional overhead. **Other Considerations** When choosing an approach, consider the following factors: * Memory usage: Using sets or other data structures that require more memory can lead to slower performance in certain cases. * Initial value requirements: Some approaches, like `reduce()`, require an initial value for the accumulator, which can impact performance if not initialized correctly. * Conditional statements: The use of conditional statements can slow down execution due to the additional overhead. **Alternatives** If you need to find unique values in an array, you can also consider using other libraries or frameworks that provide optimized algorithms for this task. Some alternatives include: * Lodash: A popular utility library that provides a `uniqBy` function for finding unique values by a specific property. * Ramda: A functional programming library that provides a `uniq` function for finding unique values in an array. * d3-delaunay: A library specifically designed for geometric computations, which includes optimized algorithms for finding unique values in arrays. Keep in mind that these alternatives may not be part of the standard JavaScript API and may require additional setup or imports.
Related benchmarks:
non-unique-elements
unique elements in array using filter v2
unique elements in array using filter v2.3
unique elements in array using filter - large array
Javascript unique string array
Comments
Confirm delete:
Do you really want to delete benchmark?