Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs array includes
(version: 0)
Comparing performance of:
set vs array
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array=[]; for(let i=0; i<2000;i++){ array[i] = i; } var set = new Set(array);
Tests:
set
set.has(1855);
array
array.indexOf(1855);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set
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 benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches for checking if an element exists in a data structure: using a `Set` or an array with `indexOf()`. The goal is to measure which approach is faster. **Options Compared** Two options are compared: 1. **Using a Set**: A `Set` is created from the generated array and then used to check if a specific element (1855) exists in it using the `has()` method. 2. **Using an array with indexOf()**: An array of 2000 elements is generated, one of which is set to 1855. Then, the `indexOf()` method is called on this array to find the index of 1855. **Pros and Cons** * **Using a Set**: + Pros: Efficient use of memory ( Sets store unique values), fast lookup times. + Cons: May not be suitable for all use cases (e.g., when order matters). * **Using an array with indexOf()**: + Pros: Suitable for most use cases, especially when order is important. + Cons: Slower than using a Set due to the need to iterate through the entire array. **Library and Purpose** In this benchmark, the `Set` object from the JavaScript API is used. A `Set` is a collection of unique values that can be added, removed, or checked for existence. **Special JS Feature/Syntax** There's no special JavaScript feature or syntax being tested in this benchmark. It's a straightforward comparison of two simple approaches. **Other Alternatives** If you were to rewrite this benchmark, you might consider adding more options, such as: * Using an object with `in` operator * Using a Map (if available in the target browser) * Using a custom data structure (e.g., a binary search tree) However, these alternatives may not be immediately relevant or representative of common use cases. **Benchmark Preparation Code Explanation** The preparation code creates an array of 2000 elements and a `Set` from it. This ensures that both options are testing the same dataset and can compare their performance accurately. Similarly, the test case definitions specify the benchmark definition for each option (using `set.has()` or `array.indexOf()`) and provide a name for the test case. **Latest Benchmark Result** The latest benchmark results show the execution counts per second for each option in Firefox 82 on a desktop device. The results indicate that using a `Set` is faster than using an array with `indexOf()`.
Related benchmarks:
recreate array vs set 2
set vs array iteration 100k elements
set vs array includestratsatsrats
new set vs array includes
Comments
Confirm delete:
Do you really want to delete benchmark?