Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
zzzzzzzzzz
(version: 0)
Comparing performance of:
IndexOf vs findIndex
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var haystack = new Array(1000).fill().map((nop, i) => i); var needles = new Array(500).fill().map(() => Math.floor(Math.random() * 1000)); function equalSearch(values, needle, params = {}) { // Retrieve last index var last = values.length - 1; // Init params var { compare, from: start = 0, to: end = last, } = params; // Outbounds ? if (start < 0 || end > last) { return -1; } // No compare callback ? if (!compare) { // Set default compare function compare = typeof needle === 'string' || needle instanceof String ? (a, b) => a.localeCompare(b) : (a, b) => a - b; } // Do search ... while (start <= end) { // Get the middle var middle = Math.floor((start + end) / 2); // Compare values var result = compare(needle, values[middle]); // Find value ? if (!result) { return middle; } // Lower ? if (result < 0) { end = middle - 1; // ... greather } else { start = middle + 1; } } return -1; }
Tests:
IndexOf
needles.forEach(needle => haystack.indexOf(needle));
findIndex
needles.forEach(needle => haystack.findIndex(item => item === needle));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
IndexOf
findIndex
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON data. **Benchmark Definition** The benchmark definition is a JavaScript function `equalSearch` that performs a search operation on an array of values (`haystack`) to find a specific value (`needle`). The function takes an optional `params` object as a parameter, which allows users to customize the search behavior. The `params` object can have two properties: `compare` and `from`, `to`. If `compare` is not provided, it defaults to a locale-based comparison for strings or a simple subtraction for numbers. **Options Compared** The benchmark compares two different search approaches: 1. **indexOf()**: The `indexOf()` method returns the index of the first occurrence of the specified value (`needle`) in the array (`haystack`). If the value is not found, it returns -1. 2. **findIndex()**: The `findIndex()` method returns the index of the first occurrence of the specified value (`needle`) in the array (`haystack`) that satisfies a given condition (in this case, `item === needle`). **Pros and Cons** Here are some pros and cons of each approach: * **indexOf()**: + Pros: Generally faster than `findIndex()` because it returns as soon as it finds the first match. + Cons: Returns the index of the first occurrence, but may not be useful if you want to iterate over all occurrences or use the index in further calculations. * **findIndex()**: + Pros: Allows for a more flexible condition and can be used to find any occurrence that satisfies the condition, not just the first one. However, it returns -1 if no match is found. + Cons: Generally slower than `indexOf()` because it needs to iterate over the entire array to find all matches. **Library** The benchmark uses two built-in JavaScript methods: * **Array.prototype.indexOf()**: Returns the index of the first occurrence of a specified value in an array. It's a part of the Array prototype. * **Array.prototype.findIndex()**: Returns the index of the first occurrence of a specified condition in an array. It's also a part of the Array prototype. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax used in this benchmark. **Other Considerations** When choosing between `indexOf()` and `findIndex()`, consider the following: * If you only need to find the first occurrence, `indexOf()` might be a better choice. However, if you need to iterate over all occurrences or use the index in further calculations, `findIndex()` is a better option. * If performance is critical, using `indexOf()` can be faster because it returns as soon as it finds the first match. **Alternatives** If you're looking for alternative search methods, consider: * **Binary Search**: A more efficient algorithm that uses a divide-and-conquer approach to find an element in a sorted array. However, this requires the array to be sorted. * **Linear Search**: Another simple approach that iterates over the entire array to find an element. It's less efficient than `indexOf()` and `findIndex()`, but can still be useful for small arrays or when performance is not critical. In summary, the benchmark compares two common search methods in JavaScript: `indexOf()` and `findIndex()`. The choice between them depends on your specific use case and performance requirements.
Related benchmarks:
forEach vs findIndex
Comparing findIndex with map & indexOf
Object arrays: findIndex vs for loop 22
Object arrays: findIndex vs for loop (length cached)
Loops and things
Comments
Confirm delete:
Do you really want to delete benchmark?