I have several places in my code where I need to iterate over a string and perform operations char by char. My node.js application needs to do this dozens of times per request and often the length of the strings can be fairly long.
The only way I've seen to convert a javascript like the one below into coffeescript is to create an array based on the length of the string. The problem with this I have is it's an extra thing to do on the hardware side, takes up extra memory, and just seems unnecessary (my node application processes dgrams - up to thousands a second - so all this extra work adds up).
The JavaScript way:
for(var i = 0; i < str.length; i++) { /* Do stuff with str here */ }
The suggested CoffeeScript way
for i in [0..str.length]
# Do stuff here
Again, I think it's silly to force the creation of an array object when the traditional for loop doesn't have to mess with that step from a hardware perspective.
My only work around I've found is to use while loops like:
i = 0
while i < str.length
# Do stuff
i++
While that works, that's far more verbose than even the straight JavaScript way of just using a simple for loop.
Is there a way to use a for loop in CoffeeScript without having to generate superfluous arrays in order to perform basic iterations?
I have several places in my code where I need to iterate over a string and perform operations char by char. My node.js application needs to do this dozens of times per request and often the length of the strings can be fairly long.
The only way I've seen to convert a javascript like the one below into coffeescript is to create an array based on the length of the string. The problem with this I have is it's an extra thing to do on the hardware side, takes up extra memory, and just seems unnecessary (my node application processes dgrams - up to thousands a second - so all this extra work adds up).
The JavaScript way:
for(var i = 0; i < str.length; i++) { /* Do stuff with str here */ }
The suggested CoffeeScript way
for i in [0..str.length]
# Do stuff here
Again, I think it's silly to force the creation of an array object when the traditional for loop doesn't have to mess with that step from a hardware perspective.
My only work around I've found is to use while loops like:
i = 0
while i < str.length
# Do stuff
i++
While that works, that's far more verbose than even the straight JavaScript way of just using a simple for loop.
Is there a way to use a for loop in CoffeeScript without having to generate superfluous arrays in order to perform basic iterations?
Share Improve this question asked Dec 5, 2012 at 15:10 DanDan 3,4595 gold badges38 silver badges46 bronze badges 10 | Show 5 more comments3 Answers
Reset to default 13It doesn't actually create an array if it doesn't have to. Look at the compiled JS. This CoffeeScript:
str = "hello"
for i in [0..(str.length-1)]
alert(i)
Generates the following JavaScript:
var i, str, _i, _ref;
str = "hello";
for (i = _i = 0, _ref = str.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
alert(i);
}
No array was actually created.
(Subtracting 1 to .length to avoid an undefined)
You may also iterate over the string itself:
for ch, i in str
# Do stuff here.
In JavaScript (And thus, also CoffeeScript), strings
can be accessed like you'd access arrays
:
console.log("Hello world".length); // returns "11"
console.log("Hello world"[6]); // returns "o"
console.log("Hello world".indexOf("w")); // returns "6"
I don't see you initializing arrays in any of those loops.
You should be able to use plain JavaScript in your CoffeeScript files, though, if that solves the issue.
console.log("Hello world"[7]);
– Cerbrus Commented Dec 5, 2012 at 15:13.length
is a native string method and also iterating withstr[i]
is okay, i don't see the problem. – Christoph Commented Dec 5, 2012 at 15:17