Range
Category: JavaScript
Usage
You usually want to use an eager range if you only have a finite amount of numbers to iterate. Next to that, you only want to use this eager range if your range is not too large and does not contain thousands of numbers.
Explanation
This range function just uses the Array.from factory method to create a new array.
The first parameter is a so-called Array-like object. It only needs a length property, which is used to calculate the resulting length of the array because you don’t want to waste too much space.
The second parameter is the so-called mapFn (map function). It is applied to each element of the Array on its creation. In this case, the map function runs over the array with its length, and uses the second parameter of the callback, the index, to calculate the next number at the desired position.
Performance
This function is fast enough, but the range it creates is still eager. Be careful when creating very large ranges. Those numbers will all take up memory, which can grow pretty large with this approach.
If you have large ranges, you might want to use a lazy range.