Skip to content

Instantly share code, notes, and snippets.

@xgrommx
Last active June 13, 2020 23:04
Show Gist options
  • Save xgrommx/a25ffa3a7753e01ee679 to your computer and use it in GitHub Desktop.
Save xgrommx/a25ffa3a7753e01ee679 to your computer and use it in GitHub Desktop.
Generate ranges in javascript
const range1 = len => Object.keys(new Int8Array(len)).map(parseFloat);
const range2 = len => Array.apply(null, {length: len}).map(Number.call, Number);
const range3 = (start, end) => Array.apply(null, Array(end - start)).map((_, i) => start + i);
const range4 = (start, end) => [...Array(end - start)].map((_, i) => start + i);
const range5 = len => Object.keys(Array.apply(null, Array(len)));
const range6 = (start, end) => Array.from(Array(end - start), (_, i) => start + i);
@ghaiklor
Copy link

ghaiklor commented Feb 9, 2016

const range = (start, end) => Array.from({length: end - start}).map((_, i) => start + i);

@shvaikalesh
Copy link

const range = (start, end) => Array.from({length: end - start}, (_, i) => start + i);

@yairhaimo
Copy link

const range8 = n => Array(n).fill().map((v, i) => i)
const range9 = (start, end) => Array(end - start).fill().map((v, i) => i + start)

@benlesh
Copy link

benlesh commented Feb 2, 2017

@shvaikalesh's is most efficient, outside of possibly using a for loop.

@kuncevic
Copy link

kuncevic commented Feb 8, 2017

@xgrommx looks like range4 and range6 are identical

@yairhaimo getting errors on range8 and range9 [ts] Supplied parameters do not match any signature of call target.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment