Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Map has vs array some in loop
(version: 0)
Benchmark.js
Comparing performance of:
Array some vs Map has
Created:
5 years ago
by:
Guest
Go to the latest result
Tests:
Array some
const referenceArray = [ {id:1},{id:2},{id:3},{id:4},{id:5},{id:6},{id:7},{id:8},{id:9},{id:0} ]; const inputArray = [ {id:5},{id:1},{id:10},{id:9},{id:5},{id:2},{id:17},{id:0},{id:4},{id:0} ]; inputArray.map(inp => ({...inp, isExisting: referenceArray.some(refA => refA.id === inp.id)}));
Map has
const referenceArray = [ {id:1},{id:2},{id:3},{id:4},{id:5},{id:6},{id:7},{id:8},{id:9},{id:0} ]; const inputArray = [ {id:5},{id:1},{id:10},{id:9},{id:5},{id:2},{id:17},{id:0},{id:4},{id:0} ]; const x = referenceArray.reduce((mapI, inp) => mapI.set(inp.id,inp), new Map()); inputArray.map(inp => ({...inp, isExisting: x.has(inp.id)}));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array some
Map has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Array some
5.9M/s
Map has
2.6M/s
View exact numbers
Test name
Executions per second
✓
Array some
5,902,884 Ops/sec
Map has
2,621,569 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Definition JSON** The benchmark definition is quite straightforward, which represents two different approaches to check if an element exists in an array or map: 1. `Array some`: This approach uses the `some()` method on the input array to check if any element matches a condition (in this case, the existence of an element with the same `id` in the reference array). 2. `Map has`: This approach uses the `has()` method on a Map object to check if a key (element's `id`) exists. **Options Compared** The two approaches being compared are: 1. Using the `some()` method on an array 2. Using the `has()` method on a Map **Pros and Cons of Each Approach:** 1. **Array some**: * Pros: + Simple to implement, as it only requires iterating over the input array. + Suitable for arrays with large numbers of elements. * Cons: + Less efficient than `Map has` because it requires scanning the entire array. 2. **Map has**: * Pros: + More efficient than `Array some`, especially when dealing with large datasets. + Can be faster due to caching and lookups on a Map object. * Cons: + Requires creating a temporary Map object, which may incur additional overhead. **Library or Special JS Feature** In this benchmark, no special JavaScript features or libraries are used beyond the standard `Map` and `Array` types. However, if we consider the `some()` method as a part of the standard library, it's still an interesting aspect to explore in terms of performance. **Other Considerations** When dealing with large datasets, the performance difference between these two approaches can be significant. In general: * If you're working with arrays and need to check if any element matches a condition, using `some()` might be a good choice. * When working with maps and need to look up specific keys, `has()` is usually a better option. **Alternatives** Other alternatives for this kind of benchmark include: 1. Using other methods on the input array or map, such as `every()`, `find()`, or `includes()`. 2. Implementing a custom function to check for element existence. 3. Using a different data structure altogether, like a Set or an object with specific properties. For this particular benchmark, measuring the performance difference between using `some()` and `Map has` provides valuable insights into how JavaScript engines handle array and map operations.
Related benchmarks
for vs map
map vs forEach Chris v2b
Map has vs get
new Map vs set array to map
Comments
Confirm delete:
Do you really want to delete benchmark?