Guys I have a couple of questions:
- Is there a performance difference in JavaScript between a
switch
statement and an if...else
?
- If so why?
- Is the behavior of
switch
and 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 converted it to a set of case
statements.
finally I run the function and it runs fine but performance differs in IE and Firefox.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…