Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes on big arrays s
(version: 0)
Comparing performance of:
includes vs lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array.from({ length: 1000 }, (_, i) => i + 1) var b = new Set(a)
Tests:
includes
return a.includes(900)
lookup
return b.has(900)
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 its results. **Benchmark Definition** The benchmark measures the performance of two JavaScript methods: `array.includes()` and `Set.has()`. The script preparation code creates an array `a` with 1000 elements, each incremented by 1, and converts it to a set `b`. The purpose is to test which method is faster when searching for a specific element (900) within these large arrays/set. **Options Compared** Two options are compared: 1. **array.includes()**: This method checks if an element with the specified value (`900`) exists in the array. 2. **Set.has()**: This method checks if an element with the specified value (`900`) exists in the set `b`. **Pros and Cons of Each Approach** * **Array Includes()** * Pros: * Widely supported across modern browsers. * Efficient for small to medium-sized arrays. * Easy to implement. * Cons: * Can be slower for very large arrays due to the need to scan the entire array. * **Set Has()** * Pros: * Much faster for large datasets since sets are optimized for fast lookups. * More memory-efficient than arrays for storing unique values. * Cons: * Requires a set implementation, which can be slower than native array methods. * May not be supported in older browsers. **Library and Purpose** The `Set` data structure is used to store unique values. In this benchmark, it's created from the array `a`. Sets are useful when you need to quickly check if an element exists without duplicates or order. **Special JS Feature/Syntax** This benchmark uses a modern JavaScript feature: the **`Array.from()` method**, which creates a new array from an iterable (e.g., object, string) and generates a sequence of values. It's used in the script preparation code to create the large array `a`. **Other Alternatives** If you need alternative methods for searching elements within arrays or sets, consider: * **Binary Search**: For sorted arrays, this method can be faster than linear search (e.g., `includes()`). * **Hash Table Lookups**: Instead of using a set, you could use an object with hash table lookups. This approach is more memory-intensive but might offer better performance in certain scenarios. * **Native Methods**: Depending on the specific browser and JavaScript engine used, other native methods like `indexOf()`, `includes()` variants (e.g., `Array.prototype.includes().namedCapture`), or even some custom solutions using SIMD instructions might be faster. Keep in mind that these alternatives may not always outperform the original approach used here, especially considering polyfill support for older browsers.
Related benchmarks:
convert to set + set.has vs. array.includes
Includes (array) vs Has (Set)
array.includes vs set.has for small-ish n
set.has vs. array.includes (find 999,999 in 1,000,000)
Comments
Confirm delete:
Do you really want to delete benchmark?