Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs array includestratsatsrats
(version: 0)
Comparing performance of:
set vs array
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array=[]; for(let i=0; i<2000;i++){ array[i] = i; }
Tests:
set
var set = new Set(array); 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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
set
23578.1 Ops/sec
array
3958803.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the JavaScript microbenchmark on MeasureThat.net. **Benchmark Definition JSON** The benchmark definition is a simple script that creates an array of 2000 elements, where each element is its index in the array. Then, it tests two different approaches: 1. Creating a new Set object from the array (`var set = new Set(array);`) and checking if the number 1855 is present in the set using the `has` method. 2. Using the `indexOf` method to search for the number 1855 in the original array. **Options Compared** The two options being compared are: * Creating a Set object from an array * Using the `indexOf` method on an array **Pros and Cons of Each Approach** 1. **Creating a Set Object from an Array** * Pros: + Faster lookup times, especially for large datasets (because Set objects use a hash table data structure) + Reduces memory usage compared to storing all elements in an array * Cons: + Requires additional memory allocation for the Set object + May be slower if the set needs to be very large (due to the overhead of creating and managing the Set object) 2. **Using the `indexOf` Method on an Array** * Pros: + Simple and straightforward implementation + No additional memory allocation required * Cons: + Slower lookup times, especially for large datasets (because array search algorithms have a linear time complexity of O(n)) + May cause performance issues if the array is very large **Library and Purpose** In this benchmark, there is no explicit library mentioned. However, it's worth noting that the `Set` object is a native JavaScript data structure introduced in ECMAScript 2015 (ES6). It provides fast lookup times for its elements and is often used to implement set-based data structures. **Special JS Feature or Syntax** There are no special JS features or syntax mentioned in this benchmark. The code uses standard JavaScript syntax and does not rely on any experimental or upcoming features. **Other Alternatives** If you wanted to explore alternative approaches, here are a few options: * Using the `Array.prototype.includes` method (introduced in ECMAScript 2015) instead of `indexOf` * Creating an object with the number as a key and checking if it exists * Using a library like Lodash or Ramda for set operations Keep in mind that these alternatives may not provide significant performance improvements over the original Set-based approach or `indexOf` method.
Related benchmarks:
recreate array vs set 2
set vs array includes
Create Set vs loop
new set vs array includes
Comments
Confirm delete:
Do you really want to delete benchmark?