I have a function that loops through large objects of raw data, translating coded strings into readable words. The function is very basic, and I keep wondering if I should use an object instead containing the words to be detected and translated. This would be cleaner-looking and more flexible, but it would not like it to harm the performance of the script, since it needs to run through potentially thousands of items.
function formatTitle(input) {
var input_lowercase = input.toLowerCase()
if (input == "total") {
return "Title"
} else if ( input_lowercase == "type" || input_lowercase == "dokumenttype" || input_lowercase == "km_type" ) {
return label_documenttype
} else if ( input_lowercase.indexOf("tema_") >= 0 || input_lowercase == "spesfikasjon" ) {
return label_theme
} else if ( input_lowercase.indexOf("prosess_") >= 0) {
return label_process
} else if (input.indexOf("SM_") >= 0) {
return input.substr(3);
}
else {
return input.charAt(0).toUpperCase() + input.slice(1)
}
}
There are more exceptions in the list, I've shortened it a bit down for practical reasons. I use the function in the following context (when t_formatheader is switched on):
if ( skipping_mechanism == "lookup" ) { //42% faster than switch block in IE
for (var key in arr_firstnode) {
if (key in keysToIgnore) continue;
if ( t_formatheader == "1" ) {
arrayPushUnknown( columns, formatTitle( key ), "", true, key )
}
else {
arrayPushUnknown( columns, key, "", true, key )
}
}
}
tema_
andprosess_
andSML_
or could it really be anywhere ? – konijn Aug 21 '14 at 17:45