-
-
Save itslenny/25f9c5213df65f5b8aa9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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