Apple Lossless Decoder in Coffeescript
github.com
github.com
Otherwise it is pretty similar to javascript, most coffeescript constructs map pretty simply down to javascript, so optimization tips for one language mostly applies to the other.
for i in [0...10]
doSomething()
is slower than for i in [0...10] by 1
doSomething()
because of the way it is compiled, so we always include the `by 1` as an optimization.Another one is that if you have a function and the last thing in that function is a loop of some kind, CoffeeScript will try to return an array from that function collecting the results of the loop. So we explicitly put a return statement at the end of the function to avoid this and the performance penalties.
There are probably a few more to be careful of, but overall performance characteristics are similar to JS. I'm sure Jens has some thoughts too, but overall I think it was a positive experience.
var i;
for (i = 0; i < 10; i++) {
doSomething();
}
for (i = 0; i < 10; i += 1) {
doSomething();
}
http://jashkenas.github.com/coffee-script/#try:for%20i%20in%... for i in [0...test]
doSomething()
for i in [0...test] by 1
doSomething()
compiles to: var i;
for (i = 0; 0 <= test ? i < test : i > test; 0 <= test ? i++ : i--) {
doSomething();
}
for (i = 0; i < test; i += 1) {
doSomething();
}
the latter one being much faster because it isn't testing the direction the counter should be going all the time. for i in [a...b]
becomes if(a <= b) {
for(i = a; i < b; i++) {
// Code
}
} else {
for(i = a; i > b; i--) {
// Code again
}
}
Or otherwise moved the extra conditions outside the loop so they only get computed once? step = (a<=b)?1:-1;
for (i = a; step*i < step*b; i += step) {
doSomething();
}
I guess if the branch predictor is any good it could be slower with multiplies, but I have no idea what to expect in a dynamic language.Not knowing CoffeeScript well, when I see this code:
n = -10 for i in [0...n] doSomething()
I'd expect it to call doSomething zero times. At least that's what I'd expect in JavaScript or any of the other languages I've used.
for i in [10..0]
I've opened a ticket to discuss this further: https://github.com/jashkenas/coffee-script/issues/1952Is this by design or a bug in the code (or a bug in Chrome (16-stable) that I used to check out the demo site)?
But if someone wants to do it, I think both me and @devongovett would be ready to help out a bit.
(We usually hang out in #ofmlabs on freenode, if someone wants to speak with us)