There could be several possible answers to this approach.
One thing you could do is to keep the condition free from evaluations. So instead of parsing date in condition expression parse it prior to comparing. After that you could put the most frequent condition, that you feel might be the case often, as the first condition and break the other conditions using binary search approach. Consider following code snippet:
function setTime() {
var currentTime = Date.parse("3/4/2020, 2:53:42 PM")
var selectedTime = Date.parse("3/5/2020, 2:53:42 PM")
if(currentTime < selectedTime) {
callThisMethod('Current time less than selected time');
} else {
if (currentTime > selectedTime) {
callThisMethod('Current time Greater than selected time');
} else {
callThisMethod('Current time is equal to selected time');
}
}
}
function callThisMethod(message) {
console.log(message);
}
setTime();
Notice how conditionals are break into pieces using split technique.
Or you could use switch case instead of conditionals. Switch case has been found to take less incremental cost on subsequent conditions when compared with if-else conditionals.
Consider following snippet:
function setTime() {
var currentTime = Date.parse("3/4/2020, 2:53:42 PM")
var selectedTime = Date.parse("3/5/2020, 2:53:42 PM")
switch(true){
case (currentTime > selectedTime):
callThisMethod('Current time Greater than selected time');
break;
case (currentTime < selectedTime):
callThisMethod('Current time less than selected time')
break;
default:
callThisMethod('Current time is equal to selected time')
}
}
function callThisMethod(message) {
console.log(message);
}
setTime();