Linux Basic — Array vs String Performance
Array vs String Performance
Arrays are faster for indexed access and avoiding repeated splits. Strings are faster for small one-off operations and use less memory.
When Arrays Win
1. Reading many lines
# SLOW — one fork per line
while IFS= read -r line; do
lines+=("$line")
done < file
# FAST — single builtin
mapfile -t lines < file
mapfile (alias readarray) is a Bash 4+ builtin — one operation to slurp a file into an array.
2. Indexed lookups
# SLOW — linear search in string
list="a b c d e"
if [[ "$list" =~ (^| )c($| ) ]]; then
# FAST — O(1) indexed access
list=("a" "b" "c" "d" "e")
echo "${list[2]}" # c
3. Avoiding repeated splits
# SLOW — splits on every access
files="a.txt b.txt c.txt"
for f in $files; do # splits again each loop
process "$f"
done
# FAST — split once
files=(a.txt b.txt c.txt)
for f in "${files[@]}"; do
process "$f"
done
When Strings Win
Small datasets
For < 100 items, the memory overhead of an array isn’t worth it.
One-time append
# strings are fine for building once
output=""
output+="line 1\n"
output+="line 2\n"
echo -e "$output"
Serialisation
If you’re passing data to an external command via stdin, a string is simpler:
data="line1\nline2\nline3"
echo -e "$data" | process
Memory Considerations
Arrays have per-element overhead. For very large datasets (10k+ items), consider:
- Streaming with
while read(constant memory) - External tools (
awk,sort) that handle large files - Writing to a temp file instead of holding in memory
Benchmark
# string append (10k lines): ~2 seconds
result=""
for i in {1..10000}; do
result+="$i\n"
done
# array (10k lines): ~0.5 seconds
result=()
for i in {1..10000}; do
result+=("$i")
done
Arrays are faster because each += on a string copies the entire string. Arrays just append a reference.
Quick Reference
| Task | Use |
|---|---|
| Read file into list | mapfile -t arr < file |
| Indexed access | array |
| Random access by position | array |
| One-time build + output | string |
| > 10k items | stream or external tool |
Next → Profiling with time