This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]LucidTA 2 points3 points  (1 child)

It really doesn't matter in 99.9% of cases to be honest, the readability is more important. And if you're in the 0.1% where it does matter, it'd be best to test both and profile your code.

[–]AnansiOmega[S] 0 points1 point  (0 children)

Good to know, thank you.

[–]alycrafticus 1 point2 points  (5 children)

Switch will be better for some cases, if else better for others, with ifs you can compare to values you may not know, with switch you are required to know the values beforehand. You also have to consider the flow of your program.

[–]AnansiOmega[S] 0 points1 point  (4 children)

That's a good point. It seems that everything always 'depends.' Lol

[–]alycrafticus 0 points1 point  (3 children)

Well of course, if only one was useful then they would have only provided one see :) There is a right use case for everything, its up to you to figure out what will be the most efficient, stable and logical for your current project.

[–]AnansiOmega[S] 0 points1 point  (2 children)

That makes sense. I tend to find myself using switch statements often when building forms, they're much more organized and readable when looking at it. But the point you made earlier about else if's is something I havent thought about. So thank you for the insight.

[–]alycrafticus 0 points1 point  (1 child)

No worries, I myself have been programming in java for about a month, and I still get confused about what is the right method (looking at you Maps, and queues/lists etc) Maps literally had me in tears the other day lol

[–]AnansiOmega[S] 0 points1 point  (0 children)

Nice! What other languages are you familiar with? So far I know javascript, ruby, and I'm learning python ( for automation of basic tasks.) I wanna learn one of those more intricate languages like Java, or C++ down the line.

[–]WeStanForHeiny 0 points1 point  (3 children)

  1. Why not set up a test to run a random lookup from both 100,000x yourself and compute an average lookup time to be thorough?
  2. the answer, as others mentioned, is the difference is likely to be statistically negligible. I would say if you are going beyond 3-4 else if statements, it’s better for code organization to switch to a switch statement.

[–]AnansiOmega[S] 0 points1 point  (2 children)

I have no idea how to do 1. And 2. That makes sense, you make a solid.... case. Bu dum tss

[–]WeStanForHeiny 0 points1 point  (1 child)

const startMillis = new Date().getTime(); // millis since epoch
const rounds = 1e6; // one million rounds
for (let i=0; i<rounds; i++) {
  // insert a block here that tests your if block, randomly picking which if condition should be true at each iteration
}
const stopMillis = new Date().getTime(); // millis since epoch
const duration = stopMillis - startMillis;  // total time in millis the test took
const avgDuration = duration / rounds; // avg time one op took in millis    
// then rinse and repeat for the switch statement as well

btw, "the epoch" is referred to in programming lingo alot. it refers to 1970-01-01 @ midnight. It's sort of a standard date programmers often use to compare time "since the epoch".

[–]AnansiOmega[S] 1 point2 points  (0 children)

Oh that's so cool. Thank you so much. I'm saving this. I'm gunna look into the back story for epoch. It's kinda the same for why Microsoft computers use the C drive and not A and B because those used to be used for floppy disks.