Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
index vs lastindexof for Array
(version: 0)
Comparing performance of:
lastIndexOf vs indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
lastIndexOf
var str = "Hello world, welcome to the universe.".split(' ');; var n = str.lastIndexOf("welcome");
indexOf
var str = "Hello world, welcome to the universe.".split(' '); var n = str.indexOf("welcome");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lastIndexOf
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lastIndexOf
24849016.0 Ops/sec
indexOf
20801142.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is designed to compare the performance of two methods: `lastIndexOf` and `indexOf` on arrays in JavaScript. **What are being compared?** * `lastIndexOf`: This method returns the highest index of a specified value within an array. If the value is not found, -1 is returned. * `indexOf`: This method returns the index of the first occurrence of a specified value within an array. If the value is not found, -1 is returned. **Options being compared** The benchmark compares two options: A) Using `lastIndexOf` on a string that has already been split into an array of words (split by spaces). B) Using `indexOf` on a string that has already been split into an array of words (split by spaces). **Pros and Cons:** * **Using `lastIndexOf`:** + Pros: - Can be faster for large arrays because it doesn't have to search from the beginning of the array. - Can handle edge cases where the value is at the end of the array more efficiently. + Cons: - May be slower than `indexOf` when searching for a specific word within a short array. - Returns the highest index, which might not always be what you want (e.g., if there are multiple occurrences). * **Using `indexOf`:** + Pros: - Faster and more efficient for small arrays or when searching for a specific word. - Returns the exact index of the first occurrence, making it easier to work with. + Cons: - May be slower than `lastIndexOf` for large arrays because it has to search from the beginning of the array. **Library used:** None explicitly mentioned. However, it's likely that a default JavaScript implementation is being used. **Special JS feature or syntax:** The benchmark uses the `split()` method, which splits a string into an array of words (split by spaces). This is a built-in JavaScript method that doesn't require any external libraries or dependencies. **Other alternatives:** If you wanted to test these methods with different scenarios or options, you might consider adding more benchmarks, such as: * Using `lastIndexOf` and `indexOf` on arrays with duplicate values. * Testing performance with arrays of different sizes (small, medium, large). * Adding a timeout or timeout option to see how the browsers handle slow or failing tests. Keep in mind that the best approach will depend on your specific use case and requirements.
Related benchmarks:
index vs lastindexof startsWith
index vs lastindexof empty
index vs lastindexof empty with startIndex set to 0
index vs lastindexofasdf
index vs lastindexof (for right biased values)
Comments
Confirm delete:
Do you really want to delete benchmark?