Run-Length Encoding Calculator

Run-length and PackBits, with the size ratio shown.

  • Text and PackBits
  • Round trip checked
  • Worst case shown
A terracotta clay stool with a row of pastel clay beads on its top, and a second row of beads on the shelf below with one long rounded bar among them.

The guide

Counting the repeats: what run-length encoding saves, and when it costs you

A run is stored once with its length. That shrinks a line of 67 letters to 18 and doubles a line with no repeats, and PackBits shows how to bound the damage.

  • 18 of 67 characters in the code for the 67-character line
  • 0.269 packed size over unpacked size of that line
  • 202 bytes PackBits size of 200 bytes with no repeats
  • FE header byte for a run of 3 equal bytes
Read the full guide Fold the guide 8 min · 13 sources

Picture a scan line of a page of black text on white paper. Most of it is white, in long stretches, with short black interruptions where a letter crosses the line. Writing it pixel by pixel records the same fact hundreds of times: still white, still white, still white. Run-length encoding is the oldest answer to that waste. It stores a stretch of identical neighbours once, together with how many there were. The idea is so small that it is easy to trust too far, and the interesting part is where it stops working.

A run is a value and a count

Take the line used in the standard description of the method: twelve W, one B, twelve W, three B, twenty-four W, one B and fourteen W, which is 67 characters in all. Written as a count followed by the character it becomes 12W1B12W3B24W1B14W. That is 18 of 67 characters, and the ratio of the packed size to the unpacked size is 18 divided by 67, which is 0.269. Reading it back is as mechanical as writing it. Read the digits, read one character, write that character as many times as the digits say, and repeat until the code ends. Nothing is guessed and nothing is lost, which is what makes it a lossless method.

The method is not tied to one layout. The count can come first or second, and a popular variant writes counts only for runs of two or more, using the doubled character itself as the signal. On the same line that variant writes 21 characters instead of 18, because the single B characters no longer carry a count. Which form wins depends on how many runs are short.

A second family splits the stream into two: the characters on one side and the run lengths on the other. For this line that is the string WWBWWBBWWBWW and the numbers 12, 12, 3, 24 and 14. Each stream can then be squeezed further by another method, which is awkward when the counts sit between the characters and break up the pattern.

What counts as a character matters too. A JavaScript string is a sequence of 16-bit units, and some characters take two of them. The string iterator walks by Unicode code points and keeps such pairs together, so a run of equal characters is measured in whole characters, not in halves of them. It does not merge combining marks into what a reader sees as one letter, so a run here means a run of code points.

Why it shrinks long runs and enlarges busy data

The saving comes from one fact: a run costs only as many characters as its count has digits, plus one. A run of 9 costs two characters and a run of 99 costs three. The longer the stretch, the bigger the win, which is why the method suits icons, line drawings and flat-colour graphics, and why it does poorly on photographs.

The loss comes from the other end. A character that is alone still needs a count, so it costs two characters instead of one. Write ABCD and you get 1A1B1C1D: four characters become eight, and the ratio is 2.000. No two neighbours are equal, so there is nothing to merge, and the code is pure overhead.

That is not a flaw of this one scheme. It is a theorem about every lossless method. By the pigeonhole principle, no lossless compression algorithm can shrink all possible data, so some inputs must get longer by at least one symbol. The only open question is how badly, and whether a format limits the damage.

The ratio is also easy to quote backwards. The usual definition divides the uncompressed size by the compressed size, so the 67 and 18 above give about 3.72, where bigger is better. This page divides packed by unpacked, which gives 0.269, where smaller is better and a figure above 1 means growth. Always check which way a quoted ratio runs before comparing two of them.

One restriction comes straight from the format. The text form cannot hold digits in the text it encodes, because the code 12W is two digits and a letter, and a digit that was part of the data would be read as part of a count. A text with digits needs a different rule, and the byte form below is one.

PackBits: a literal or a repeat, told by one signed byte

PackBits was introduced by Apple with a paint program on the Macintosh, and it later appeared inside TIFF files. Baseline TIFF readers must handle three compression schemes, and PackBits is the byte-oriented run-length one. Targa files use the same packet idea on pixels rather than bytes, and ILBM files use PackBits too.

A PackBits stream is a series of packets. Each packet begins with one header byte, read as a signed number n, and the data follows. If n is from 0 to 127, then 1 + n bytes follow and are copied as they are: a literal packet. If n is from -1 to -127, one byte follows and is repeated 1 - n times: a repeat packet of 2 to 128 copies. A header of -128 does nothing. The stream has no end marker. A reader must already know the packed or the unpacked size, because nothing inside the data says where it stops. The page states its rule for that plainly: the whole input is one stream.

The format description gives a short example. The 15 bytes FE AA 02 80 00 2A FD AA 03 80 00 2A 22 F7 AA unpack to 24. The header FE is -2, so AA is written three times. The header 02 means 3 literal bytes, 80 00 2A. The header FD is -3, so AA four times, then 03 and four literal bytes, and finally F7 is -9, ten copies of AA. The ratio of packed to unpacked is 15 over 24, which is 0.625.

The signed header is the easiest place to make a mistake. In the browser a byte lives in a Uint8Array, an array of 8-bit unsigned integers, so the signed reading is only a way of interpreting the stored number. A repeat of one byte is written as 257 minus the run length, reduced mod 256, which is the same as 256 minus (run length minus 1) as an unsigned byte. For a run of 3 that is 254, hex FE, which reads as -2 and means three copies. Writing the header as minus n instead gives FD, which means four.

What a greedy encoder does, and the worst case

An encoder has to decide where to cut. This page uses a greedy rule. At each position, if a run of 3 or more equal bytes starts there, it becomes a repeat packet of at most 128 bytes. Everything else is collected into a literal packet of at most 128 bytes, with the header one less than the length. Why the threshold of 3? A repeat packet costs two bytes, and three equal bytes inside a literal packet cost three, so the repeat does not lose. For two equal bytes beside other data, splitting the literal packet costs an extra header, so the format description says such runs are typically written as literal data.

The packet limits make the worst case exact. A run of 128 equal bytes is one repeat packet, two bytes in all: header 81 and the byte. A run of 129 does not fit, so it needs a second packet, and the whole thing takes 4 bytes. A run of 130 leaves a remainder of 2, which goes into a literal packet, so it takes 5.

Data with no repeats shows the other limit. Take 200 bytes in which no neighbours are equal. The first 128 become one literal packet with header 7F, the last 72 another with header 47, and the output is 200 plus two headers, 202 bytes. In general the output is never more than n + ceil(n / 128) bytes. That bound is derived here, not quoted, and the page checks it on thousands of random strings.

Compare it with the text form. There the worst case is a doubling for any input, however long. PackBits grows by about 0.8 percent at most, one byte in 128. Other formats pay differently: PCX keeps a count in six bits and flags it with the top two bits of a byte, so every byte value from 192 to 255 must be stored as a pair even when it appears once, and such bytes double.

Whether a run may cross a row is a separate rule that formats disagree on. One older specification of Targa lets a repeat run on across scan lines, and the later one says it never should. A reader has to accept both, and a writer must follow the stricter rule. Nothing in the packet syntax settles it.

How this page checks itself

Every result goes through two routines. The encoder writes the code, and a separate decoder reads it back. The two are compared and reported as MATCH or MISMATCH, in words and with a mark. When the input is a stream written by some other packer, the round trip can still match while the stream differs, because the same bytes can be packed in more than one way. The decoder refuses bad input with a position. A count of zero, a count with no character after it, a header that promises more bytes than remain, a hex string with an odd number of digits, and a decoded size above 10 million all stop with a plain message naming where and why.

Larger inputs run in a background worker, which the Web Workers API describes as a thread separate from the main one, so the page stays responsive, with a time limit and a Cancel button. Small inputs are worked on the page in a few milliseconds, and the work is timed with the browser’s performance marks.

Hex input follows the rule of Python’s bytes.fromhex: two digits per byte, white space ignored. The values were also checked outside the page. A short Python program encodes 6,000 random texts and 6,000 random byte strings with the standard library, grouping equal neighbours, and the page’s output is compared with it exactly. That program also confirms that the 67-character line gives 18 characters and that the 15-byte stream unpacks to 24.

What the tool leaves out is as important. It does not cover Huffman coding, LZ77 or other compressors, runs in two dimensions, or the headers and rows of an image file. It will not tell you how well a real picture compresses, only how a given string or stream does under two stated rules.

Run-length encoding rewards data with long runs and punishes data without them. Packed over unpacked, a figure below 1 is a saving, a figure above 1 is a cost, and the worst case can be written down before the data is seen. That is the honest way to describe the method, and the reason to compute it rather than assume it.

Sources

Every source consulted for this guide. Links checked November 10, 2026.

  1. Run-length encoding Wikipedia
  2. PackBits Wikipedia
  3. Lossless compression Wikipedia
  4. Data compression ratio Wikipedia
  5. PCX - Wikipedia Wikipedia
  6. Truevision TGA Wikipedia
  7. TIFF - Wikipedia Wikipedia
  8. Web Workers API - Web APIs MDN Web Docs
  9. Uint8Array - JavaScript MDN Web Docs
  10. String.prototype[Symbol.iterator]() - JavaScript MDN Web Docs
  11. itertools (Python documentation) Python Software Foundation
  12. Built-in Types (Python documentation) Python Software Foundation
  13. Performance: measure() method - Web APIs MDN Web Docs