最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Iterating over a string in coffeescript without having to create an array - Stack Overflow

programmeradmin2浏览0评论

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
  • Why iterate over a string? What's wrong with the first way? – Christoph Commented Dec 5, 2012 at 15:11
  • 2 @Christoph why not iterate over a string? Maybe he's parsing it. – Pointy Commented Dec 5, 2012 at 15:12
  • 1 How do you mean "without having to generate superfluous arrays"? You can just use a string as if it were a array: console.log("Hello world"[7]); – Cerbrus Commented Dec 5, 2012 at 15:13
  • @Pointy is correct. In my case I'm performing bitwise operations converting buffers to and from strings/numbers. It's a tedious enough process to get bits to/from bytes and CoffeeScript is only adding another layer of complexity. – Dan Commented Dec 5, 2012 at 15:15
  • .length is a native string method and also iterating with str[i] is okay, i don't see the problem. – Christoph Commented Dec 5, 2012 at 15:17
 |  Show 5 more comments

3 Answers 3

Reset to default 13

It 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.

发布评论

评论列表(0)

  1. 暂无评论