Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Filter - unique string
(version: 0)
Comparing performance of:
Set spread vs Set array from vs Filtered array
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var stringArray = Array.from({ length: 9999 }, () => `${Math.floor(Math.random() * 100)}`);
Tests:
Set spread
const setSpread = [... new Set(stringArray)]
Set array from
const arrayFromSet = Array.from(new Set(stringArray))
Filtered array
const filteredArray = stringArray.filter((i,index) => stringArray.indexOf(i) === index)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set spread
Set array from
Filtered array
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 provided JSON benchmark definition and test cases. **Benchmark Definition** The benchmark measures performance differences between three approaches: 1. **Set spread**: Creating a new array by spreading a Set object created from an array (`const setSpread = [... new Set(stringArray)]`). 2. **Set array from**: Converting a Set object to an array using the `Array.from()` method (`const arrayFromSet = Array.from(new Set(stringArray))`). 3. **Filtered array**: Creating a new array by filtering the original array, where each element is included only if its index in the original array matches its first occurrence (using `stringArray.indexOf(i) === index`). **Options Comparison** Here's a brief overview of each approach: * **Set spread**: * Pros: Creates a new Set object from the array, which allows for efficient lookup and removal of elements. This can lead to better performance when dealing with large datasets or when using the `delete` method. * Cons: Requires creating an additional set object, which may incur overhead due to memory allocation and garbage collection. Also, this approach creates a new array by spreading the set, which may not be necessary if only the set operations are required. * **Set array from**: * Pros: This method avoids creating an intermediate set object and directly converts it to an array using `Array.from()`. It also preserves the original order of elements, unlike the spread operator. * Cons: This approach may be slower due to the overhead of converting a set to an array. Additionally, this method does not preserve the unique property of sets, as the resulting array might contain duplicates if the input array has duplicate values. * **Filtered array**: * Pros: This method directly filters the original array, which can lead to better performance since it avoids creating intermediate data structures. * Cons: This approach requires comparing each element with its index using `stringArray.indexOf(i) === index`, which may be slower than set-based operations for large datasets. Also, this approach does not preserve uniqueness. **Library and Special JS Features** * **Set**: The Set data structure is a built-in JavaScript object that allows you to store unique values. It provides efficient lookup, insertion, and removal of elements. * **Spread operator (`...`)**: This operator is used to create a new array by spreading the elements of an iterable (like an array or set). * **Array.from() method**: This method creates a new array from an iterable (like an array, string, or set) and returns the resulting array. * Special JS feature not mentioned here as this benchmark only includes standard JavaScript features. **Alternative Approaches** For performance-critical benchmarks like this one, you might want to consider alternative approaches such as: * **Native set operations**: If available in your target environment, native set operations (e.g., `Set.prototype.delete()`, `Set.prototype.has()`) can be faster than the spread operator or `Array.from()` method. * **Iterating over indices directly**: Instead of using `stringArray.indexOf(i) === index`, you could directly access elements at their corresponding indices in the original array, skipping unnecessary lookups. Keep in mind that benchmarking is all about finding the most efficient approach for your specific use case. You may need to experiment with different techniques and test them thoroughly before making a conclusion.
Related benchmarks:
Unique values of array
Filter vs Set (get unique elements)
Filter vs Set (unique elements)
Set from array vs array Filter unique
Comments
Confirm delete:
Do you really want to delete benchmark?