Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if block #151

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

if block #151

satyr opened this issue Jul 13, 2012 · 6 comments

Comments

@satyr
Copy link
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
@vendethiel
Copy link
Contributor

This is switch, isn't it ?

@satyr
Copy link
Owner Author

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;

@vendethiel
Copy link
Contributor

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)

@vendethiel
Copy link
Contributor

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

@satyr
Copy link
Owner Author

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?

@apaleslimghost
Copy link

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 satyr closed this as completed in f2dc5bb Jan 9, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants