Gwt - Which is more efficient navigation using HashMap or If statement

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I have a GWT web application that uses the URL to navigate and the MVP pattern. For each URL I have a presenter to be called. I have about 20 differents presenters, and to decide which one to call, I use conditions like

if (view.equals("vue1")){
   presenter = new presenter1();
}else if(view.equals("vue2")){
   presenter = new presenter2();
}else if(view.equals("vue3") || vue.equals("vue4") || vue.equals("vue5")){
   presenter = new presenter345();
}
...

I tried another alternative, which is to load a HashMap at the starting of the application. The map contains the view as a key and the Presenter as the value. But it get complicated because I often have multiple keys (views) pointing to the same Presenter. So for the last condition in my example above, I will do something like this :

map.put("vue3",new Presenter345);
map.put("vue4",new Presenter345);
map.put("vue5",new Presenter345);

Questions :

1- Which one is efficient, knowing that the code is on the client side (JavaScript) ?

2- Is there another alternative ?

Answers

If you only have 20 or so, I wouldn t really worry much about that. I would use a switch statement, and try to include the most likely views first. Or keep the if statements, and again test for the most likely condition first. But I suppose a hashmap will pretty much give you an average O(1), whereas an if statement or switch would give you average O(n)

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/11743967/which-is-more-efficient-navigation-using-hashmap-or-if-statement

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils