Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (40)
(version: 0)
Comparing performance of:
includes vs lookup
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,37, 38, 39, 40]; var b = new Set(a)
Tests:
includes
return a.includes(39)
lookup
return b.has(39)
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, compared, and considered. **Benchmark Definition** The benchmark is testing two approaches to check if an element exists in an array or set: 1. `array.includes(element)`: This method checks if a specific value (`element`) exists in the given array. 2. `set.has(element)`: This method checks if a specific value (`element`) exists in the given set. The benchmark is specifically testing these two approaches on a large array of 40 elements and comparing their performance using JavaScript's built-in `Set` data structure. **Options Compared** The two options being compared are: * `array.includes(element)`: This approach uses a linear search algorithm to find the presence of an element in the array. It has a time complexity of O(n), where n is the size of the array. * `set.has(element)`: This approach uses a hash table data structure to store elements, allowing for constant-time lookups (O(1)). However, it's more memory-intensive since sets require storing all unique elements. **Pros and Cons** * **array.includes(element)**: + Pros: widely supported, easy to implement, doesn't require extra memory. + Cons: linear search algorithm can be slow for large arrays, may cause performance issues due to unnecessary comparisons. * **set.has(element)**: + Pros: constant-time lookups, more efficient for large datasets, uses less memory since sets only store unique elements. + Cons: requires JavaScript's built-in `Set` data structure, which might not be available in older browsers or environments. **Library and Purpose** In this benchmark, the `Set` library is used to create a set from the given array. The purpose of using a set here is to demonstrate how efficient constant-time lookups can be when compared to linear search algorithms like `includes`. **Special JS Feature or Syntax** There's no special JavaScript feature or syntax being tested in this benchmark, as it only involves standard language constructs and built-in data structures. **Other Alternatives** If you were to rewrite the benchmark using alternative approaches: * For large arrays, you might consider using a more efficient algorithm like binary search. * For sets, you could use other data structures like balanced trees (e.g., AVL trees or red-black trees) instead of hash tables. * You could also explore using external libraries or frameworks that provide optimized set implementations. Keep in mind that the specific alternatives will depend on your performance requirements and constraints.
Related benchmarks:
convert to set + set.has vs. array.includes
Includes (array) vs Has (Set)
array.includes vs. set.has on the fly
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?