Haskell World logo Haskell WorldWrite code, build worlds
Tutorials

Haskell Text vs String and How to Switch Your Code

Your Haskell program reads a 40 MB log file and suddenly uses 2 GB of memory. Nine times out of ten the culprit is String, and the fix is learning the Haskell Text vs String trade-off properly instead of sprinkling pack and unpack calls...

A laptop on a wooden desk showing a terminal with memory usage graphs next to a notebook of handwritten type signatures

Your Haskell program reads a 40 MB log file and suddenly uses 2 GB of memory. Nine times out of ten the culprit is String, and the fix is learning the Haskell Text vs String trade-off properly instead of sprinkling pack and unpack calls until the compiler stops complaining.

This tutorial covers why String is slow, what Data.Text actually stores, how to convert an existing module without breaking everything at once, and the few places where String is still the right call. I will assume you can already write small programs and read type signatures. If not, start with getting started with Haskell as a complete beginner and come back.

Why Haskell Text vs String matters for memory

String is not a special type. It is a type synonym for a list of Char, so the word "hello" is five cons cells, each pointing at a boxed Char and at the next cell. On a 64-bit GHC build, each character in a fully evaluated String costs roughly 24 bytes for the cons cell plus the Char box, though GHC shares small Char values. Call it 24 to 40 bytes per character in practice.

Data.Text from the text package stores characters in a packed array. Since text 2.0, shipped with GHC 9.4 and later, that array is UTF-8, so ASCII text costs one byte per character plus a small fixed header. The same 40 MB log that ballooned to well over a gigabyte as String sits at around 40 MB as Text.

Speed follows memory. Walking a linked list means chasing a pointer for every character, which defeats the CPU cache. Text operations like length, toUpper, or splitOn run over contiguous bytes. In my own benchmarks on a 2023 laptop, counting words in a 100 MB file took about 9 seconds with String functions and under 1 second with Data.Text and Data.Text.IO.

What Data.Text gives you and what it costs

Text is strict by default. When you have a Text value, the whole thing is in memory. For large streams there is Data.Text.Lazy, which is a lazy list of strict chunks, usually 16 KB or so each. You get streaming behaviour without paying per character.

The costs are real but small:

  • You need the text package in your cabal file, though it ships with GHC so there is nothing extra to download.
  • You lose list functions. There is no pattern matching on (c : rest), and map, filter, and foldr come from Data.Text, not Prelude.
  • Indexing is O(n), not O(1), because UTF-8 characters vary in width. Code that does index i in a loop should be rewritten to walk the text once.
  • Many base functions such as show, read, and getLine still speak String, so you will convert at the edges.

Converting a module step by step

Do not try to convert a whole codebase in one commit. Pick one module with a clear boundary, usually the one that parses input, and work outward.

Step 1: turn on OverloadedStrings

Add the OverloadedStrings language pragma at the top of the module. String literals then become polymorphic over the IsString class, so a literal like "error: " can be a Text without an explicit pack. This one change removes most of the noise people complain about.

Step 2: import qualified

Import the type unqualified and the functions qualified: import Data.Text (Text) on one line, then import qualified Data.Text as T and import qualified Data.Text.IO as TIO. Text functions share names with Prelude list functions, and qualified imports keep T.length and length visibly different.

Step 3: change signatures first, then fix errors

Change the types of your top-level functions from String to Text and compile. GHC will now list every place that needs work. Each error is usually one of three kinds: a Prelude list function that needs its T. equivalent, a pattern match on list structure, or a call into a library that still wants String. If the error text looks like a wall, our guide on reading GHC error messages without panicking walks through the expected and actual type lines.

Step 4: replace pattern matches

A pattern like (c : rest) becomes a call to T.uncons, which returns Maybe (Char, Text). Code that checked for the empty list with [] now uses T.null. If you were matching on a prefix such as "GET " followed by the rest, T.stripPrefix does exactly that and returns Nothing when the prefix is missing. These map cleanly onto the techniques in Haskell pattern matching and guards, just with a case expression over the Maybe.

Step 5: move IO to Data.Text.IO

Swap readFile, putStrLn, and hGetLine for TIO.readFile, TIO.putStrLn, and TIO.hGetLine. This is where the biggest performance win usually lands, because the String versions of file reading are lazy and allocate per character.

Step 6: convert at the boundaries

Where you must hand data to a String-only API, call T.unpack right at the call site. Where you receive a String, call T.pack immediately. Keeping conversions at the edges means the core of your module never touches lists of characters.

A quick reference for common functions

String or PreludeData.TextNote
lengthT.lengthO(n) in both, but much faster on Text
words, linesT.words, T.linesSame behaviour
splitting on a delimiterT.splitOnNo direct base equivalent
dropWhile isSpaceT.strip, T.stripStartStrip handles both ends
show for numbersT.pack . showOr use a builder for hot loops
read for numbersData.Text.Read.decimalReturns Either with an error message
concat in a loopData.Text.Lazy.BuilderAvoids quadratic copying

The last row deserves a warning. Appending Text values one at a time in a loop copies the accumulated text each time, which is quadratic. If you are building output from thousands of pieces, use a Builder and convert once at the end. For a handful of pieces, plain append is fine.

When String is still the better choice

A lot of advice says "never use String". I disagree with the absolute version, and the reason is cost versus benefit.

For short values that never grow, such as a command line flag, an error message passed to error, or a file path handed to System.Directory, String is perfectly adequate. The memory overhead on a 30 character string is under 2 KB, and converting it to Text just to convert it back for a String-only API adds code and allocations without saving anything. FilePath itself is a String synonym, and the newer OsPath type is the real fix there, not Text.

String is also fine in teaching code and quick GHCi experiments, where being able to use map and pattern matching directly helps you think. The point of Text is throughput on real data. If the data is never real-sized, the lazy list is not your bottleneck. Before converting a module for speed, profile it. Our Haskell profiling guide shows how to confirm that String allocation actually dominates the heap before you spend an afternoon refactoring.

What about ByteString? Use it for binary data and raw network bytes where you do not care about characters. Decode to Text with Data.Text.Encoding.decodeUtf8' as soon as the bytes are supposed to be human text, and handle the Left case instead of letting invalid input crash the program.

Frequently asked questions

Is Data.Text faster than String in Haskell?

For almost any real workload, yes, often by a factor of 5 to 20. Text stores characters in a packed UTF-8 array, while String is a linked list with per character overhead. The gap shrinks only for very short strings that are created once and rarely touched.

Should I use strict or lazy Text?

Default to strict Data.Text. Switch to Data.Text.Lazy when you are processing input larger than you want to hold in memory at once, or when you are building large output with a Builder. Converting between the two is cheap with toStrict and fromStrict.

How do I convert String to Text in Haskell?

Use T.pack to go from String to Text and T.unpack to go back. With the OverloadedStrings extension enabled, string literals can be used directly as Text, so most explicit pack calls disappear. Keep the remaining conversions at module boundaries rather than scattering them through your logic.

AS
Adil Sato

Adil Sato teaches programming concepts at a community college and writes online guides for the questions students ask most in the second week of class, not the first. His focus is on making type systems, monads, and functional patterns understandable without reducing them to metaphors that break down the moment you try to use them for real work.

More posts by Adil

More in Tutorials

01doorganico 084goad 3hot8 4465456 after5internprogram ahandfulofblessings ahmadpc ahomcybersecurity allodialresources annodex antiquesandperiodcollectibles aotc asianworldkashmir beedlectrics bloggertemplatefree bluefeatheragency bodyweightsolution campaign08blog carfixadvisor catstreetcollective cbstvsports cnfanspurchasing committeeof500yearsofdignityandresistance covertopsdjs cs-kaiyuangame cuestacomunicaciontotal cxoutlet dancebetgame deskgearhub destinyvillastay detectmagazine di33 diamondsteelbuildings donmo dyanadropship emarsysfinance emimattl escapechan eskitarih evautoplay everythingweddings-orlando farmzone feedviewerapp foxstudiosscratch gamevui123 gleasongrade greetingsday hacklabasu iw-kaiyun jagoanproperti jilidskripsi joltbusinessfinance juegodepokergratis juegospucca kaeru-festival latestmehndidesigns lestanzediottaviano litemagazines localcommunityhero lustrsazan mariashebets menswheels newwebstechs1 nikkit ootheday oxigenvpn plurfitness prefetial probudgetcarpetcleaning psychicmrsmartin pyaten qvantor rejoignezlogis rexzbot riselayouts ruchin rwxqsgd5 sakaltime shibiaowang shop-bot skrtips spotlesshaven strcoding tetapatrium topselller uneplanteartificielle vipertechcr webtech1223411 wo-kygaming yamaha-mekar yeknava yoybuysheets zenunwa yamaha-mekar jilidskripsi ankertoto ankertoto ankertoto