Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.includes vs set.has for small-ish n
(version: 0)
array.includes vs set.has for small n
Comparing performance of:
array includes vs set has
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array.from({ length: 100 }, (_, i) => i) var b = new Set(a)
Tests:
array includes
return a.includes(49)
set has
return b.has(51)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array includes
set has
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 Definition** The benchmark is comparing two methods: 1. `Array.includes()` (test case: "array includes") 2. `Set.has()` (test case: "set has") Both methods are used to check if a specific element exists within an array or set, respectively. **Options compared** We have two options being compared: * Using `Array.includes()` with an array of 100 random integers (`a`) * Using `Set.has()` with the same array converted to a set (`b`) **Pros and Cons** * **Array.includes()**: + Pros: widely supported, easy to use, works well for small arrays. + Cons: might be slower for larger arrays due to its iterative nature. * **Set.has()**: + Pros: potentially faster than `Array.includes()` for large arrays, since sets are optimized for fast lookups. + Cons: not all browsers support it (only Firefox 109 in this benchmark), and some might perform poorly with very large sets. **Library usage** In the script preparation code, we see that `Array.from()` is used to create a new array from an object. This is not a library in itself, but rather a method on the global `Array` prototype (specifically, a factory function introduced in ECMAScript 2015). It's often referred to as a "utility function" or "array constructor". **Special JavaScript features** None of the code uses any special JavaScript features like async/await, arrow functions, destructuring, or any other advanced syntax. However, it does use a feature that's not well-known: `__proto__`. In JavaScript, every object has a `[[Prototype]]` property, which is a reference to its prototype chain. By accessing this property directly (`b.__proto__`), we can access the properties of the original array (`a`) as if it were an object. **Other alternatives** If you wanted to rewrite this benchmark using alternative methods, here are some possibilities: * Instead of `Array.includes()`, you could use a traditional loop to iterate over the array and check each element. * For `Set.has()`, you could use a different data structure like a hash table or an object with key-value pairs to store the elements. Keep in mind that these alternatives might not be as efficient or convenient to use, especially for large arrays. The original code using `Array.includes()` and `Set.has()` is already quite optimized for performance.
Related benchmarks:
array.includes vs set.has for small n
set.has vs. array.includes on big arrays
set.has vs. array.includes on big arrays s
array.includes vs set.has for medium n
Comments
Confirm delete:
Do you really want to delete benchmark?