Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (run new Set in has test)
(version: 0)
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];
Tests:
includes
return a.includes(9)
lookup
var b = new Set(a) 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, compared, and their pros and cons. **Benchmark Name:** `set.has vs. array.includes (run new Set in has test)` This benchmark compares two approaches for checking if an element exists in a JavaScript array: using the `includes()` method on the array itself, and creating a new `Set` instance from the array and then using the `has()` method on the set. **Options Compared:** 1. **Array `includes()` method**: This approach checks if a specific value is present in the array by iterating through its elements. 2. **Creating a new Set instance**: Creates a new Set instance from the array and uses the `has()` method to check for the presence of an element. **Pros and Cons:** 1. **Array `includes()` method**: * Pros: + Simple and straightforward approach. + Works well for small to medium-sized arrays. * Cons: + Iterates through all elements in the array, which can be inefficient for large datasets. + May have performance issues if the array is very large or sparse. 2. **Creating a new Set instance**: * Pros: + More efficient than iterating through the entire array, as sets use a hash table to store unique values. + Can take advantage of CPU caching and other optimizations for better performance. * Cons: + Requires creating an additional object (the set), which may incur overhead in terms of memory allocation and garbage collection. + May not be suitable for very large datasets, as the set's internal representation can become too complex to manage. **Other Considerations:** * For small arrays or arrays with a small number of unique elements, the difference between these two approaches might be negligible. However, for larger datasets, creating a new Set instance is likely to outperform the array `includes()` method. * If you need to frequently add or remove elements from the array, using an array with a `Set` as its underlying data structure (e.g., using `Array.from()` and `Set`) might provide better performance and scalability. **Library:** None of the benchmark tests explicitly use any external libraries. However, creating a new Set instance involves the internal `Set` class in JavaScript. **Special JS Feature or Syntax:** This benchmark uses standard JavaScript features and syntax without any special or experimental features. Now, let's look at the individual test cases: 1. **`includes`**: * This test case measures the performance of checking if a specific element is present in an array using the `includes()` method. 2. **`lookup`**: * This test case measures the performance of creating a new Set instance from an array and then using the `has()` method to check for the presence of an element. The latest benchmark result shows that Chrome 96 outperforms itself when running the `lookup` test, while still performing slower than itself when running the `includes` test. This may indicate that there are issues with the execution or measurement of this specific benchmark case.
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?