Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map.has vs. array.includes - large array - random Access
(version: 0)
Comparing performance of:
includes vs lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = []; var b = new Map() for(let i = 0; i < 100000; i++){ a.push(i); b.set(i, Math.random()); }
Tests:
includes
return a.includes(Math.floor(Math.random()*100000))
lookup
return b.has(Math.floor(Math.random()*100000))
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 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0
Browser/OS:
Firefox 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
21757.3 Ops/sec
lookup
2182811136.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **What is tested?** The main difference between two approaches: using `array.includes()` versus `Map.has()`. Both methods are used to check if an element exists in a collection (an array or a Map, respectively). **Options being compared** There are two options: 1. **`array.includes()`**: A method that checks if an element is present in the given array. It returns `true` if the element is found and `false` otherwise. 2. **`Map.has()`**: A method on the Map object that checks if a key (element) exists in the map. **Pros and Cons of each approach** 1. **`array.includes()`** * Pros: + Widespread support across browsers and JavaScript engines. + Simple to understand and use. * Cons: + May be slower for large arrays due to the need to traverse the array from start to end. 2. **`Map.has()`** * Pros: + Generally faster than `array.includes()` for large datasets, as it uses a hash table lookup. * Cons: + Limited support across older browsers and JavaScript engines that don't natively support Maps (e.g., IE 11). **Library used** In this benchmark, the `Map` object is used from the ECMAScript standard library. **Special JS feature or syntax** None mentioned in the provided test cases. However, it's worth noting that `Map.has()` is a relatively new feature introduced in ECMAScript 2015 (ES6) and was not widely supported until recent browsers and JavaScript engines adopted it. **Other considerations** When choosing between `array.includes()` and `Map.has()`, consider the following: * If you're working with large datasets, `Map.has()` might be a better choice due to its performance advantages. * If you need to use this approach in older browsers or JavaScript engines that don't natively support Maps, you may need to consider alternative solutions. **Other alternatives** If you can't use `Map.has()`, other alternatives for checking if an element exists in a collection might include: 1. **`indexOf()`**: A method on arrays that returns the index of the first occurrence of the specified element, or -1 if it's not found. 2. **`in` operator**: Can be used to check if an element is present in an array using `for...in` loops (although this approach can be slower than others). However, these alternatives might not offer the same performance benefits as `Map.has()` for large datasets. I hope this explanation helps!
Related benchmarks:
Map vs Array vs Object vs Set add item speed in 50000 iters 2
new Array() vs Array.from() with random data
Array.find vs. Map.getss
Array.find vs. Map.get fork
Array.find vs. Map.get 300
Comments
Confirm delete:
Do you really want to delete benchmark?