Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS find vs indexOf vs Map
(version: 0)
JS find vs indexOf
Comparing performance of:
Array.find vs Array.indexOf vs dsds
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var ms = new Map(); var i = 0; while (i <= 1E5) { arr[i] = i; ms.set(i, i); i++; }
Tests:
Array.find
const item = arr.find(item => item == 1E5);
Array.indexOf
const index = arr.indexOf(1E5);
dsds
ms.has(1E5 - 1)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.find
Array.indexOf
dsds
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/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.find
2002.4 Ops/sec
Array.indexOf
87336.4 Ops/sec
dsds
24850028.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Overview** The benchmark measures the performance of three different ways to find an element in an array or a Map: `find`, `indexOf`, and direct key lookup using a Map (`ms.has`). **Script Preparation Code** The script preparation code sets up two data structures: 1. An empty array `arr` with 100,000 elements, initialized with values from 0 to 99,999. 2. A new Map `ms` with the same keys as `arr`, but initialized with the same values. **Individual Test Cases** There are three test cases: ### Array.find * Benchmark Definition: `const item = arr.find(item => item == 1E5);` * Purpose: This test case measures the performance of the `find` method, which returns the first element in the array that satisfies the provided condition. * Pros and Cons: + Pros: The `find` method is a concise and expressive way to find an element in an array. It's also less prone to off-by-one errors compared to indexing-based approaches like `indexOf`. + Cons: If the array is very large, iterating over all elements until finding the match can be slow. * Notes: This test case uses a simple equality comparison (`item == 1E5`) to find the target element. ### Array.indexOf * Benchmark Definition: `const index = arr.indexOf(1E5);` * Purpose: This test case measures the performance of the `indexOf` method, which returns the first index at which the specified value can be found in the array. * Pros and Cons: + Pros: The `indexOf` method is a well-established and widely supported way to find an element in an array. It's also relatively fast compared to iterating over all elements. + Cons: If the target value is not found, it throws an error. * Notes: This test case uses direct indexing (`arr.indexOf(1E5)`), which may not be as efficient as using a `find` or `map` method for large arrays. ### Map lookup (dsds) * Benchmark Definition: `ms.has(1E5 - 1)` * Purpose: This test case measures the performance of direct key lookup using a Map. * Pros and Cons: + Pros: Direct key lookup is generally very fast, especially when used with optimized data structures like Maps. + Cons: The comparison operator (`==`) may not be as efficient as other methods for finding matches in large arrays. * Notes: This test case uses the `has` method of the Map to check if a key exists. Note that this assumes that the keys are hashed and stored in an optimized format. **Browser and Device Information** The benchmark result shows data from multiple browsers (Chrome 125 on Mac OS X 10.15.7) with different execution frequencies per second. In summary, this benchmark measures the performance of three different methods for finding an element in a large array or Map: `find`, `indexOf`, and direct key lookup using a Map. The results provide insight into the relative efficiency of these approaches on modern browsers.
Related benchmarks:
findIndex vs map & indexOf
indexOf vs findIndex with a simple case
Array find with indexOf vs includes
findIndex vs IndexOf + map
findIndex vs indexOf - JavaScript performance v2
Comments
Confirm delete:
Do you really want to delete benchmark?