Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Filter for unique vs for
(version: 0)
Comparing performance of:
Set spread vs Array from set vs Filter vs Create Set vs For
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({length: 40}, () => Math.floor(Math.random() * 140)); var arrayA = Array.from({length: 40}, () => Math.random().toString(36).substring(2, 15));
Tests:
Set spread
const f = [... new Set(array)] const z = [... new Set(arrayA)]
Array from set
const s = new Set(array) const l = Array.from(s) const sa = new Set(array) const la = Array.from(sa)
Filter
const b = array.filter((i,index) => array.indexOf(i)=== index) const ba = arrayA.filter((i,index) => arrayA.indexOf(i)=== index)
Create Set
const ss = new Set(array) const sc = new Set(arrayA)
For
const s ={} const lc = [] for(let i =0; i < array.length; i++){ if (s[array[i]]) { continue } s[array[i]] = 1 lc.push(s[array[i]]) } const sa ={} const lca = [] for(let i =0; i < arrayA.length; i++){ if (s[arrayA[i]]) { continue } sa[arrayA[i]] = 1 lca.push(s[arrayA[i]]) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Set spread
Array from set
Filter
Create Set
For
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):
Measuring performance of JavaScript operations is crucial for ensuring efficient and scalable code. Let's break down the provided benchmarking JSON. **Benchmark Overview** The test creates an array `array` with 40 random elements, each between 0 and 139 (inclusive). Another array `arrayA` is created with 40 random base64 encoded strings. These arrays are used to compare various JavaScript operations for creating a unique set. **Options Compared** 1. **Set Spread**: Creating a new Set from the original array (`const f = [... new Set(array)]`) and then spreading it back into an array (`const z = [... new Set(array)]`). 2. **Array.from(Set)**: Creating a new Set from `array` and then using `Array.from()` to convert it back into an array (`const l = Array.from(s)`). The same approach is applied for `arrayA`. 3. **Filter**: Using the `filter()` method with a callback function that checks if the element's index in the original array matches its value (assuming no duplicates) (`const b = array.filter((i, index) => array.indexOf(i) === index)`). 4. **Create Set**: Creating a new Set directly from `array` and storing it in `ss`, then creating another Set directly from `arrayA` and storing it in `sc`. 5. **For Loop**: Using a loop to iterate over the arrays, checking for existing values in the `s` object (or its equivalent) before adding new ones. **Pros and Cons of Each Approach** 1. **Set Spread**: Simple and efficient, as Set operations are optimized by JavaScript engines. 2. **Array.from(Set)**: Also efficient but may incur additional overhead due to converting between Set and Array. 3. **Filter**: May be slower due to the use of `indexOf()` which can lead to linear search in large arrays. 4. **Create Set**: Straightforward, yet may be slower than other methods as it involves direct Set creation and conversion. 5. **For Loop**: The loop approach is typically slower, especially for larger arrays, due to its inherent overhead. **Library Usage** The `Array.from()` method uses the native JavaScript API, which is implemented in the browser's engine (e.g., WebKit, Blink). **Special JS Features or Syntax** * None mentioned explicitly. However, some features like ES6 syntax (`const`, `let`, `=>`) are used implicitly. **Alternatives** Other alternatives to compare could include: * Using `Map` instead of Sets * Implementing custom data structures (e.g., hash tables) for faster lookups * Using libraries like `lodash` which provide optimized implementations of Set-like operations
Related benchmarks:
Set vs Filter for unique vs for string and numbers
Filter vs Set (get unique elements)
Filter vs Set (unique elements)
Set vs Filter - unique string
Set from array vs array Filter unique
Comments
Confirm delete:
Do you really want to delete benchmark?