Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Created September 13, 2012 22:59
Show Gist options
  • Save juandopazo/3718393 to your computer and use it in GitHub Desktop.
Save juandopazo/3718393 to your computer and use it in GitHub Desktop.
Why I want ES.next yesterday
// some code I'm writing...
class Matrix {
/* ... */
transpose() {
var [n, m] = this.size(),
rows = this.rows,
result = [],
i = 0, j = 0;
for (j = 0; j < m; j++) {
result[j] = [];
for (i = 0; i < n; i++) {
result[j][i] = rows[i][j];
}
}
return new Matrix(...result);
}
svd() {
let A = this.transpose().x(this),
[n, m] = this.size(),
S = Matrix.Zeros(n, m);
A.eigenvalues()
.sort((a, b) => b - a})
.map(Math.sqrt)
.forEach((val, i) => S.rows[i][i] = val);
/* ... */
@rauschma
Copy link

Possibility: replace var with let.

@juandopazo
Copy link
Author

I've been avoiding let because Traceur uses try...catch when you use it inside blocks and that's a performance killer. For example this:

transpose() {
    var [n, m] = this.size(),
        rows = this.rows,
        result = [],
        j = 0;

    for (j = 0; j < m; j++) {
        result[j] = [];
        for (let i = 0; i < n; i++) {
            result[j][i] = rows[i][j];
        }
    }

    return new Matrix(...result);
}

...turns into:

transpose: function() { 
  var destructuring$m$n = this.size(), n = destructuring$m$n[0], m = destructuring$m$n[1], rows = this.rows, result =[], j = 0; 
  for(j = 0; j < m; j ++) { 
    result[j]=[]; 
    { 
      try { 
        throw undefined; 
      } catch($i) { 
        $i = 0; 
        for(; $i < n; $i ++) { 
          try { 
            throw undefined; 
          } catch(i) { 
            i = $i; 
            try { 
              result[j][i]= rows[i][j]; 
            } finally { 
              $i = i; 
            } 
          } 
        } 
      } 
    } 
  } 
  return traceur.runtime.spreadNew(Matrix,[true, result]); 
}

When you use let in the top scope of a function it just turns it into a var so in this case it works ok.

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