Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has (w/ creation) vs. array.includes
(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];
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:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 121 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
7826860.0 Ops/sec
lookup
1167185.6 Ops/sec
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 comparing two approaches for checking if an element exists in an array: `array.includes` vs. creating a new `Set` instance from the array and using the `has` method on it (`set.has(9)`). The goal is to determine which approach is faster. **Options Compared** There are two options being compared: 1. **array.includes**: This method checks if an element exists in the array by iterating through the elements and checking for a match. 2. **Set-based approach (`set.has(9)`)**: This method creates a new `Set` instance from the array and uses the `has` method to check if the target element (in this case, 9) is present in the set. **Pros and Cons** **array.includes**: Pros: * Widely supported across browsers and environments * Simple to implement Cons: * May have slower performance compared to the set-based approach for large datasets * Requires iteration through the array elements, which can be expensive for large arrays **Set-based approach (`set.has(9)`)**: Pros: * Can be faster than `array.includes` for large datasets due to the use of a data structure that allows for efficient lookups (sets are optimized for fast membership testing) * Can be more memory-efficient as it only stores unique elements from the original array Cons: * May require additional overhead for creating and managing the set * Not all browsers or environments support sets or the `has` method **Other Considerations** The benchmark also uses a JavaScript script preparation code to create an array of 10 elements, which is used as the input data for both tests. The HTML preparation code is empty, suggesting that the browser's default document structure is being used. **Library/Feature Used** In this case, no specific library or feature is being tested beyond the standard JavaScript features mentioned earlier (arrays, sets, includes method). However, it's worth noting that some browsers may have additional optimizations or features for arrays and sets that could affect performance. For example, modern browsers like Chrome and Safari have optimized array methods like `includes` and `findIndex`, which can provide better performance than the basic implementation. **Alternatives** If you wanted to write a similar benchmark, here are some alternative approaches: 1. Use `array.findIndex` instead of `includes`, as it returns the index of the first matching element or -1 if not found. 2. Compare the performance of other array methods like `some()`, `every()`, or `filter()` against `array.includes`. 3. Test the performance of using a custom data structure, like a hash table, instead of an array. 4. Consider benchmarking additional features or libraries that might affect performance in similar scenarios. By exploring these alternatives and understanding the trade-offs involved, you can gain a deeper appreciation for the nuances of JavaScript performance optimization and how different approaches can impact your application's speed and efficiency.
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?