Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Performance: Array vs Object vs switch

ANSWERED

Has anybody some experience with the following task?

 

Since .toLocaleString() seems to support "en-??" only, I need to localise the montname and the weekday maually.

 

I have three different approaches and I'm asking myself, which would have the best performance and best space efficiency on the Ionic.

 

Approach 1: Arrays

var weekday = [];
var monthname = [];
weekday["en"]=["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
monthName["en"]=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
weekday["de"] = ["Sonntag", "Montag", "Dienstag", "Mitwoch", "Donnerstag", "Freitag", "Samstag"];
monthName["de"] = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"];

Approach 2: Objects

var weekday = {
	en: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
de: ["Sonntag", "Montag", "Dienstag", "Mitwoch", "Donnerstag", "Freitag", "Samstag"],
};
var monthName = {
de: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
en: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
};

Approach 3: switch

function getWeekDay(prefix, num) {
	var weekday = [];
	switch (prefix) {
		case "de":
			weekday = ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"];
			break;
		case "en":
			weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
			break;
};
function getMonthName(prefix, num) {
var monthname = [];
switch (prefix) {
case "de":
monthname = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"];
break;
case "en":
monthname = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
break;
}

Which apporach would you prefer and why?

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

I believe all 3 approaches would consume about the same memory and perform about the same.  The advantage of any of these 3 approaches would be so minor it's not worth worrying about.

 

Update:

This question was bugging me last night, so I did a little digging and confirmed that there is little to no difference between the 3.  Approach #1 and #2 have the most consistent performance, and for simplicity sake, I'd recommend #2.


 

View best answer in original post

Best Answer
2 REPLIES 2

I believe all 3 approaches would consume about the same memory and perform about the same.  The advantage of any of these 3 approaches would be so minor it's not worth worrying about.

 

Update:

This question was bugging me last night, so I did a little digging and confirmed that there is little to no difference between the 3.  Approach #1 and #2 have the most consistent performance, and for simplicity sake, I'd recommend #2.


 

Best Answer

Thank you! I'll choose approach # 2.

Best Answer
0 Votes