The requirement is to determine the most efficient approach to render a string, for example, "#1a2b3c"
, where "1a2b3c"
are randomly selected from the set
"abcdef0123456789"
or
["a", "b", "c", "d", "e", "f", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
For the uniformity of comparing results, the string .length
should be precisely 7
, as indicated at example above.
The number of iterations to determine resulting time of procedure should be 10000
as used in the code below.
We can commence the inquiry with two prospective examples and benchmarks. Benchmarks for the approaches should be included within the text of the Answer. Note, if more accurate benchmarks can be utilized, or text of Question can be improved, do advise at comment. Related: Can someone fluent in Javascript explain to me whats going on here SIMPLY.
function randColor() {
return '#' + (function co(lor) {
return (lor += [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f'][Math.floor(Math.random() * 16)]) &&
(lor.length == 6) ? lor : co(lor);
})('');
}
console.time("random string recursion");
for (let i = 0; i < 10000; i++) {
randColor()
}
console.timeEnd("random string recursion");
console.time("random string regexp");
for (let i = 0; i < 10000; i++) {
"xxxxxx".replace(/x/g, function() {
return "abcdef0123456789".charAt(Math.floor(Math.random() * 16))
});
}
console.timeEnd("random string regexp");
What is the most efficient, where efficiency is defined as the least amount of resource necessary for "speed" and "storage", to achieve returning a string of having .length
of N
?
Does the efficiency of speed and storage decrease as N
increases?