function repeatify(string, repetitions) {
if (repetitions < 0 || repetitions === Infinity) {
throw new RangeError('Invalid repetitions number');
}
let result = '';
for (let i = 0; i < repetitions; i++) {
result += string;
}
return result;
}
repeatify('*', 10000);
function repeatify2(string, repetitions) {
if (repetitions < 0 || repetitions === Infinity) {
throw new RangeError('Invalid repetitions number');
}
const isEven = repetitions % 2 === 0;
const iterations = Math.floor(repetitions / 2);
const stringTwice = string + string;
let result = '';
for (let i = 0; i < iterations; i++) {
result += stringTwice;
}
if (!isEven) {
result += string;
}
return result;
}
repeatify2('*', 10000);
function repeatify3(string, repetitions) {
if (repetitions < 0 || repetitions === Infinity) {
throw new RangeError('Invalid repetitions number');
}
const cache = new Map();
function repeat(string, repetitions) {
if (repetitions === 0) {
return '';
}
const log = Math.floor(Math.log2(repetitions));
let result;
if (cache.has(log)) {
result = cache.get(log);
} else {
result = string;
for (let i = 1; i <= log; i++) {
result += result;
cache.set(i, result);
}
}
const repetitionsProcessed = Math.pow(2, log);
const repetitionsLeft = repetitions - repetitionsProcessed;
return result + repeat(string, repetitionsLeft);
}
return repeat(string, repetitions);
}
repeatify3('*', 10000);
function repeatify4(string, repetitions) {
if (repetitions < 0 || repetitions === Infinity) {
throw new RangeError('Invalid repetitions number');
}
var resultArray = new Array(repetitions);
for (let i = 0; i < repetitions; i++) {
resultArray[i] = string;
}
return resultArray.join('');
}
repeatify4('*', 10000);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Simple for loop | |
Extended for loop | |
Log based | |
Array join |
Test name | Executions per second |
---|---|
Simple for loop | 4937.9 Ops/sec |
Extended for loop | 8985.0 Ops/sec |
Log based | 141699.6 Ops/sec |
Array join | 2974.7 Ops/sec |
Measuring the performance of different string repetition approaches is crucial in understanding how JavaScript engines handle such tasks.
Benchmark Test Cases
The test cases provided measure the execution speed of four different string repetition functions:
repeatify(string, repetitions)
uses a traditional for loop to repeat the string.repeatify2(string, repetitions)
uses an extended for loop with an additional check to append the original string when the number of repetitions is odd.repeatify3(string, repetitions)
uses a logarithmic approach to cache and reuse previously computed results, reducing the number of iterations required.repeatify4(string, repetitions)
creates an array with repeated strings using the join()
method.Options Compared
The test cases compare the performance of these four approaches under different conditions:
Pros and Cons of Each Approach
join()
method.Other Considerations
When choosing a string repetition approach, consider the following factors:
Alternatives
Other alternatives for string repetition could include:
repeat-string
or string-repeat
, which provide optimized implementations for different use cases.String.prototype.repeat()
(available in modern browsers).Keep in mind that the choice of approach depends on specific requirements and performance constraints.