I have a CoffeeScript code
for y in [coY - limit .. coY + limit]
for x in [coX - limit .. coX + limit]
I was looking for ways how to improve speed of my code and found what it compiles into:
for (y = _i = _ref = coY - limit, _ref1 = coY + limit; _ref <= _ref1 ? _i <= _ref1 : _i >= _ref1; y = _ref <= _ref1 ? ++_i : --_i) {
for (x = _j = _ref2 = coX - limit, _ref3 = coX + limit; _ref2 <= _ref3 ? _j <= _ref3 : _j >= _ref3; x = _ref2 <= _ref3 ? ++_j : --_j) {
When I replaced that with my own JavaScript
for(y = coY - limit; y <= coY + limit; y++) {
for(x = coX - limit; x <= coX + limit; x++) {
I have measured the script to be significantly faster (from 25 to 15 ms). Can I somehow force CoffeeScript to compile into code similar to mine? Or is there other solution?
Thank you.
Assuming your loop will always go from a smaller number to a bigger number, you can use
by 1:Which compiles to:
It’s not HEAPS better, but possibly a bit.