Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has() vs. array.includes()
(version: 0)
Comparing performance of:
includes vs lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,1, 2, 3, 4, 5, 6, 7, 8, 9, 10,1, 2, 3, 4, 5, 6, 7, 8, 9, 10,1, 2, 3, 4, 5, 6, 7, 8, 9, 10,1, 2, 3, 4, 5, 6, 7, 8, 9, 10,1, 2, 3, 4, 5, 6, 7, 8, 9, 10,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 dive into the world of JavaScript microbenchmarks. **What is being tested?** The provided benchmark tests two different approaches to check if an element exists in an array or a Set object: 1. `array.includes()` vs `set.has()` In the first test case, the script creates an array `a` with 20 occurrences of the number 9 and then checks if `9` is included in the array using the `includes()` method. In the second test case, the same script creates a Set object `b` from the array `a` and then checks if `9` is present in the set using the `has()` method. **Options compared:** The two options being compared are: * `array.includes()`: A method that returns a boolean value indicating whether an element exists in the array. * `set.has()`: A method that returns a boolean value indicating whether an element exists in the set. **Pros and Cons of each approach:** **Array includes():** Pros: * Widely supported and implemented in most JavaScript engines. * Can be used to check if an element exists in any type of array (not just numerical arrays). * Returns a boolean value, which can be useful for conditional statements. Cons: * May have performance issues when dealing with very large arrays or sets due to the need to iterate over all elements. * May not be as efficient as `set.has()` because it requires an explicit iteration over the array. **Set has():** Pros: * Generally faster than `array.includes()` for checking membership in a set because it can take advantage of the underlying data structure's optimized lookup mechanisms. * Can be more memory-efficient than creating and iterating over large arrays. Cons: * Less widely supported and implemented, which may lead to browser or engine compatibility issues. * May not work as expected with non-set data structures (e.g., arrays, objects). * Returns a boolean value, similar to `array.includes()`. **Library and purpose:** The Set library is built into modern JavaScript engines and provides an efficient way to store and manipulate collections of unique values. Its primary purpose is to offer a fast and memory-efficient way to check for membership in a collection of elements. **Special JS feature or syntax:** None mentioned in this benchmark. **Other alternatives:** If you need to check if an element exists in a set, but the `set.has()` method is not available, you can use other approaches like: * Using the `Array.prototype.indexOf()` method (although slower than `array.includes()`) or `Array.prototype.findIndex()` method. * Creating a custom function to iterate over the set and check for membership. For checking if an element exists in an array, you can use other approaches like: * Using the `Array.prototype.lastIndexOf()` method. * Creating a custom function to iterate over the array and check for membership. Keep in mind that these alternatives may not be as efficient or convenient as using the built-in methods provided by modern JavaScript engines.
Related benchmarks:
set.has vs. array.includes bigger sample
set.has vs. array.includes (with big array)
set.has vs. array.includes (with big array and last index match)
set.has vs. array.includes (300 elements)
Large containers non existing Array.includes vs Set.has vas Map.has
Comments
Confirm delete:
Do you really want to delete benchmark?