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');
}
return Array(repetitions + 1).join(string);
}
repeatify4('*', 10000);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Simple for loop | |
Extended for loop | |
Log based | |
Array approach |
Test name | Executions per second |
---|---|
Simple for loop | 6040.2 Ops/sec |
Extended for loop | 11658.2 Ops/sec |
Log based | 127897.6 Ops/sec |
Array approach | 1041.9 Ops/sec |
I'll provide an explanation of the benchmark, options compared, pros and cons, library usage, special JS features, and alternative approaches.
Benchmark Overview
The Repeating String benchmark measures the performance of different approaches to create a string by repeating a given input string a specified number of times. The goal is to determine which approach is the fastest and most efficient.
Options Compared
There are four test cases:
for
loop to repeat the string.for
loop that reduces the number of iterations by using arithmetic operations instead of increments.join()
method to repeat the string.Pros and Cons
join()
method.Library Usage
There is no explicit library usage in any of the test cases. However, some implementations (e.g., Log based
) might use internal libraries or functions provided by JavaScript engines (e.g., Firefox's Math.log2()
).
Special JS Features
The repeatify3
implementation uses a special feature called " memoization" through the Map
data structure to cache intermediate results. This is an advanced optimization technique that can significantly improve performance.
Alternative Approaches
Other approaches to repeating a string could include:
String.prototype.repeat()
(introduced in ECMAScript 2015):var result = '*'.repeat(10000);
Buffer
or TypedArray
for optimized repeated operations:const buffer = Buffer.alloc(10000 * string.length, '*');
These alternatives might offer different trade-offs in terms of performance, readability, and complexity.