Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS lastIndexOf vs indexOf
(version: 0)
JS lastIndexOf vs indexOf
Comparing performance of:
Array.lastIndexOf vs Array.indexOf
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = ''; var i = 0; while (i <= 1E5) str += `${i++}`;
Tests:
Array.lastIndexOf
const item = str.lastIndexOf(1E5);
Array.indexOf
const index = str.indexOf(1E5);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.lastIndexOf
Array.indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:147.0) Gecko/20100101 Firefox/147.0
Browser/OS:
Firefox 147 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.lastIndexOf
45430460.0 Ops/sec
Array.indexOf
11175.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand what's being tested in this JavaScript microbenchmark. **Overview** The benchmark compares the performance of two methods on an array: `lastIndexOf` and `indexOf`. Both methods are used to find the last index of a specific element (in this case, 100,000) within an array that starts with numbers from 0 to 100,000. **Options Compared** The two options being compared are: 1. **`lastIndexOf`**: This method finds the highest index at which a specified value can be found in the array. 2. **`indexOf`**: This method finds the index of the first occurrence of a specified value in the array. **Pros and Cons** * **`lastIndexOf`**: + Pros: Can be faster for large arrays because it stops searching as soon as it finds the target element, which can reduce overhead. + Cons: Returns -1 if the element is not found, which may lead to false positives or unnecessary calculations in some cases. Also, if you want to find the last occurrence of an element that appears multiple times, `lastIndexOf` might be slower due to its implementation. * **`indexOf`**: + Pros: Returns 0 as soon as it finds the target element, which can reduce overhead for finding repeated elements. Also, it's generally more concise and readable than `lastIndexOf`. + Cons: Can be slower for large arrays because it searches the entire array even if it finds the target element early. **Library and Special JS Features** There is no specific library being used in this benchmark. However, note that both methods are part of the standard JavaScript API, which means they are supported by most modern browsers and environments without additional dependencies. No special JavaScript features are being tested or highlighted in this benchmark. **Other Considerations** When choosing between `lastIndexOf` and `indexOf`, consider the specific use case: * Use `lastIndexOf` when you need to find the last occurrence of an element, and it's not a concern if it doesn't exist. * Use `indexOf` when you want to find the first occurrence of an element, or when finding repeated elements is more efficient. **Alternatives** If you're interested in comparing other methods for finding indices in arrays, some alternatives might include: 1. **Binary search**: This method can be faster than linear search but requires a sorted array. 2. **Splice-based search**: Some implementations use `splice()` to remove the element from the start of the array and then check if it's present, which can be slower for small arrays. Keep in mind that this benchmark focuses specifically on comparing `lastIndexOf` and `indexOf`, so exploring alternative methods might not provide meaningful results without further customization.
Related benchmarks:
index vs lastindexof startsWith
index vs lastindexof empty
index vs lastindexof empty with startIndex set to 0
index vs lastindexof (last index)
Comments
Confirm delete:
Do you really want to delete benchmark?