Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array find VS Array indexOf VS Map has VS Map get VS Set has
(version: 0)
Comparing performance of:
Array find vs Array indexOf vs Map has vs Map get vs Set has
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Array find
const positions = Array.from({ length: 10_000 }, () => [Math.random(), Math.random()]); const existing = []; let counter = 0; for (const p of positions) { existing.push([p[0], [1]]); } for (const p of positions) { const exists = existing.find((e) => e[0] === [0] && e[1] === p[1]); }
Array indexOf
const positions = Array.from({ length: 10_000 }, () => [Math.random(), Math.random()]); const existing = []; let counter = 0; for (const p of positions) { existing.push(`${p[0]}-${p[1]}`); } for (const p of positions) { const exists = existing.indexOf(`${p[0]}-${p[1]}`) > -1; if (exists) { counter++; } }
Map has
const positions = Array.from({ length: 10_000 }, () => [Math.random(), Math.random()]); const existing = new Map(); let counter = 0; for (const p of positions) { existing.set(`${p[0]}-${p[1]}`, true); } for (const p of positions) { const exists = existing.has(`${p[0]}-${p[1]}`); if (exists) { counter++; } }
Map get
const positions = Array.from({ length: 10_000 }, () => [Math.random(), Math.random()]); const existing = new Map(); let counter = 0; for (const p of positions) { existing.set(`${p[0]}-${p[1]}`, true); } for (const p of positions) { const exists = existing.get(`${p[0]}-${p[1]}`) === true; if (exists) { counter++; } }
Set has
const positions = Array.from({ length: 10_000 }, () => [Math.random(), Math.random()]); const existing = new Set(); let counter = 0; for (const p of positions) { existing.add(`${p[0]}-${p[1]}`, true); } for (const p of positions) { const exists = existing.has(`${p[0]}-${p[1]}`); if (exists) { counter++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Array find
Array indexOf
Map has
Map get
Set has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36
Browser/OS:
Chrome 148 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array find
0.7 Ops/sec
Array indexOf
1.4 Ops/sec
Map has
79.1 Ops/sec
Map get
83.7 Ops/sec
Set has
88.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark. **What is being tested?** The provided JSON represents a set of JavaScript microbenchmarks that test the performance of different data structures and methods in JavaScript: 1. **Array `find`**: Tests the performance of finding an element in an array using the `find()` method. 2. **Array `indexOf`**: Tests the performance of finding an index of an element in an array using the `indexOf()` method. 3. **Map `has`**: Tests the performance of checking if a key exists in a Map object using the `has()` method. 4. **Map `get`**: Tests the performance of retrieving the value associated with a key in a Map object using the `get()` method. 5. **Set `has`**: Tests the performance of checking if an element exists in a Set object using the `has()` method. **Options compared** In each test case, multiple options are being compared: * Array vs. Map for storing data (e.g., `find`, `has`) * Using arrays with string values (`"String-based"` vs. just pushing numbers) for array tests * Using arrays or Sets to store data and checking existence **Pros and Cons of each approach** Here's a brief summary of the pros and cons of each approach: 1. **Array `find`**: * Pros: Simple, widely supported. * Cons: O(n) complexity, can be slow for large datasets. 2. **Array `indexOf`**: * Pros: Fast, linear search (O(n)). * Cons: Can be slower than `find()` for smaller arrays due to the search algorithm. 3. **Map `has`**: * Pros: Fast, O(1) complexity on average. * Cons: May have higher memory overhead due to the Map data structure. 4. **Map `get`**: * Pros: Similar to `has`, but retrieves the value associated with a key. * Cons: Can be slower than just checking existence if no value is present. 5. **Set `has`**: * Pros: Fast, O(1) complexity on average. * Cons: May have higher memory overhead due to the Set data structure. **Other considerations** When choosing a data structure for storing and searching data, consider factors like: * Data size and complexity * Performance requirements (e.g., speed vs. memory usage) * Memory constraints Keep in mind that JavaScript engines may implement optimizations or caching mechanisms that affect performance in different scenarios. **Alternative approaches** If you're looking for alternative approaches to these microbenchmarks, consider exploring other data structures like: * Linked lists * Trees (e.g., binary search trees) * Hash tables (which are similar to Maps but optimized for fast lookups) You can also experiment with caching or memoization techniques to reduce the number of computations performed during benchmarking. That's a wrap!
Related benchmarks:
array first vs find
Array find with indexOf vs includes
set vs array find if exists
set vs array find if exists v2
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?