5 ms·
Don't allocate an array when you just need a for loop.
by edflsafoiewq 6y ago
Don't allocate an array when you just need a for loop.
- suby 6y agoRight, what's wrong with just for (var i = 0; i < 1000; i++) { console.log("Hello world"); }
- cygx 6y agoNothing per se, but more declarative code is often nicer to look at than imperative code. In the for loop example, you need to name and explicitly initialize a variable, you need to explicitly write down the comparison, and you need to explicitly increment the variable. In the Ruby example, the plumbing is hidden from view, and you can just tell the runtime how often you'd like the thing to happen. On a tangential note, in Raku (the language formerly known as Perl6), you could have written that as say("Hello world") xx 10; That's pretty hard to beat, imo.
- antihero 6y agoThe console.log was just an example. This is just getting a range of numbers.