Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes various array sizes
(version: 0)
Comparing performance of:
includes10k vs lookup10k vs includes1k vs lookup1k
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a10k = new Array(10000).fill(undefined).map((x,i) => i); var b10k = new Set(a10k) var a1k = new Array(1000).fill(undefined).map((x,i) => i); var b1k = new Set(a1k); var c = Math.floor(Math.random()*10000); var d = Math.floor(Math.random()*1000);
Tests:
includes10k
return a10k.includes(c)
lookup10k
return b10k.has(c)
includes1k
return a1k.includes(d)
lookup1k
return b1k.has(d)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
includes10k
lookup10k
includes1k
lookup1k
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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches for searching an array: `array.includes()` and `set.has()`. The test cases are created by preparing arrays and sets with varying sizes, using random values. **Options Compared** There are four options being compared: 1. **`array.includes()`**: This method searches for the existence of a value in an array. 2. **`set.has()`**: This method checks if a value exists in a set (a collection of unique values). 3. **`array.includes()` with large arrays**: The test case uses arrays of 10,000 elements (`a10k`) and smaller arrays of 1,000 elements (`a1k`). 4. **`set.has()` with large sets**: The test case uses sets created from the large arrays (`b10k` and `b1k`). **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **`array.includes()`**: + Pros: widely supported, simple to implement. + Cons: has to iterate through the entire array, which can be slow for large arrays. * **`set.has()`**: + Pros: faster lookup times, especially for large sets, since sets only store unique values and don't require searching. + Cons: requires a set data structure, which can be slower to create than an array. **Library: `Array.prototype.includes()`** The `includes()` method is a built-in JavaScript method that searches for the existence of a value in an array. It's implemented using a binary search algorithm, which makes it relatively efficient. **Special JS Feature/ Syntax: None mentioned** There are no special JavaScript features or syntax used in this benchmark. **Benchmark Preparation Code Explanation** The preparation code creates four sets and arrays: * `a10k` and `b10k`: large arrays with 10,000 elements, filled with random values. * `a1k` and `b1k`: smaller arrays with 1,000 elements. * `c` and `d`: two random integers used as test values. The sets are created using the `Set()` constructor and the arrays are created using the `Array()` constructor. The preparation code also includes some additional setup to ensure consistent results across different browsers and environments. **Alternative Approaches** Some alternative approaches for searching in arrays include: * **`indexOf()`**: This method returns the index of the first occurrence of a value in an array. It's slower than `includes()` but can be useful if you need to find the actual position of a value. * **`filter()`**: This method creates a new array with all elements that pass a test (in this case, checking for existence). * **`reduce()`**: This method reduces the array to a single value based on a callback function. It can be used to search for a value in an array. Keep in mind that these alternative approaches may have different performance characteristics and use cases compared to `array.includes()`.
Related benchmarks:
Set.has vs. Array.includes
set.has vs. array.includes 10k el
array vs set lookup
Lodash _.union vs the native Set() for multiple arrays
Comments
Confirm delete:
Do you really want to delete benchmark?