vendredi 5 avril 2019

Is if-else better than try-catch in this particular scenario, performance wise? Which way is the best practice?

I've a json, I need to check a few values and perform a certain task, say this is my json,

s = {
    "d": {
      "f": {
        "g": 1
      }
    }
  }

and the keys d,f and g might or might not be present, and if g == 1, I need to perform a certain task. So, which of the following gives me better performance.

if-else way of doing this

if(s.d && s.d.f && s.d.f.g && s.d.f.g ==1)
{
    doSomeJob();
}
else
{
    doSomeOtherJob();
}

try-catch way of doing this

try{
if(s.d.f.g ==1)
{
    doSomeJob();
}
}catch(e){
    doSomeOtherJob();
}

Aucun commentaire:

Enregistrer un commentaire