Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Repeating String
(version: 0)
Comparing performance of:
Simple for loop vs Extended for loop vs Log based vs Array approach
Created:
9 years ago
by:
Guest
Jump to the latest result
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 approach
function repeatify4(string, repetitions) { if (repetitions < 0 || repetitions === Infinity) { throw new RangeError('Invalid repetitions number'); } return Array(repetitions + 1).join(string); } repeatify4('*', 10000);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Simple for loop
Extended for loop
Log based
Array approach
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **Simple for loop**: This implementation uses a traditional `for` loop to repeat the string. 2. **Extended for loop**: This implementation uses an optimized `for` loop that reduces the number of iterations by using arithmetic operations instead of increments. 3. **Log based**: This implementation uses bitwise operations and caching to reduce the number of iterations required. 4. **Array approach**: This implementation uses JavaScript's built-in `join()` method to repeat the string. **Pros and Cons** * **Simple for loop**: + Pros: Easy to understand, straightforward implementation. + Cons: Can be slow due to unnecessary increments in the loop variable. * **Extended for loop**: + Pros: Optimized for performance, reduces iterations using arithmetic operations. + Cons: May require additional computation and caching. * **Log based**: + Pros: Highly optimized for performance, uses bitwise operations and caching. + Cons: More complex implementation, requires understanding of binary logarithms and caching. * **Array approach**: + Pros: Simple and easy to understand, leverages built-in `join()` method. + Cons: May not be the most efficient due to unnecessary array creation. **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: * Using `String.prototype.repeat()` (introduced in ECMAScript 2015): ```javascript var result = '*'.repeat(10000); ``` * Utilizing `Buffer` or `TypedArray` for optimized repeated operations: ```javascript const buffer = Buffer.alloc(10000 * string.length, '*'); ``` These alternatives might offer different trade-offs in terms of performance, readability, and complexity.
Related benchmarks:
Repeating String
Repeating String
repeat
Trimming multiple leading/trailing characters
Comments
Confirm delete:
Do you really want to delete benchmark?