Guys I have a couple of questions:
- Is there a performance difference in Javascript between a switch statement and an if...else if....else?
- If so why?
- Is the behavior of switch and if...else if...else different across browsers? (FireFox, IE, Chrome, Opera, Safari)
The reason for asking this question is it seems that I get better performance on a switch statement with approx 1000s cases in Firefox.
Edited Unfortuantly this is not my code the Javascript is being produced serverside from a compiled library and I have no access to the code. The method that is producing the javascript is called
CreateConditionals(string name, string arrayofvalues, string arrayofActions)
note arrayofvalues is a comma separated list.
what it produces is
function [name] (value) {
if (value == [value from array index x]) {
[action from array index x]
}
}
Note: where [name] = the name passed into the serverside function
Now I changed the output of the function to be inserted into a TextArea wrote some javascript code to parse through the function and convert it to a set of case statments. finally I run the function and it runs fine but performance differs in IE and Firefox.