I need to map accented letters (é, ü, á, ï, ö, ..., etc.) to their corresponding letters in English (e, u, a, i, o, ..., etc.).
I know I can do this in several approaches:
- Looping through an array - complexity O(n):
const mappingArray = [
['é', 'e'],
['ü', 'u'],
['á', 'a'],
['ï', 'i'],
['ö', 'o']
];
accentedToEnglish(accented) {
for (let i = 0; i < mappingArray.length; i++ ) {
if (mappingArray[i][0] === accented)
return mappingArray[i][1];
}
}
- Accessing object keys - complexity O(1):
const mappingObject = {
'é': 'e',
'ü': 'u',
'á': 'a',
'ï': 'i',
'ö': 'o'
};
accentedToEnglish(accented) {
return mappingObject[accented];
}
Why is the complexity of the mapping above using object keys = O(1) in javascript? Does't the javascript engine iterate through all the keys behind the scenes in order to find the exact value?