-1

I'm new to javascript and just wrote the following, however the tool I'm using is saying it takes too long to execute. I checked some older posts and found similar questions but the conditions are not quite the same. Is there any way to shorten this code so the computer does not think it is taking too much time to execute? Maybe by just shortening the code it will do the trick.

if ((test.something("WhereTo").indexOf("LCUB")!= -1) || (test.something("WhereTo").indexOf("LMDV")!= -1) || (test.something("WhereTo").indexOf("LMUS")!= -1) || (test.something("WhereTo").indexOf("LDOM")!= -1) || (test.something("WhereTo").indexOf("LMEX")!= -1) || (test.something("WhereTo").indexOf("LMDV")!= -1) || (test.something("WhereTo").indexOf("LBRB")!= -1) || (test.something("WhereTo").indexOf("LKEN")!= -1) || (test.something("WhereTo").indexOf("LTHA")!= -1) || (test.something("WhereTo").indexOf("LJAM")!= -1) || (test.something("WhereTo").indexOf("LABW")!= -1))
{
return true;
}
else
{return false;}
JohnS
  • 1
  • 3
    It probably depends on what `test.something("WhereTo")` does. If it's a complicated operation, it is probably worth executing it once, storing the return value in a variable, then performing all the `indexOf` operations on the variable. – lonesomeday Nov 20 '14 at 15:12
  • I've voted as a duplicate, but see this answer in particular for something that might fancy your tickle: http://stackoverflow.com/a/11820643/1470607 – Etheryte Nov 20 '14 at 15:12
  • possible duplicate of [how to check if the url contains multiple strings. Javascript/jquery](http://stackoverflow.com/questions/11820480/how-to-check-if-the-url-contains-multiple-strings-javascript-jquery) – Etheryte Nov 20 '14 at 15:13
  • Put the string args to `indexOf` in an array. Use something like underscore or lodash to see if any of those elements satisfy the condition. Approximately 1-4 lines depending on style, and much more maintainable. Or use a regex since you're just looking for contains. – Dave Newton Nov 20 '14 at 15:13
  • @Nit it is not a duplicate, the condition is in OR, your link are in AND – albanx Nov 20 '14 at 15:14
  • 1
    @albanx ... Not really a significant difference. – Dave Newton Nov 20 '14 at 15:14
  • try storing test.something("WhereTo") in a variable.. then perform indexOf on that variable. test.something("WhereTo") is called too many times.. – Stefan P. Nov 20 '14 at 15:15
  • @DaveNewton for the kind of question that the user made believe me is a significant difference for him. – albanx Nov 21 '14 at 16:55
  • That's a typical programmer's joke – JohnS Nov 24 '14 at 14:23

3 Answers3

2

How about;

var haystack = ["LCUB", "LMDV", "LMUS", .....];
var needle = test.something("WhereTo");

var result = needleInHaystack(needle, haystack);

function needleInHaystack(needle, haystack) {
    for (var i = 0; i < haystack.length; i++)
        if (needle.indexOf(haystack[i]) != -1)
            return true;

    return false;    
}

var haystack = ["LCUB", "LMDV", "LMUS"];

var result = needleInHaystack("xxx LMDV xxx", haystack);
document.write(result);

var result = needleInHaystack("xxx ZZZZ xxx", haystack);
document.write("<br>" + result);


function needleInHaystack(needle, haystack) {
    for (var i = 0; i < haystack.length; i++)
        if (needle.indexOf(haystack[i]) != -1)
            return true;

    return false;    
}
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Thanks for the help.This doesn't return false when non of the ["LCUB", "LMDV", "LMUS", .....] exists? I added else return false in the end it actually broke the code – JohnS Nov 20 '14 at 16:10
  • Updated. You would not use an else, you would return false at the end of the loop. – Alex K. Nov 20 '14 at 16:13
  • My bad. I didn't return result. Thanks that worked perfectly for me. Could you explain how did you come up with the for loop solution & why it works? – JohnS Nov 21 '14 at 11:21
  • You are putting all the strings you search for in an array then looping through each element of that array seeing if its text its present in the test string, if it is the function immediately returns `true` (thus breaking the loop and exiting). If the loop reaches the end of the function it means that `true` has not been previously returned so there must be no match and `false` is returned instead. – Alex K. Nov 21 '14 at 11:47
1

If test.something("WhereTo") returns a String, you can do the following

var yourString = test.something("WhereTo");
if(yourString.search(/LCUB|LMDV|LMUS|.../) != -1) return true;
return false;
miwe
  • 543
  • 3
  • 16
0
var foo = test.something('WhereTo');
var bar = ['LCUB', 'LMDV', 'LMUS'];
return bar.some(function(x){
    return foo.indexOf(x) !== -1;
});
1983
  • 5,882
  • 2
  • 27
  • 39