Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs Set.has vs Map.has vs Object in
(version: 0)
Comparing performance of:
Array includes vs Set has vs Map has vs object key
Created:
2 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]]); var d = {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);
object key
return "9" in d
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array includes
Set has
Map has
object key
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array includes
110344064.0 Ops/sec
Set has
194053648.0 Ops/sec
Map has
192628208.0 Ops/sec
object key
190403408.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **What is being tested?** The provided benchmark tests the performance of four different approaches for checking if a value exists in an object, array, set, or map: 1. `Array.includes()` 2. `Set.has()` 3. `Map.has()` 4. `object key` (using the "in" operator) **Options compared** Each test case compares two approaches: one using the built-in method and another using a direct property access. **Pros and Cons of each approach:** 1. **Array.includes()** * Pros: + Widely supported in modern browsers. + Often optimized for performance. * Cons: + May not work as expected with sparse arrays or large arrays. + Can be slower due to iteration over the array elements. 2. **Set.has()** * Pros: + Fast lookup times, especially for large sets. + Less prone to errors compared to direct property access. * Cons: + Not supported in older browsers (IE 11 and below). + May not work well with non-numeric keys or values. 3. **Map.has()** * Pros: + Fast lookup times, similar to Set.has(). + Suitable for large maps with many key-value pairs. * Cons: + Less supported compared to Sets (not in older browsers). + May not work well with non-numeric keys or values. 4. **object key** * Pros: + Widely supported across different browsers and versions. + Often faster than built-in methods for small objects. * Cons: + Can be slower for large objects due to property iteration. **Special JS features or syntax** None of the tested approaches rely on special JavaScript features like async/await, Promises, or modern JavaScript syntax. **Library usage** No libraries are used in these benchmark test cases. However, if you're interested in using a library like Lodash for utility functions, it can impact performance and may not be suitable for microbenchmarking. **Other considerations** When choosing an approach, consider the following factors: * Performance requirements: If speed is crucial, Set.has() or Map.has() might be a better choice. * Browser support: Make sure to test on different browsers and versions to ensure compatibility. * Object size: For small objects, object key might be faster than built-in methods. For larger objects, the performance difference may not be noticeable. **Alternatives** Other alternatives for checking if a value exists in an object or array include: 1. **Object.prototype.hasOwnProperty.call()**: A more explicit and safer approach. 2. **for...in loop**: Iterating over the object's properties using a for...in loop can also work, but it may not be as efficient. 3. **Array.prototype.findIndex()**: This method returns the index of the first element that satisfies the condition or -1 if no element matches. Keep in mind that these alternatives might have different performance characteristics and are not always suitable for microbenchmarking.
Related benchmarks:
Array.includes vs Set.has vas Map.has
Includes (array) vs Has (Map)
Array.includes vs Set.has vas Map.has vs Object accessor
Array.includes vs Set.has vas Map retrive
Comments
Confirm delete:
Do you really want to delete benchmark?