Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
new set vs array includes
(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.includes(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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 HBPC/12.1.3.306
Browser/OS:
Chrome 99 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
set
26306.1 Ops/sec
array
15092034.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **What is being tested?** The benchmark measures the performance difference between using an array's `includes()` method versus creating a new Set object from the same array, and then checking if a specific element (1855) exists in it. The test case compares the execution times of these two approaches. **Options compared:** 1. **Array's `includes()` method**: This is the first approach. It checks if a specified value (`1855`) exists in the array. 2. **Creating a new Set object and checking its existence**: The second approach involves creating a Set object from the array and then checking if the element (`1855`) exists in it using the `has()` method. **Pros and Cons of each approach:** 1. **Array's `includes()` method**: * Pros: + Simple to implement. + Fast, as it only requires a linear search through the array. * Cons: + Can be slow for very large arrays (O(n) complexity). 2. **Creating a new Set object and checking its existence**: * Pros: + Faster for large arrays, as Set operations have an average time complexity of O(1) due to hash-based lookup. * Cons: + Requires creating a new object, which can be slower than using the array's `includes()` method. **Other considerations:** * The test case assumes that the array is randomly generated with values from 0 to 1999, which might skew the results if the optimal approach depends on the distribution of numbers. * The benchmark does not account for the cost of creating and garbage-collecting the Set object, as this can vary depending on JavaScript engine and runtime environment. **Library or special JS feature:** None mentioned in the provided benchmark definition. However, it's worth noting that using a library like `lodash` might provide an optimized implementation for the `includes()` method, which could potentially change the outcome of the benchmark. If you were to implement this benchmark yourself, consider using a testing framework and ensuring your code is well-optimized for performance. **Other alternatives:** * Using other data structures, such as `Map`, to store unique elements instead of an array. * Optimizing the `includes()` method implementation for specific use cases or edge cases (e.g., checking if an element exists in a sorted array). * Comparing the performance of different JavaScript engines or runtime environments. To run this benchmark yourself, you would: 1. Create an empty array (`array`) and populate it with 2000 elements using a loop. 2. Implement the two test cases: one for `includes()` and one for creating a Set object and checking its existence. 3. Run each test case multiple times and measure the execution time. 4. Compare the results to determine which approach is faster in your specific environment.
Related benchmarks:
recreate array vs set 2
set vs array includes
set vs array iteration 100k elements
set vs array includestratsatsrats
Comments
Confirm delete:
Do you really want to delete benchmark?