Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Repeating String
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:139.0) Gecko/20100101 Firefox/139.0
Browser:
Firefox 139
Operating system:
Mac OS X 10.15
Device Platform:
Desktop
Date tested:
10 months ago
Test name
Executions per second
Simple for loop
47869.8 Ops/sec
Extended for loop
54770.8 Ops/sec
Log based
2675591.8 Ops/sec
Array join
32677.6 Ops/sec
Tests:
Simple for loop
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);
Extended for loop
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);
Log based
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);
Array join
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);