`if` block
satyr opened this issue · 6 comments
if
a then b
c then d
else e
as:
if a
b
else if c
d
else
e
might make us miss less JS's good ol' cond-exp chain:
r = a ? b
: c ? d
: e
This is switch
, isn't it ?
Basically yes. Only that the subjectless switch
is verbose and inefficient:
$ coco -bcs
r = switch
case a then b
case c then d
default e
var r;
r = (function(){
switch (false) {
case !a:
return b;
case !c:
return d;
default:
return e;
}
}());
vs
$ bin/coco -bcs
r = if
a then b
c then d
e
var r;
r = a
? b
: c ? d : e;
AFAIK, ternary conditions are also slow because they clone the left side, and it can be costly (ie for long strings).
Plus it's really hard to read (harder than the switch imho)
While I admit here, in such an expression (assignment) it's far worst to use a switch + IIFE
ternary conditions are also slow because they clone the left side, and it can be costly (ie for long strings).
Compared to what? Got benchmarks?
Hacked up a quick jsPerf. Looks like an if-chain is the fastest, followed closely by a ternary chain. Switch is two orders of magnitude slower.