<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>JS TEST</TITLE>
</HEAD>
<BODY>
<script type="text/javascript">
var start = (new Date()).getTime();
var flag = false;
for (i=0; i<10000000; i++) {
// if (flag) {
// var v = "something";
// } else {
// var v = "else";
// }
var v = (flag) ? "something" : "else";
}
var elapsed = (new Date()).getTime() - start;
alert (elapsed);
</script>
</BODY>
</HTML>
It would depend on exactly how you are using it. There would be situations where either would be faster than the other but the time difference is so small you’d need a huge number of calls to even be able to record any difference and a page that takes days to download on broadand before your visitors would actually notice any difference in the time those statements take.
If you are looking to optimise code then you should look at those parts where a small change has a significant effect.
Except for when you have significant issues with run times readability/maintainability should always be the reason for deciding on which approach to take. So start with nice readable easy to maintain code and only change it if you really need to for performance.
The ternary operator can make code easier to read where the purpose is to select one of two or more values to assign to a variable just as a switch/case makes it easier to read where you are testing a single field for a number of different values.