Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes v22
(version: 0)
Comparing performance of:
includes vs lookup
Created:
2 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]; 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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
87489672.0 Ops/sec
lookup
186865072.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the benchmark and its various components. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net, specifically comparing two approaches for checking if an element exists in an array: `array.includes()` and `set.has()`. This benchmark is designed to measure the performance difference between these two methods in a v22 version of JavaScript. **Script Preparation Code** The script preparation code creates two variables: 1. `a`: An array of 20 elements, containing numbers from 1 to 20. 2. `b`: A new Set object created from the elements of array `a`. Sets are a data structure in JavaScript that store unique values. ```javascript var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20]; var b = new Set(a); ``` **Html Preparation Code** There is no HTML preparation code provided for this benchmark. **Test Cases** The test cases are defined in the "Individual test cases" array: 1. **"includes"`**: This test case uses the `array.includes()` method to check if an element (9) exists in the original array `a`. The script will return a boolean value indicating whether 9 is present or not. 2. **"lookup"`**: Similar to the first test case, this one uses the `set.has()` method on the Set object `b` created earlier to check if the same element (9) exists. **Libraries and Features** * The `Set` data structure is a built-in JavaScript library for storing unique values. It's used in both test cases. * There are no specific features or syntaxes being tested in this benchmark, as it only compares two fundamental methods for checking existence. **Options Compared** The benchmark tests the performance of two options: 1. **`array.includes()`**: Checks if an element exists in the array by iterating through its elements. 2. **`set.has()`**: Checks if an element exists in a Set data structure by using a hash-based lookup. **Pros and Cons of Each Approach** * `array.includes()`: Pros: + Widely supported across browsers and environments. + Efficient for small to medium-sized arrays or sets. Cons: + Has a linear search complexity (O(n)) due to iterating through the array, which can be slower for large datasets. * `set.has()` : Pros: + Faster lookup time (O(1)) due to hash-based storage. + Suitable for storing unique values and checking membership efficiently. **Other Alternatives** If you need a different approach or performance optimization, consider: * Using other set data structures like `Map` or `Set-like objects`, which might offer better performance in specific use cases. * Investigating native WebAssembly (WASM) or browser-specific optimizations for improved performance on newer systems. * Implementing custom iterative algorithms that could potentially outperform the existing methods. Keep in mind that the choice of approach depends on your specific requirements, data size, and performance 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
Comments
Confirm delete:
Do you really want to delete benchmark?