I have a variable consisting of multiple sentences separated with dots named var a
. Then I want to check if Result
value is equal to one of the sentences on var a
:
Here is the code:
var a = "my name is jack. your name is sara. her name is joe. his name is mike";
var nameSplit = a.split(".");
console.log(nameSplit.length);
console.log(nameSplit);
var Result = "his name is mike";
var i;
for (i = 0; i < nameSplit.length; i++) {
if (nameSplit[i].includes(Result)) {
console.log("Result is one of the sentences on var a");
}
}
The code works fine .
The problem is I have no idea if this code works fast and efficient if I have 1000 sentences or 10000 sentences on var a
?
Is there any modification that makes the code faster to execute?
Is there any better solution to do this?
a.includes(Result)
- the splitting is irrelevant to the result. Other than that, you still need a linear search, so not much can be improved.his name is
? Or do you only want full matches?