1 line of code: How to create a range array
Tags: javascript, webdev, performance, codequality
Published: 2021-12-02 17:13:24


```javascript
const createRange = (from, to) => Array.from({ length: to - from + 1 }, (_, i) => from + i);
```

Returns a new array with numeric items in the given range.

---

## Optimised code ([Benchmark](https://perf.link/#eyJpZCI6IjJqeGJpMm45cmpwIiwidGl0bGUiOiJjcmVhdGUgcmFuZ2UgYXJyYXkiLCJiZWZvcmUiOiIiLCJ0ZXN0cyI6W3sibmFtZSI6IlRlc3QgQ2FzZSIsImNvZGUiOiJjb25zdCBjcmVhdGVSYW5nZSA9IChmcm9tLCB0bykgPT4gQXJyYXkuZnJvbSh7IGxlbmd0aDogdG8gLSBmcm9tICsgMSB9LCAoXywgaSkgPT4gZnJvbSArIGkpO1xuY3JlYXRlUmFuZ2UoMiwyMDApOyIsInJ1bnMiOlsyNTAsMTI1LDMwMTI1LDE4NzUsMTI1LDc2MjUsMzg3NSwzNTAwLDQ1MDAsMTIxMjUsMzI2MjUsNDc1MCwyNjI1LDM3NSwxNjI1LDI2MjUsMTI1LDE2MjUsMzc1MCwyMzc1LDEwMDAsMTAwMCwyMTc1MCwxNjEyNSwzODAwMCwxNzUwLDE5ODc1LDM4NzUsNDk1MDAsNTM3NSw1MTI1LDE2MjUsNzc1MCwzODc1LDE0NjI1LDE2MjUwLDEwMDAsMTAwMCwyNTI1MCw2MjUsMTIyNTAsNjYyNSwxMTI1LDcyNTAsMjEyNSwzMzc1LDExMjUsMjEwMDAsNDM3NSwxNTM3NSwyMDYyNSw0NTAwLDY2MjUsMTM3NSwzNjI1LDE2MjUsMzc1LDI0MjUwLDE0ODc1LDI5Mzc1LDE2MjUsMTI1MCwxMjUsMjYyNSwyNjI1LDIxMjUsMTc1MCwxNDAwMCw3NTAsNDM3NSw0MTI1LDQ4NzUsMjAzNzUsNTg3NSwyMTI1LDE2MjUwLDQyNTAsMTAwMCw5MjUwLDIwMzc1LDIxMjUsMzE2MjUsMzM3NSwyMjUwLDIzNzUsMTg2MjUsMjM3NSw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c2lnbihiIC0gYSkpID0%2BICBBcnJheShNYXRoLmFicyhiIC0gYSArIHNpZ24pKS5maWxsKCkubWFwKChfLCBpKSA9PiBhICsgc2lnbiAqIGkpO1xuICBjcmVhdGVSYW5nZTMoMiwyMDApO1xuIiwicnVucyI6Wzg3NSwxNjI1LDIyNTAsMjAwMCwyMzc1LDUzNzUsMjg3NSwxMjUsOTEyNSwzMTAwMCwyMTI1LDEwMDAsNzg3NSwxMzc1LDUwMCwyMjYyNSwzMTYyNSwzMTYyNSw1Mzc1LDE4NzUsNTYyNSwxMTI1LDE4NzUsMTYyNSw5MjUwLDEzODc1LDEzNzUsMTQxMjUsNDI1MCw2MjUsMTc1MCwyNjI1LDEzNzUsNzM3NSwxMzc1MCw5NjI1LDIxMjUsMjYyNSw4ODc1LDMyNTAsMjAwMCw0MTI1LDE4MTI1LDgwMDAsMTI1MCwzMDAwLDE5MTI1LDIyMDAwLDMwMDAwLDE3NTAsMTU4NzUsNzAwMCw5MjUwLDM1MDAsMzEyNSw1NjI1LDkxMjUsMjUwMCw4NzUsMjI1MCwyMTc1MCwzNzUwLDI4NzUsMjg3NTAsMTI1LDE3NTAwLDE2NzUwLDMxNjI1LDEwMjUwLDIxODc1LDc1MCwyNjEyNSwxNTAwLDI3NTAsMjE1MDAsMTUwMCwxNjI1LDQ1MDAsMTIwMDAsMTg3NSw3NjI1LDIyMjUwLDE0NTAwLDMxNjI1LDExODc1LDI4NzUsMTAwMCwyNzg3NSwzMzc1LDQyNTAsMjc1MCw2MjUsMzE2MjUsMjI1MCwyMzc1LDE2MjUwLDE3NTAsMzAwMCwxODc1LDc3NTBdLCJvcHMiOjg2NjF9XSwidXBkYXRlZCI6IjIwMjEtMTItMDNUMTk6MzM6NTUuMTQ0WiJ9))

```javascript
const createRange = (a, b, sign = Math.sign(b - a)) =>  Array(Math.abs(b - a + sign)).fill().map((_, i) => a + sign * i);
```
---

## The repository & npm package

You can find the all the utility functions from this series at [github.com/martinkr/onelinecode](https://github.com/martinkr/onelinecode)
The library is also published to [npm as @onelinecode](https://www.npmjs.com/package/@onelinecode/onelinecode) for your convenience.

The code and the npm package will be updated every time I publish a new article.

---

Follow me on [Twitter: @martinkr](http://twitter.com/_martinkr) and consider to [buy me a coffee](https://www.buymeacoffee.com/martinkr)

Photo by [zoo_monkey](https://unsplash.com/@zoo_monkey) on [Unsplash](https://unsplash.com/s/photos/fuji)

---