Which would take less execution time; a switch-case or an equation?
I want to make a page dim more the more cascading windows are appearing one on top of the other like pop-ups.
So if I have 1 child window; the dimming percentage could be 20%, 2 child windows; 40%; 4 child windows; 50% .. and so and so, so it gets less darker as the window cascades and eventually stops at a certain value, for example 55%, so it is never too dark.
The equation it should follow is: f(x) = 5x^4/8 - 65x^3/12 + 95x^2/8 + 155x/12
This should give values as follow:
f(x) | 0 | 20 | 40 | 50 | 55 |
x | 0 | 1 | 2 | 3 | 4 |
Domain of x = [0,4]
Whereas; f(x) = needed mount of dimming and x is the number of windows cascading
Is that a better implementation in terms of execution time; or would a switch-case be better for comparing the current number of windows opened against the needed dimming percentage?
I don't know how many instructions are used per the switch case versus the equation mentioned above?
If performance is really an issue, don't use switch
or an equation. Use an array.
function f(x) {
const dim_pct = [0, 20, 40, 50, 55];
return dim_pct[x];
}