Skip to content

Instantly share code, notes, and snippets.

@itslenny
Forked from mindoftea/fastArray.js
Created May 15, 2014 01:42
Show Gist options
  • Save itslenny/25f9c5213df65f5b8aa9 to your computer and use it in GitHub Desktop.
Save itslenny/25f9c5213df65f5b8aa9 to your computer and use it in GitHub Desktop.
var generateArray=function(x,n)
{
var y,z;
y=" "+x; //added space here
z="";
while(n>0)
{
if(n%2)
{
z+=y;
n--;
}
if(n===0)
{
break;
}
y+=y;
n/=2;
}
//trimemd space here and split on it
return z.trim().split(" ");
};
var generateArrayShorter=function(val,len)
{
var out = new Array(len);
for(var idx = 0; idx < out.length; idx++){
out[idx]=val;
}
return out;
};
var generateArrayShortest=function(val,len)
{
return new Array(len+1).join(val+' ').trim().split(' ');
};
var myArray1 = generateArray("x",1594323);
// Takes 124ms
var myArray2 = generateArrayShorter("x",1594323);
// Takes 162ms
var myArray3 = generateArrayShortest("x",1594323);
// Takes 229ms
console.log(myArray1);
//takes about 14 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment