Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.includes vs set.has 2022
(version: 0)
Comparing performance of:
includes vs lookup
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = []; for (var i = 0; i < 100000; i++) { a.push(i); } var b = new Set(a)
Tests:
includes
return a.includes(99999)
lookup
return b.has(99999)
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 provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches: using `Array.includes()` (test case "includes") versus using a `Set` data structure (`b.has()`) for fast membership testing. The test creates an array `a` with 100,000 elements and then converts it into a set `b`. **Comparison Options** There are two comparison options: 1. **`Array.includes()`**: This method checks if a value exists in the array by iterating through each element until it finds a match or reaches the end of the array. 2. **`Set.has()`**: This method checks if a value exists in the set by using a hash table to store elements and then checking for membership. **Pros and Cons** * **`Array.includes()`**: + Pros: Simple, widely supported, and works well for small arrays or arrays with few unique values. + Cons: Can be slow for large arrays or arrays with many duplicate values, as it needs to iterate through each element. * **`Set.has()`**: + Pros: Fast lookup times, especially for large sets or arrays with many unique values. Sets are also useful for removing duplicates and preserving uniqueness. + Cons: Requires creating a new set data structure, which can be memory-intensive. Also, not all browsers support sets natively. **Library and Purpose** In this benchmark, the `Set` library is used to create a set data structure (`b`) from an array (`a`). The purpose of using a set here is to leverage its fast lookup times for membership testing. **Special JavaScript Feature or Syntax** There is no special JavaScript feature or syntax being tested in this benchmark. However, it's worth noting that sets are a built-in data structure in modern JavaScript and are widely supported across most browsers. **Other Alternatives** For fast membership testing, other alternatives to `Array.includes()` and `Set.has()` include: * **`Map.get()`**: Similar to `Set.has()`, but uses a map data structure instead of a set. Maps can be more memory-efficient than sets for large datasets. * **`Array.prototype.indexOf()`** or **`Array.prototype.lastIndexOf()`**: These methods search for the first or last occurrence of a value in an array, respectively. They can be faster than `Array.includes()` but may not be as efficient for very large arrays. Overall, this benchmark provides a useful comparison between two common approaches to fast membership testing in JavaScript, and it highlights the trade-offs between simplicity, memory usage, and performance.
Related benchmarks:
set.has vs. array.includes (1 million entries)
set.has vs. array.includes - large array
set vs array iteration 100k elements
set vs array iteration new new
Comments
Confirm delete:
Do you really want to delete benchmark?