Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes on 25 000 items
(version: 0)
Comparing performance of:
includes vs lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array(25000).fill(0).map((_,i) => i+=1) var b = new Set(a)
Tests:
includes
return a.includes(23250)
lookup
return b.has(23250)
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):
I'll break down the provided JSON and explain what's being tested, the options compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark is focused on comparing two approaches: 1. `array.includes()` (also known as "slice-based lookup") 2. `Set.has()` (a built-in JavaScript method for checking membership in a Set data structure) **Script Preparation Code** ```javascript var a = Array(25000).fill(0).map((_,i) => i+=1) var b = new Set(a) ``` This code creates an array `a` with 25,000 elements and fills them with values starting from 1 (since `i += 1`). It then creates a new Set `b` containing the same elements. **Html Preparation Code** There is no HTML preparation code provided, which means that this benchmark only tests the JavaScript performance of these two approaches. **Options Compared** The two options being compared are: 1. **Slice-based lookup (`array.includes()`):** This approach searches for a value in an array by iterating over the elements until it finds a match or reaches the end of the array. 2. **Set membership check (`Set.has()`):** This approach uses a data structure (a Set) to store unique values and checks if a value is present in the Set. **Pros and Cons** **Slice-based lookup (`array.includes()`):** Pros: * Widely supported by most browsers * Can be used with arrays of any size * Can be optimized for performance Cons: * Iterates over all elements, which can be slow for large datasets * May involve bounds checking (e.g., when searching within a range) **Set membership check (`Set.has()`):** Pros: * O(1) lookup time on average, making it much faster than slice-based lookup for large datasets * Efficient use of memory Cons: * Not all browsers support Set objects or the `has()` method * Requires creating and populating a Set with unique values (as in this benchmark) **Other Considerations** In general, when dealing with arrays, `slice-based lookup` might be preferred for smaller datasets due to its simplicity. However, as the dataset grows, `Set membership check` becomes more efficient. It's also worth noting that Sets are designed for fast lookups and insertion/removal operations, making them suitable for applications where data is frequently added or removed. In contrast, arrays are optimized for sequential access and manipulation. **Library or Framework** None explicitly mentioned in the benchmark definition. **Special JS Feature or Syntax** The `Map` object and the `Set` object are being used here to demonstrate the performance difference between these two approaches. Both of these data structures are built-in JavaScript objects that provide efficient lookup, insertion, and deletion operations. In the context of this benchmark, using a Set (`b`) is crucial for achieving the expected O(1) lookup time. Without it, the comparison would not be fair or meaningful. If you're interested in exploring other JavaScript features, I can discuss some alternatives, such as: * Using `Map` objects instead of Sets * Comparing different array methods (e.g., `indexOf()`, `findIndex()`) * Testing browser-specific optimizations (e.g., WebAssembly) Feel free to ask if you'd like me to elaborate on any of these points!
Related benchmarks:
set.has vs. array.includes 2
set.has vs. array.includes (10000 items)
Includes (array) vs Has (Set)
set.has vs. array.includes for 1000 elements
Comments
Confirm delete:
Do you really want to delete benchmark?