Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs Set.has vas Map.has
(version: 0)
Comparing performance of:
Array includes vs Set has vs Map has
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = new Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); var c = new Map([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7], [8, 8], [9, 9], [10, 10]]);
Tests:
Array includes
return a.includes(9);
Set has
return b.has(9);
Map has
return c.has(9);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array includes
Set has
Map has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array includes
91008784.0 Ops/sec
Set has
193776864.0 Ops/sec
Map has
190644896.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of three different data structures in JavaScript: arrays, sets, and maps. The test cases focus on searching for an element within each data structure using the `has` method. **Options Being Compared** * **Array.includes**: This method checks if a specific element is present in the array. * **Set.has**: This method checks if a specific value is present in the set. * **Map.has**: This method checks if a specific key-value pair exists in the map. **Pros and Cons of Each Approach** 1. **Array.includes**: * Pros: Simple, widely supported, and fast for small arrays. * Cons: Slow for large arrays (O(n) search), may not be as efficient as other data structures for very large datasets. 2. **Set.has**: * Pros: Fast for sets with a limited number of elements (O(1) lookup). * Cons: May not be suitable for very large sets, and its performance degrades as the set size increases. 3. **Map.has**: * Pros: Fast for maps with a limited number of key-value pairs (O(1) lookup). * Cons: May not be suitable for very large maps, and its performance degrades as the map size increases. **Library Used** None explicitly mentioned in the provided benchmark definition and test cases. However, it's likely that the JavaScript engine being used (e.g., V8 in Chrome) provides an implementation of these data structures. **Special JS Feature/Syntax** The benchmark uses the `var` keyword for variable declarations, which is a legacy syntax in JavaScript. In modern JavaScript, you would typically use `let` or `const`. **Alternative Approaches** If you need to search for an element within a large dataset, other alternatives might include: 1. **Array.prototype.indexOf()**: Similar to `Array.includes`, but returns the index of the first occurrence instead of a boolean value. 2. **Binary Search**: For sorted arrays, binary search can be significantly faster than linear search (O(log n) vs O(n)). 3. **Data Structures like Hash Tables or Trie**: These data structures are optimized for fast lookups and might be more suitable for large datasets. Keep in mind that the best approach depends on the specific requirements of your use case and the characteristics of your dataset.
Related benchmarks:
Array.includes vs Set.has vs Map.has vs Object in
Array.includes vs Set.has vas Map.has vs Object accessor
Array.includes vs Set.has vas Map retrive
Array.includes vs Set.has vas Map.has 2
Comments
Confirm delete:
Do you really want to delete benchmark?