5

What is the best way to initialize an array with a particular value 0 in coffee script. What I have done so far -

[0..100].map -> 0

And

arr = []
arr.push(0) while arr.length isnt 100

If personally feel that the first one will have a poor performance and the second one is too verbose and destroys the charm of programming in coffee script.

Update 2: Incase performance is not an issue then this is also an option I suppose.

arr = new Array(10).join(0).split('')

Update 2: the above will perform better than others options if the join is passed number

Update 3: After seeing a couple of JSPerf tests mentioned in the comments and answers I tried to perform them my self using node.js. Here are the weird results - Code -

    size = 10000000;
key = 1

console.time('splits')
arr1= Array(size + 1).join(key).split('')
console.timeEnd('splits')


console.time('maps')
arr2 = [1..size].map -> key
console.timeEnd('maps')

console.time('loop')
arr3 = []
arr3.push(key) while arr3.length isnt size
console.timeEnd('loop')


console.time('for')
arr4 = (0 for n in [0...size])
console.timeEnd('for')

console.time('for-no-var')
arr5 = (0 for [0...size])
console.timeEnd('for-no-var')


### node- 0.10.15
splits: 162ms
maps: 1639ms
loop: 607ms
for: 659ms
###

Interestingly the split and join is taking much less time. Also if we care about performance really then we should try to initialize and array which is really big instead of something in the order of hundreds.

7
  • Use the first it's very nice and clear. Worry about performance when it becomes an issue. Commented Aug 30, 2013 at 22:13
  • 4
    there's also arr = (0 for n in [0...100]) Commented Aug 30, 2013 at 23:04
  • Yes that too. I wish there was a builtin feature for this in coffeescript. I love the way we can swap numbers in coffeescript using arrays. Commented Aug 31, 2013 at 0:11
  • jsperf.com/big-array-initialize/2 finds that push out performs ary[x]=0 on Chrome, but not on Firefox. There are many more such tests on jsperf.com Commented Aug 31, 2013 at 16:52
  • 1
    The join/split version creates ['0','0',...]. Initials to characters, not integers. Commented Aug 31, 2013 at 18:02

3 Answers 3

17

There's also the arr = (0 for [1..100]) form if you don't want to have any iteration variable leaking outside the comprehension ;)

Sign up to request clarification or add additional context in comments.

4 Comments

This is what I would go for, but its performance isn't as great as the one @nicolashruchten's answer see jsperf.com/array-initialization-in-coffeescript/4
This answer is better than mine, and it compiles to essentially exactly the same JS minus the variable, so the performance should very very close
@Tushar, notice that in that jsPerf the size of the array is not hard-coded in the expression (it uses (0 for [0...size]) instead of (0 for [0...100000])), and because of that, the Coffee compiler generates a loop that has to check if the range is ascending or descending in each iteration. Nevertheless, Firefox doesn't seem to suffer any performance penalty because of that :D
This seems like the best option interms of both performance and syntax. Thanks :)
5

My vote goes to arr = (0 for x in [0...100])

It's clear, concise, CoffeeScript-ish, and compiles to reasonably clear Javascript:

var arr, x;

arr = (function() {
  var _i, _results;
  _results = [];
  for (x = _i = 0; _i < 100; x = ++_i) {
    _results.push(0);
  }
  return _results;
})();

2 Comments

Can we get rid of the variable x in CoffeeScript?
@epidemian has given an example for it.
1

Here's a comparison of the performance of each of the options mentioned in the question/comments

http://jsperf.com/array-initialization-in-coffeescript

for me on Chrome 28

arr = []
arr.push(0) while arr.length isnt 100

is the fastest

and

[0..100].map -> 0

is the slowest.

That said, the slowest one is on the order of 100k ops/sec. Since initialization should be a relatively uncommon operation, I think its safe to say that performance here is less important than readability.

Personally I find the push version and map versions to be the most readable, but that should really be a decision made by you and whoever else will be working with/reading this code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.