What Actually Happens When You Repeat a String 1,000,000 Times — and Which Operations Become Catastrophically Slow
Repeating "abc" 1,000 times is a one-character operation in Python — but what you do with the result matters enormously. String concatenation in a loop with a large repeated string is O(n²); using join() is O(n); at scale, the difference is seconds vs hours. Here's what repetition actually creates in memory, which operations scale linearly vs quadratically on large strings, how JavaScript's "string ropes" differ from Python's contiguous buffers, and when virtual repetition (storing pattern + count) is better than materializing the full string.
By sadiqbd · June 16, 2026
Repeating a string 1,000 times in Python is a one-character operation ("x" * 1000) — but what you do with the resulting string matters enormously, because a 1 MB string repeated to 1 GB doesn't get "1,000 times harder to process" — some operations become catastrophically slower
The previous articles on this site covered stress testing with repetitive data, string repetition across languages, and using string repetition for terminal UIs. This article addresses the memory and performance implications of large repeated strings — what actually happens in memory when you repeat a string, and why some subsequent operations on that string are O(n) while others become O(n²).
What repetition actually creates in memory
In most programming languages, "abc" * n creates a new, contiguous string of length 3n in memory — every character physically present, n copies of "abc" laid end to end. There's no "lazy" or "virtual" repetition that avoids storing all the characters — the full string exists in memory.
For small repetitions (hundreds, thousands), this is entirely practical. For large repetitions (millions, billions), the memory requirement becomes the constraint before any processing does.
A 1-character string repeated 1 billion times: 1 GB of memory (assuming 1 byte per character ASCII, or up to 4 GB for UTF-32). This can exhaust available memory on most machines.
The O(n) vs O(n²) trap: string concatenation with repeated strings
If you have a large repeated string and need to modify or process it, the choice of operation dramatically affects performance:
O(n) operations (linear, scales well):
- Reading characters one at a time
- Splitting on a delimiter
- Hashing the string
- Writing to a file sequentially
- Passing it to a regex that matches linearly (without backtracking)
O(n²) operations (quadratic, can become catastrophically slow):
- String concatenation in a loop — if you build a string by repeatedly concatenating a large repeated string to another variable (
result = result + repeated_stringin a loop), each concatenation copies the entireresultstring so far, resulting in O(n²) total copies for n iterations - Naive substring search on strings with highly repetitive patterns — certain string search algorithms that depend on pattern uniqueness may degrade to O(n²) on pathologically repetitive inputs
The string concatenation in a loop problem is classic and appears in many languages:
# This is O(n²) — each iteration copies all of result so far
result = ""
for i in range(1000):
result = result + ("abc" * 100)
# This is O(n) — join assembles from parts without repeated copying
parts = ["abc" * 100 for _ in range(1000)]
result = "".join(parts)
The fix — using join() in Python, StringBuilder in Java/C#, String.concat or template literals in JavaScript — is one of the most well-known performance improvements in string handling, precisely because the quadratic trap is easy to fall into and the improvement from O(n²) to O(n) is dramatic at scale.
Language-specific behavior: some languages optimize under the hood
Some languages (especially compiled or JIT-optimized languages) detect and optimize common concatenation patterns at runtime:
Java's JIT may optimize a + concatenation loop into StringBuilder operations in simple cases, though this isn't guaranteed and shouldn't be relied on for large-scale string work.
C++ std::string concatenation in a loop is reliably O(n²) — the optimizer doesn't typically transform loop concatenation into a single reserve+copy operation.
JavaScript (V8 engine) uses "string ropes" — a tree data structure where concatenation is O(1) by creating a node linking the two strings, deferring the actual character copy until needed. Large concatenations in JavaScript may be more efficient than in languages using contiguous string buffers, though the eventual flatten operation is still O(n).
Python explicitly warns in documentation that str.join() is the correct approach for assembling strings from many parts — the "".join(parts) idiom is idiomatic Python specifically because it's O(n) vs the O(n²) concatenation alternative.
When large repeated strings are actually needed: testing and benchmarking
Legitimate uses of large repeated strings:
Buffer/memory fill testing: verifying that a system handles large inputs without crashing, hitting memory limits, or triggering integer overflow in size calculations — a common security test.
Network transfer benchmarking: generating a known-size payload to measure throughput or latency between services.
Regex performance testing (the ReDoS case): creating adversarial inputs to test whether a regex pattern exhibits catastrophic backtracking — the previous "catastrophic backtracking" article covered this; a long string of repeated characters is the classic trigger input.
Database bulk insert testing: generating a large number of test records with repeated string content to verify database performance at scale.
For each of these, the string repeat operation is a generation step — generating the test input. The processing of that input (what you do with the large string after creating it) is where the complexity concerns matter.
How to use the String Repeater on sadiqbd.com
- For generating test data: use the tool to produce a specific volume of content for copy-pasting into a test environment — the output is exactly what you specify, repeated
- For large repetitions: be aware of the resulting string's size (characters × repetitions) before generating — very large strings may be slow to generate in a browser context or difficult to copy/paste
- For code use: the tool generates the output; if you need to repeat strings programmatically, use the language-native approach (
"str" * nin Python,"str".repeat(n)in JavaScript) rather than copy-pasting a pre-generated large string into your source code
Frequently Asked Questions
Is it ever faster to repeat a short string many times than to store the full repeated version?
Yes — "virtual repetition" patterns exist. Instead of storing "abc" * 1000000, you store "abc" and the count 1000000, and generate characters on demand. This is called a "run-length encoding" of the string. For output purposes (writing a file, generating HTTP responses), streaming the pattern character-by-character costs O(1) memory regardless of repetition count. For any operation that requires random access to specific positions in the string (e.g., "what character is at position 2,847,113?"), you compute position % pattern_length to find the right pattern character — O(1). This "virtual" approach is used in file-generation tools, text-editor rendering of large repetitive content, and protocols that compress repetitive data efficiently.
Is the String Repeater free? Yes — completely free, no sign-up required.
Try the String Repeater free at sadiqbd.com — repeat any string any number of times with optional separators.