With JavaScript, unless you're building a CPU-intensive library or a function that will be called thousands of times per second, microoptimizations are pointless. I prefer clarity to code length, so I would probably do it like this:
const digitGeneration = Array(10); for (let i = 0; i < 10; ++i) { digitGeneration[i] = i.toString(); }
The way you did it is perfectly fine, though. I suppose if you wanted a general purpose range() like in python, you could create a generator:
function* range(startVal, endVal, step = 1) { const stepSign = Math.sign(step); if (stepSign === 0) { throw new TypeError('step cannot be 0'); } if (arguments.length == 0) { throw new TypeError('no range specified'); } if (arguments.length == 1) { endVal = startVal; startVal = 0; } for (let i = startVal; Math.sign(endVal - i) === stepSign; i += step) { yield i; } }
Since no array is created, for iteration purposes this is as efficient as a for loop, even for massive ranges.
for (const value of range(10000, 1, -1)) { console.log(value); } // Outputs 10000...2 separated by newline without taking 10KB of memory
Or you could create an array, like you're trying to do:
const digitGeneration = [...range(10)].map(e => e.toString()); // This is ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
[...Array(10).keys()].map(String)orObject.keys(Array(10).fill())