Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes 10k el
(version: 0)
Comparing performance of:
includes vs lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = new Array(10000).fill(undefined).map((x,i) => i); var b = new Set(a) var c = Math.floor(Math.random()*10000);
Tests:
includes
return a.includes(c)
lookup
return b.has(c)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
lookup
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 what's being tested in this JavaScript benchmark. **Benchmark Definition** The benchmark is designed to compare the performance of two approaches: 1. `array.includes(c)`: This checks if an element `c` exists within an array `a`. 2. `b.has(c)`: This checks if an element `c` exists within a Set `b`. **Script Preparation Code** The script preparation code creates: * An array `a` of 10,000 elements with indices from 0 to 9,999. * A Set `b` created from the elements in `a`. * A random integer `c` between 0 and 9,999. **Html Preparation Code** There is no HTML preparation code provided. **Options being compared** The benchmark compares two options: 1. Using `array.includes(c)` to check if an element exists within an array. 2. Using a Set (`b`) with the `has` method to check if an element exists. **Pros and Cons of each approach:** * **Array includes()**: + Pros: - Simple and easy to implement. - Works well for small arrays or fixed-size arrays. + Cons: - Performance degrades linearly with the size of the array, making it slower than Sets for large datasets. - May be less efficient due to potential unnecessary checks (e.g., checking if an element exists in the entire array). * **Set has()**: + Pros: - Efficient and fast, especially for large datasets, since Set operations are optimized. - Uses a hash table internally, which reduces the number of comparisons needed. + Cons: - Requires creating a Set from the data, which may be slower than simply checking each element in an array. **Other considerations:** * The use of a Set (`b`) with the `has` method is particularly efficient because it uses a hash table to store elements, allowing for fast lookups. * The random integer `c` is used to simulate the scenario where the target value is not necessarily the first element in the array or set. **Library and syntax:** The benchmark does not explicitly use any libraries. However, it relies on JavaScript's built-in `Set` data structure and its `includes()` method for arrays. No special JavaScript features or syntax are being tested in this benchmark. **Alternatives:** There are several alternatives to the approaches used in this benchmark: * For array-based lookups, consider using a data structure like a hash table (e.g., `Map`) or an indexed data structure (e.g., `ArrayBuffer`). * For Set-based lookups, consider using other Set-related methods, such as `has()` with a custom iterator, or implementing your own Set-like data structure. Keep in mind that these alternatives may not be directly comparable to the original approaches in terms of performance and readability.
Related benchmarks:
Set.has vs. Array.includes
set.has vs. array.includes - large array - random Access
map.has vs. array.includes - large array - random Access
set.has vs. array.includes (100000)
Comments
Confirm delete:
Do you really want to delete benchmark?