Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes perf
(version: 0)
set.has vs. array.includes perf
Comparing performance of:
Includes vs lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = new Set(a)
Tests:
Includes
return a.includes(9)
lookup
return b.has(9)
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 the benchmark and explain what's being tested. **Benchmark Overview** The test compares the performance of two approaches: `Array.prototype.includes()` and `Set.prototype.has()`. Both methods are used to check if an element exists in a dataset (array or set, respectively). **Options Compared** * `Array.prototype.includes()`: This method checks if a specific value (`9` in this case) is present in the array. * `Set.prototype.has()`: This method checks if a specific value (`9`) is present in the set. **Pros and Cons of Each Approach** * `Array.prototype.includes()`: + Pros: Widely supported, easy to use, and well-documented. It's also relatively fast for small to medium-sized datasets. + Cons: Can be slower than `Set.prototype.has()` for large datasets because it needs to iterate over the entire array to find the element. * `Set.prototype.has()`: + Pros: Generally faster than `Array.prototype.includes()` for large datasets because it uses a data structure (hash table) that allows for efficient lookups. It's also more memory-efficient than arrays. + Cons: Less widely supported and may require additional setup to use. It requires the `Set` object, which may not be available in older browsers. **Library and Its Purpose** In this benchmark, the `Set` library is used to create a set from an array (`var b = new Set(a)`). The `Set` object is a data structure that stores unique values, allowing for efficient lookups using the `has()` method. **Special JavaScript Feature or Syntax** This benchmark uses a standard JavaScript feature: arrays and sets. No special syntax or features are required to run this benchmark. **Other Alternatives** If you want to test alternative approaches, here are a few options: * Using `indexOf()` instead of `includes()`: This will add an additional comparison step, making it slower than `includes()`. * Using a custom data structure (e.g., a binary search tree) for lookups: This would require significant changes to the code and may not be practical for a simple benchmark. * Using a different data structure (e.g., a linked list) instead of an array: This would also require changes to the code and may impact performance. Keep in mind that these alternatives are more complex and may not be representative of real-world use cases. The comparison between `includes()` and `has()` is a simple and straightforward way to demonstrate the performance difference between these two methods.
Related benchmarks:
convert to set + set.has vs. array.includes
set.has vs. array.includes Perf Random
set.has vs. array.includes Perf Random 1
Includes (array) vs Has (Set)
Comments
Confirm delete:
Do you really want to delete benchmark?