0

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?

3
  • 2
    Well, you can remove the split. You are still ultimately looking for 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.
    – VLAZ
    Commented Jun 5, 2019 at 14:50
  • Other than the fact that split is unneeded, you can check this post about performances: stackoverflow.com/questions/5296268/… . Felix kling did a good coverage about what is more performant about strings containing other strings.
    – briosheje
    Commented Jun 5, 2019 at 14:51
  • 1
    It's unclear if partial sentences count as a match. Should this match: his name is? Or do you only want full matches?
    – Mark
    Commented Jun 5, 2019 at 14:54

4 Answers 4

2

You can use Array.prototype.includes to check if an array includes a string. or String.prototype.includes to match a substring.

var a = "my name is jack. your name is sara. her name is joe. his name is mike";
var nameSplit = a.split(". ");
var Result = "his name is mike";
nameSplit.includes(Result) // true

or

var a = "my name is jack. your name is sara. her name is joe. his name is mike";
var Result = "his name is mike";
a.includes(Result) // true
6
  • 1
    You don't need to split at all, since includes work on strings too.
    – briosheje
    Commented Jun 5, 2019 at 14:53
  • 1
    The split is still completely irrelevant to the result. You are just doing a.includes(Result) with extra steps.
    – VLAZ
    Commented Jun 5, 2019 at 14:53
  • More specifically, this works because String.prototype.includes is also a thing.
    – Domino
    Commented Jun 5, 2019 at 14:57
  • 1
    the last way will result in "true" even if result = "his name is mi"
    – Sara Ree
    Commented Jun 5, 2019 at 14:59
  • @SaraRee but with Result = "his name is mi" and a = "my name is jack. your name is sara. her name is joe. his name is mike", you would still get a positive using if (nameSplit[i].includes(Result))
    – VLAZ
    Commented Jun 5, 2019 at 15:03
2

You need to ask yourself how many substring you want to find in a, if just one, then you dont need to split as split itself takes time and space. (If interested , you can further check KMP algorithm which can efficiently match word W in a string S.)

If you have a huge amount of substrings to be matched, you then can use split, which costs more space to trade off time.

1

I think the fastest way should be with

str.indexOf('his name is mike') !== -1

but for 1k words i dont think its relevant

1

Your question says you "want to check if Result value is equal to one of the sentences" but your code doesn't check for equality. It allows partial matches — for example if Result is i it matches the i in my name is jack..

If you want to match the whole sentences, you could split the sentences into a Set and then checking for matches is a constant time operation. But they'll need to be exact matches, including whitespace.

var a = "my name is jack. your name is sara. her name is joe. his name is mike";
var nameSplit = new Set(a.split(/\.\s+/g))

var Result = "his name is mike";
if (nameSplit.has(Result))  {
  console.log("Result is one of the sentences on var a");
}
  
Result = "his name is";
if (nameSplit.has(Result))  {
  console.log("Result is one of the sentences on var a");
} else {
  console.log("not in sentences")
}
  

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.