We're doing an integration project where we receive a lot of code values from a webservice.These codes need to be mapped to a user-friendly, translateable text in a visualforce page. We've set up a custom label for each possible code.
The straight forward solution to the mapping is probably a long if else structure, but is that the most performance efficient ?
public static string getTypeLabel(String code) {
if (code == '1') {
return Label.TypeONE;
} else if (code == '2') {
return Label.TypeTWO;
} else if (code == '...') {
return Label.TypeStuffInBetween;
} else if (code == 'n') {
return Label.TypeN;
}
}
I considered that the above approach takes more runtime execution time, specially if there are a lot of possible if statements. I thought that shifting the mapping mechanism to using the internal Map:key-value mechanism could bring improvement.
private static Map < string, string > TYPE_MAP = new Map < string, string > {
'1' => Label.TypeONE,
'2' => Labem.TypeTWO,
'...' => Label.TypeStuffInBetween,
'n' => Label.TypeN
};
public static string getTypeLabel(String code) {
if (TYPE_Map.containsKey(code) {
return TYPE_MAP.get(code);
} else {
return code;
}
}
My initial thought was that this would reduce execution time, but maybe I'm know too little of the internal stuff for this to actually be true. But we have actually implemented it like this, would a class with LARGE (10-100 items) hardcoded map declarations possibly be not more efficient at all,does the use of containsKey() and get() on large maps limit or reverse the performance gain?
I'm not quite sure how to properly evaluate this, so I turn to you :)