Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes (1m)
(version: 0)
Comparing performance of:
includes vs lookup
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [...Array(5).keys(10000)]; var b = new Set(a)
Tests:
includes
return a.includes(9000)
lookup
return b.has(9000)
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 Purpose:** The benchmark is designed to compare two approaches for searching an array of numbers within it: 1. Using `Array.prototype.includes()` 2. Using `Set` data structure with `has()` method **Options Compared:** * Option 1: `Array.prototype.includes()`. This method iterates over the array until it finds the specified value (in this case, `9000`) or reaches the end of the array. * Option 2: Using a `Set` data structure with the `has()` method. A `Set` is an unordered collection of unique values, which makes it suitable for fast membership testing. **Pros and Cons of Each Approach:** 1. **Array.prototype.includes()** * Pros: + Widely supported across different browsers and JavaScript engines. + Easy to implement. + Cons: + Iterates over the entire array, making it less efficient for large datasets. 2. **Set with has() method** * Pros: + Fast membership testing (O(1) average time complexity). + Can be more efficient for large datasets. * Cons: + Requires creating a `Set` object from the array, which may add overhead. + Not all browsers or JavaScript engines support `Set` data structures. **Library:** In this benchmark, the `Set` library is used to create a `Set` object from the array. The `has()` method is then used to search for the value in the set. This approach relies on the underlying implementation of the `Set` data structure in each browser or JavaScript engine. **Special JS Feature/Syntax:** None mentioned in this benchmark. **Benchmark Preparation Code:** The script preparation code creates an array of 10,000 numbers from 0 to 9,999 using `Array.from()`. A new `Set` object is then created from this array using the spread operator (`...`). This sets up the data structure for testing both methods. **Other Alternatives:** * Other search algorithms or data structures, such as binary search or hash tables, could also be used to compare with these approaches. * Using a library like Lodash or Ramda, which provides utility functions for array and set operations, might offer additional performance benefits or simplified code. In summary, this benchmark compares the performance of two approaches for searching an array: `Array.prototype.includes()` and using a `Set` data structure with `has()` method. The goal is to determine which approach is faster and more efficient for large datasets.
Related benchmarks:
convert to set + set.has vs. array.includes
set.has vs. array.includes (large)
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?