String Handling Showdown: Tale of Unexpected Performance Pitfalls in Haskell

In a quest to shed light on Haskell’s string-handling performance, I venture through a series of code examples that highlight its unexpected struggles against dynamic languages.

Haskell, a compiled programming language known for its elegant and concise code, falls short when it comes to processing strings efficiently. As someone who sought to harness the power of Haskell for fast execution of short dynamic programs, I was surprised by its underwhelming performance in string manipulation tasks. In this post, I will delve into the criticisms of Haskell’s string-handling capabilities and explore alternative techniques that can significantly enhance string processing speed.

Haskell Strings: Performance Challenge

My primary motivation for learning Haskell was to create concise, dynamic-like programs that execute swiftly after compilation. However, Haskell has not lived up to its promise of bare metal speed, especially in scenarios involving extensive string processing. As a developer who predominantly works on text-centric scripts and utilities, I frequently encounter the need to read files, parse their contents, and generate meaningful outputs.

To illustrate the inefficiencies of Haskell’s string processing, let’s build a simple program that highlights the problem:

Program Objective: Read a file named “file” containing prose, capitalize every word within the text, and print the result to the standard output (stdout).

Initially, I approached the task using a straightforward Haskell implementation:

— Normal.hs

module Main where

import Data.Char

convert :: String -> String

convert = unlines . (map convertLine) . lines

convertLine :: String -> String

convertLine = unwords . (map convertWord) . words

convertWord :: String -> String

convertWord s = (toUpper (head s)):(tail s)

main = do

    name <- readFile “file”

    putStr $ convert name

Haskell String

After compiling and executing this code, it took approximately 17 seconds to process the given file, which consisted of around 1.2 million lines of Lorem Ipsum text.

Exploring Alternative Approaches

Dissatisfied with the performance of Haskell’s default string type, I turned to the programming community for recommendations. One common suggestion I encountered was to use Data.Text as a substitute for the native String type in Haskell.

Embracing this advice, I modified the code to utilize Data.Text for improved string handling:

— Main.hs

module Main where

import Data.Char

import qualified Data.Text as T

import qualified Data.Text.IO as X

convert :: T.Text -> T.Text

convert = T.unlines . (map convertLine) . T.lines

convertLine :: T.Text -> T.Text

convertLine = T.unwords . (map convertWord) . T.words

convertWord :: T.Text -> T.Text

convertWord s = T.cons (toUpper (T.head s)) (T.tail s)

main = do

    name <- X.readFile “file”

    X.putStr $ convert name

Surprisingly, this improved version using Data.Text turned out to be significantly slower, clocking in at a whopping 60 seconds. Moreover, it consumed substantial amounts of memory, peaking around 600MB on my machine.

Seeking further optimizations, I decided to leverage lazy IO when reading the file, in hopes of achieving performance enhancements:

import qualified Data.Text.Lazy as T

import qualified Data.Text.Lazy.IO as X

With this modification, the runtime reduced to 27 seconds, demonstrating an improvement over the non-lazy Data.Text implementation.

Next, I explored the possibility of disregarding Unicode support and aiming for optimal bare-metal speed. I opted to replace Data.Text with ByteString:

module Byte where

import Data.Char

import qualified Data.ByteString as B

import qualified Data.ByteString.Char8 as C

convert :: B.ByteString -> B.ByteString

convert = C.unlines . (map convertLine) . C.lines

convertLine :: B.ByteString -> B.ByteString

convertLine = C.unwords . (map convertWord) . C.words

convertWord :: B.ByteString -> B.ByteString

convertWord s = C.cons (toUpper (C.head s)) (C.tail s)

main = do

    name <- B.readFile “file”

    B.putStr $ convert name

Despite these efforts, the performance gains were marginal, with the runtime still at 27 seconds, similar to the lazy Data.Text implementation.

Finally, I decided to explore an alternative approach that leverages Haskell’s scanl function, which allows for a more concise solution:

module Main where

import Data.Char

import qualified Data.Text.Lazy as T

import qualified Data.Text.Lazy.IO as X

convert :: T.Text -> T.Text

convert = T.tail . T.scanl (\a b -> if isSpace a then toUpper b else b) ‘ ‘

main = do

    name <- X.readFile “file”

    X.putStr $ convert name

Remarkably, this concise solution achieved a runtime of 8.5 seconds, representing a notable improvement.

Alternative Language Performance Comparison

To put Haskell’s performance in perspective, let’s explore the processing speed of similar programs implemented in other programming languages.

Python Language


The Python implementation completed the task in approximately 6 seconds.

Java Script

JavaScript (V8 Engine)

The JavaScript implementation, utilizing the V8 engine, completed the task in approximately 4.5 seconds.



The Go implementation achieved a runtime of approximately 2 seconds, showcasing its remarkable performance.

C Sharp


The C implementation showcased exceptional performance, completing the task in a mere 1 second.

Summing Up

Haskell’s string processing performance leaves much to be desired, particularly when compared to dynamic languages. Despite the language’s elegance and conciseness, its default string type falls short in terms of efficiency. However, by utilizing alternative techniques such as lazy IO, scanl, or opting for other programming languages like Python, JavaScript, Go, or C, developers can achieve significantly improved string processing performance.

As technology evolves, it’s crucial to consider the specific requirements of a project and choose the most suitable language or technique to ensure optimal performance. Keep experimenting, exploring, and pushing the boundaries of each language to maximize the potential of your code.For any suggestions, criticisms, or comments, I encourage readers to reach out via email or consider writing their own articles in response. Let’s continue fostering an environment of knowledge sharing and collaboration. Happy coding!