`if` block #151

Closed
satyr opened this Issue Jul 13, 2012 · 6 comments

Comments

Projects
None yet
3 participants
Owner

satyr commented Jul 13, 2012

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
Contributor

vendethiel commented Jul 13, 2012

This is switch, isn't it ?

Owner

satyr commented Jul 13, 2012

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;
Contributor

vendethiel commented Jul 13, 2012

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)

Contributor

vendethiel commented Jul 13, 2012

While I admit here, in such an expression (assignment) it's far worst to use a switch + IIFE

Owner

satyr commented Jul 13, 2012

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.

satyr closed this in f2dc5bb Jan 9, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment