vendredi 31 août 2018

Have multiple condition and rules, which is better switch or if-else in javascript [duplicate]

This question already has an answer here:

I need to know which is better switch or if-else, but in my case, it will be sensitive because I will add condition rules for APIs, so I need to know the best way to switch between APIs condition without damage memory,

Note: the page will load with the condition, I will not switch between them in SPA, no It will load by condition but I need to know which better to make sure other conditions contain APIs off If user don't choose it

there are many roles and this role knows as props, so user will update it manually and refresh the page, I need to make sure other condition will be off, and which is better as a performance

switch (true) {
    case (role == 'rolenumberOne'):
      this.$http
        .get(`https://api.github.com/repos/moumen-soliman/frontend-helper/stats/contributors`)
        .then(response => (this.getData = response.data))
        .catch(error => console.log(error.response))
      break;

    case (role == 'rolenumberTwo'):
      this.$http
        .get(`https://api.github.com/repos/moumen-soliman/frontend-helper/stats/contributors`)
        .then(response => (this.getData = response.data))
        .catch(error => console.log(error.response))
      break;

    case (role == 'rolenumberThree'):
      this.$http
        .get(`https://api.github.com/repos/moumen-soliman/frontend-helper/stats/contributors`)
        .then(response => (this.getData = response.data))
        .catch(error => console.log(error.response))
      break;

    case (role == 'rolenumberFour'):
      this.$http
        .get(`https://api.github.com/repos/moumen-soliman/frontend-helper/stats/contributors`)
        .then(response => (this.getData = response.data))
        .catch(error => console.log(error.response))
      break;

    default:
      break;
}

Aucun commentaire:

Enregistrer un commentaire