Skip to content

Instantly share code, notes, and snippets.

@jpiccari
Last active January 4, 2016 12:39
Show Gist options
  • Save jpiccari/8622410 to your computer and use it in GitHub Desktop.
Save jpiccari/8622410 to your computer and use it in GitHub Desktop.
Simple function to dynamically create an array of a given length where the values are equal to the indices. (407 bytes; 87 bytes minified)
/**
* Creates an array in which each value stored in the array represets the value's index (zero-based).
* @param {number} len - Length of array to produce.
* @returns {array} The resulting array.
*/
function arrayOfNumbers(len) {
/*
* You could replace Number with eval and save 2 additional bytes,
* however that is less secure and probalby not worth the 2 bytes.
*
* Date is used below to save 2 bytes. This works since Date.call.bind
* and Number.call.bind both bind Function.call, so it doesn't matter
* which is used.
*/
return Array.apply(0, Array(len)).map(Date.call.bind(Number));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment