var str = "According to that, indexOf is just a little bit faster. Of course, it does depend on what you are indexing.IndexOf looks for the first occurrence and lastIndexOf returns to the last occurrence. That all said, their are much better functions around now like map.To be honest though, if Google is using it there is probably a good reason.";
var n = str.lastIndexOf("havent string", 0);
var str = "According to that, indexOf is just a little bit faster. Of course, it does depend on what you are indexing.IndexOf looks for the first occurrence and lastIndexOf returns to the last occurrence. That all said, their are much better functions around now like map.To be honest though, if Google is using it there is probably a good reason.";
var n = str.indexOf("havent string", 0);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lastIndexOf | |
indexOf |
Test name | Executions per second |
---|---|
lastIndexOf | 30921458.0 Ops/sec |
indexOf | 798050816.0 Ops/sec |
Let's break down what this benchmark is testing.
What are we comparing?
We're comparing two different JavaScript methods: indexOf
and lastIndexOf
. Both of these methods search for the first or last occurrence of a specified string within another string, respectively.
indexOf
: Looks for the first occurrence of a specified string.lastIndexOf
: Returns the index of the last occurrence of a specified string.What are we testing?
We're testing how fast it is to use these two methods on a specific string. In this case, the string is: "According to that, indexOf is just a little bit faster. Of course, it does depend on what you are indexing.IndexOf looks for the first occurrence and lastIndexOf returns to the last occurrence. That all said, their are much better functions around now like map.To be honest though, if Google is using it there is probably a good reason."
We're specifically testing how long it takes to call indexOf
or lastIndexOf
on this string with a specific search string ("havent string"
). The test sets the starting index to 0, which means we're searching from the beginning of the string.
What library is being used?
No libraries are mentioned in the benchmark. This is pure JavaScript code.
What special JS feature or syntax is being used?
Not specifically mentioned.
Pros/Cons and other considerations:
indexOf
returns the index of the first occurrence, which can be useful if you need to find the starting point of a search string within another string.lastIndexOf
, on the other hand, returns the index of the last occurrence. This can be useful if you're interested in finding the "latest" or most recent instance of a search string.Based on these results, it seems that calling indexOf
is significantly faster than calling lastIndexOf
for this specific test case.
Other alternatives:
If you need to find all occurrences of a string within another string, you might consider using a more powerful regex (regular expression) or splitting the strings into an array and iterating over them. For example:
let str = 'your string';
let search = 'search string';
// Using regex
let regex = new RegExp(search);
let indexes = [];
for (let match of str.matchAll(regex)) {
indexes.push(match.index);
}
console.log(indexes);
// Splitting into an array and iterating over it
let words = str.split(' ');
indexes = [];
words.forEach((word, index) => {
if (word === search) indexes.push(index);
});
Keep in mind that these alternatives may have their own performance implications, depending on the size of your strings and the complexity of your searches.