0

I have six different values {string1, string2, .., string6}.
I have to fetch all these values from MongoDB where I compare different conditions. On the basis of these six values I applied my logic.

I need to write an efficient and fast way to compare. Should I go for regex based search or should I compare six separate Boolean conditions Which method is best and why?

Note: I must compare these six values with my data.

Anurag Jain
  • 461
  • 2
  • 8
  • 21
  • Please make clear what you need to do. Do you get an input string and you need to return whether or not it is identical to one of the 6? Something else? – shapiro yaacov Oct 06 '16 at 06:50
  • Your question is a little bit vague but if you want it to be fast you shouldn't use regex – Nick Oct 06 '16 at 06:57
  • if you compare only 6 values, it doesn't really matter; performance differences are imperceptible (maybe not even measurable). If you compare 6 values inside a loop that iterates at least a few hundred/thousand times, and that every few moments/frames/seconds, then this get's a topic. – Thomas Oct 06 '16 at 07:00
  • And besides that, I don't see how regex would fit in this task. You might want to explain your task and why you think you need to optimize performance here. Maybe even adding some of your attempts (code)? – Thomas Oct 06 '16 at 07:09

2 Answers2

1

I wouldn't advise using Regex for this, but in any case you won't see any performance hit unless your querying a heck of allot more items. For simplicity and readability I'd recommend using the switch() statement.

0

Well, if you want to check equality of a variable between six different string values, it is advisable to use switch over if-else. Please go through the following links:

When to use if-else and switch

How switch statements are faster than if-else

Community
  • 1
  • 1
Samu
  • 1,448
  • 10
  • 14