<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: datalog</title>
    <description>The latest articles on DEV Community by datalog (@datalog).</description>
    <link>https://dev.to/datalog</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4205559%2F95686e39-99ed-43ce-a7ca-6cf867e04574.png</url>
      <title>DEV Community: datalog</title>
      <link>https://dev.to/datalog</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/datalog"/>
    <language>en</language>
    <item>
      <title>How Tracing Contours Cut My rMQR SVG Path by 67%</title>
      <dc:creator>datalog</dc:creator>
      <pubDate>Wed, 11 Nov 2026 05:22:05 +0000</pubDate>
      <link>https://dev.to/datalog/how-tracing-contours-cut-my-rmqr-svg-path-by-67-3e1a</link>
      <guid>https://dev.to/datalog/how-tracing-contours-cut-my-rmqr-svg-path-by-67-3e1a</guid>
      <description>&lt;p&gt;Whenever I needed vector artwork to survive resizing, rotation, or a trip to the print shop, I had a familiar routine. Open Illustrator, select the pieces, click &lt;strong&gt;Unite&lt;/strong&gt; in Pathfinder, and get on with the job.&lt;/p&gt;

&lt;p&gt;Then I caught myself doing that to an rMQR Code generated by my own JavaScript library.&lt;/p&gt;

&lt;p&gt;The generator had already worked out exactly which modules were dark. It exported an SVG. And there I was, opening Illustrator to join the pieces it had just drawn. What was I doing?&lt;/p&gt;

&lt;p&gt;In my earlier posts, I was trying to save space in the &lt;a href="https://dev.to/datalog/how-i-shrunk-a-qr-kanji-table-by-36-using-dynamic-programming-1fmc"&gt;Kanji lookup table&lt;/a&gt; and then in the &lt;a href="https://dev.to/datalog/adding-a-14-kb-kanji-table-to-a-javascript-qr-generator-16le"&gt;encoded text&lt;/a&gt;. This time, the matrix was already finished. I wanted to draw it with less SVG and stop reaching for Pathfinder.&lt;/p&gt;

&lt;h2&gt;
  
  
  A dot here, a fence over there
&lt;/h2&gt;

&lt;p&gt;The obvious way to draw a QR Code is one square per dark module. It is easy to generate and easy to check. It also means that a solid block of dark modules is described as a pile of little squares, each with its own position and edges.&lt;/p&gt;

&lt;p&gt;I tried the usual ways of making the markup smaller. Move the fill color to the root element. Define a square once and reference it with &lt;code&gt;&amp;lt;use&amp;gt;&lt;/code&gt;. Eventually I settled on one &lt;code&gt;&amp;lt;path&amp;gt;&lt;/code&gt; with a little rectangular subpath for each dark module.&lt;/p&gt;

&lt;p&gt;Then I started joining neighbors. If five dark modules sit next to each other in a row, draw one rectangle five modules wide. Do the same down the columns, compare the two strings, and keep the shorter one.&lt;/p&gt;

&lt;p&gt;That helped, but the rectangles still met along internal edges. In some of my export and rendering workflows, I could see hairline seams after scaling or transforming artwork built from touching shapes. Sometimes the squares looked like dots. Sometimes the longer strips looked like a picket fence. The geometry said the pieces met. The rendered result occasionally disagreed.&lt;/p&gt;

&lt;p&gt;My usual workaround was to make the pieces a tiny bit bigger so they overlapped. The idea felt familiar from print. In offset printing, &lt;strong&gt;trapping&lt;/strong&gt; creates small overlaps to compensate for misregistration. In my SVG, I was borrowing the overlap trick to hide a seam. &lt;a href="https://helpx.adobe.com/illustrator/desktop/manage-colors/apply-transparency-and-blending/trap-colors-between-objects.html" rel="noopener noreferrer"&gt;Adobe’s guide to trapping&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It worked well enough that I kept doing it. Then came that next trip to Illustrator. I already had the grid in JavaScript, including the answer to whether every pair of neighbors belonged to the same dark region. Why was I asking a vector editor to figure that out again?&lt;/p&gt;

&lt;h2&gt;
  
  
  Four ways to draw the same grid
&lt;/h2&gt;

&lt;p&gt;I used the same &lt;strong&gt;43 × 13&lt;/strong&gt; rMQR matrix for all four versions. It has &lt;strong&gt;283 dark modules&lt;/strong&gt;. All three rectangle passes start at the bottom right. The per-module and horizontal passes go row by row, while the vertical pass goes column by column.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Subpaths&lt;/th&gt;
&lt;th&gt;Characters in &lt;code&gt;d&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;One square per module&lt;/td&gt;
&lt;td&gt;283&lt;/td&gt;
&lt;td&gt;3,676&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Merge vertical runs&lt;/td&gt;
&lt;td&gt;152&lt;/td&gt;
&lt;td&gt;1,978&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Merge horizontal runs&lt;/td&gt;
&lt;td&gt;159&lt;/td&gt;
&lt;td&gt;2,073&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Trace the contours&lt;/td&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;1,195&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The counts cover the &lt;code&gt;d&lt;/code&gt; attribute alone, before compression. All four methods use &lt;code&gt;z&lt;/code&gt; to close the final side. The 17 contour subpaths include the cutouts.&lt;/p&gt;

&lt;p&gt;Tracing reduced the path string by about &lt;strong&gt;67%&lt;/strong&gt; compared with individual squares and &lt;strong&gt;40%&lt;/strong&gt; compared with the shorter grouped path. For this grid, that was the vertical pass.&lt;/p&gt;

&lt;h3&gt;
  
  
  What those paths actually draw
&lt;/h3&gt;

&lt;p&gt;All four paths below draw the same solid &lt;code&gt;3 × 2&lt;/code&gt; block, from &lt;code&gt;(0, 0)&lt;/code&gt; to &lt;code&gt;(3, 2)&lt;/code&gt;. Each starts at the bottom-right corner, &lt;code&gt;(3, 2)&lt;/code&gt;, and follows the same clockwise direction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One square per module&lt;/strong&gt; Each of the six dark modules gets its own rectangle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;M3,2h-1v-1h1zM2,2h-1v-1h1zM1,2h-1v-1h1zM3,1h-1v-1h1zM2,1h-1v-1h1zM1,1h-1v-1h1z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Vertical runs&lt;/strong&gt; The two dark modules in each column form one rectangle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;M3,2h-1v-2h1zM2,2h-1v-2h1zM1,2h-1v-2h1z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Horizontal runs&lt;/strong&gt; The three dark modules in each row form one rectangle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;M3,2h-3v-1h3zM3,1h-3v-1h3z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Contours&lt;/strong&gt; The boundary of the whole block forms one closed rectangle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;m3,2h-3v-2h3z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fill is identical in every case. The difference is how many edges we ask SVG to draw.&lt;/p&gt;

&lt;center&gt;&lt;/center&gt;

&lt;p&gt;Left to right: six squares, three vertical runs, two horizontal runs, one contour.&lt;/p&gt;

&lt;p&gt;Parts × modules per part: 6 × 1 = 3 × 2 = 2 × 3 = 1 × 6 = 6 modules.&lt;/p&gt;

&lt;p&gt;The difference shows up when a shape extends in both directions. A solid &lt;code&gt;3 × 2&lt;/code&gt; block takes six squares, two horizontal strips, or three vertical strips. Its outline is one rectangle. The grid already tells us where that outline is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep the edges that matter
&lt;/h2&gt;

&lt;p&gt;For each dark module, &lt;code&gt;contourPath&lt;/code&gt; checks its four neighbors. A side belongs to the boundary if it faces a light module or the outside of the matrix. When two dark modules touch, their shared side contributes nothing to the outline.&lt;/p&gt;

&lt;center&gt;&lt;/center&gt;

&lt;p&gt;Left to right: two modules with directed edges; their shared side highlighted; the remaining outer boundary. The opposing edges on the shared side are removed.&lt;/p&gt;

&lt;p&gt;I store each surviving edge at its starting vertex on the grid. The four directions fit in a bit mask. The numbers beside the arrows in the diagram are their bit values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0001  =  1  |  right
0010  =  2  |  down
0100  =  4  |  left
1000  =  8  |  up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A vertex can have more than one outgoing edge, so several bits can be set at once.&lt;/p&gt;

&lt;center&gt;&lt;/center&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;At the shared vertex, two edges point out:

0100 | 0001   = 0101
left | right
   4 | 1      = 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each edge points in the direction that keeps the dark area on its right. That little rule determines which way the walk goes around the outside and which way it goes around a cutout.&lt;/p&gt;

&lt;h2&gt;
  
  
  Follow the edges into closed paths
&lt;/h2&gt;

&lt;p&gt;Starting at a vertex with an unused edge, the walk takes that edge, clears its bit, and pushes the next vertex onto a stack. When it reaches a vertex with no outgoing edges left, it pops back along the stack and records the route in reverse.&lt;/p&gt;

&lt;p&gt;The stack earns its keep when two dark squares meet at a corner. There can be more than one way out of that vertex. The walk can return to it and take the remaining branch, visiting the same vertex twice in one closed route. When that route is complete, the outer loop looks for another unused edge elsewhere in the grid.&lt;/p&gt;

&lt;p&gt;When writing the SVG, I keep the turns. Five steps to the right become &lt;code&gt;h5&lt;/code&gt;. Three steps up become &lt;code&gt;v-3&lt;/code&gt;. The closing &lt;code&gt;z&lt;/code&gt; brings the path back to its starting vertex.&lt;/p&gt;

&lt;center&gt;&lt;/center&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. At the shared vertex, choose the right edge.
   Clear bit 1: 0101 -&amp;gt; 0100

2. The top and right edges have been followed.
   Choose the bottom edge, pointing left.

3. The right loop is complete.
   At the shared vertex again, choose the left edge.
   Clear bit 4: 0100 -&amp;gt; 0000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The orange point is the shared vertex. Red marks the selected edge, green the edges already followed, and blue the unused edges. Continuing around the left loop joins both loops into one closed route.&lt;/p&gt;

&lt;h2&gt;
  
  
  A hole is a contour too
&lt;/h2&gt;

&lt;p&gt;Take a &lt;code&gt;3 × 3&lt;/code&gt; block of dark modules and remove the center. The walk finds an outside boundary and a smaller boundary around the light module.&lt;/p&gt;

&lt;p&gt;Following the dark-on-the-right rule takes the outside boundary clockwise and the inside boundary counterclockwise. Both go into the same SVG path. Under SVG’s default &lt;code&gt;nonzero&lt;/code&gt; fill rule, their winding contributions cancel inside the hole, so the background shows through. &lt;a href="https://www.w3.org/TR/SVG11/painting.html#FillRuleProperty" rel="noopener noreferrer"&gt;SVG fill rules&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;&lt;/center&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Left:  8 dark modules, 1 empty center.
Right: the same shape, traced as two contours.

Outer boundary: clockwise.
Inner boundary: counterclockwise.

Inside the ring: one winding contribution.
Inside the hole: opposite contributions cancel.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the exported SVG, the foreground is a single filled path.&lt;/p&gt;

&lt;center&gt;&lt;/center&gt;

&lt;h2&gt;
  
  
  Try it on your own rMQR Code
&lt;/h2&gt;

&lt;p&gt;Set &lt;code&gt;vrb: 1&lt;/code&gt; to get a square per dark module, or use the default to get the traced path. Both start from the same encoded matrix, so you can compare the SVGs without changing the message or the symbol.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://datalog.github.io/demo/rmqr-generator/" rel="noopener noreferrer"&gt;Open the live demo&lt;/a&gt;, expand &lt;strong&gt;Inside rMQR Code&lt;/strong&gt;, and turn on &lt;strong&gt;Explore SVG contours&lt;/strong&gt; to see the routes and cutouts.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>svg</category>
      <category>algorithms</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Adding a 14 KB Kanji Table to a JavaScript QR Generator</title>
      <dc:creator>datalog</dc:creator>
      <pubDate>Thu, 29 Oct 2026 03:42:07 +0000</pubDate>
      <link>https://dev.to/datalog/adding-a-14-kb-kanji-table-to-a-javascript-qr-generator-16le</link>
      <guid>https://dev.to/datalog/adding-a-14-kb-kanji-table-to-a-javascript-qr-generator-16le</guid>
      <description>&lt;p&gt;In my &lt;a href="https://dev.to/datalog/how-i-shrunk-a-qr-kanji-table-by-36-using-dynamic-programming-1fmc"&gt;previous post&lt;/a&gt;, I described how I reduced a QR Kanji lookup table by 36% using dynamic programming.&lt;/p&gt;

&lt;p&gt;That table is now integrated into &lt;a href="https://github.com/datalog/qrcode-generator" rel="noopener noreferrer"&gt;datalog/qrcode-generator&lt;/a&gt;. It provides the character mapping needed for Kanji mode without an external encoding package.&lt;/p&gt;

&lt;p&gt;The integration also gave me a few useful test cases for choosing encoding modes, including characters I had not expected Kanji mode to support.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Kanji support
&lt;/h2&gt;

&lt;p&gt;Kanji mode encodes supported characters in 13 bits each, compared with the 24 bits typically needed for Japanese characters in UTF-8.&lt;/p&gt;

&lt;p&gt;The table provides the character mapping. The segment optimizer chooses modes based on character costs and segment headers.&lt;/p&gt;

&lt;h2&gt;
  
  
  A space stays a space
&lt;/h2&gt;

&lt;p&gt;Consider this input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;東京 大阪
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ordinary space (&lt;code&gt;U+0020&lt;/code&gt;) cannot be encoded in Kanji mode. The encoder preserves it and uses another mode for that part of the input.&lt;/p&gt;

&lt;p&gt;For this example, the optimizer produces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Kanji("東京") → Alphanumeric(" ") → Kanji("大阪")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A single space is valid in Alphanumeric mode, so it gets its own segment.&lt;/p&gt;

&lt;p&gt;All bit counts below use the segment-header widths for QR versions 1–9. They exclude the terminator, padding, and error correction.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Segment&lt;/th&gt;
&lt;th&gt;Header bits&lt;/th&gt;
&lt;th&gt;Payload bits&lt;/th&gt;
&lt;th&gt;Total&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Kanji: &lt;code&gt;東京&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;26&lt;/td&gt;
&lt;td&gt;38&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alphanumeric: space&lt;/td&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kanji: &lt;code&gt;大阪&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;26&lt;/td&gt;
&lt;td&gt;38&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;37&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;58&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;95&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Encoding the whole string as UTF-8 requires 13 bytes: three for each Japanese character and one for the space. Including the Byte segment header, that is &lt;strong&gt;116 bits&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Splitting the text saves &lt;strong&gt;21 bits&lt;/strong&gt;, even after paying for three segment headers.&lt;/p&gt;

&lt;p&gt;There is another option if you control the input. JIS X 0208 includes the ideographic space (&lt;code&gt;U+3000&lt;/code&gt;), which can be encoded in Kanji mode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;東京　大阪
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that space, the whole string fits in one Kanji segment: &lt;strong&gt;12 + 5 × 13 = 77 bits&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The ideographic space is usually wider than an ordinary space and can look out of place in other text. It is also a different Unicode character, so the generator does not substitute it automatically.&lt;/p&gt;

&lt;h2&gt;
  
  
  When switching modes pays off
&lt;/h2&gt;

&lt;p&gt;Keeping ordinary spaces means accounting for segment overhead. Whether switching modes saves bits depends on the surrounding characters.&lt;/p&gt;

&lt;p&gt;JIS X 0208, the character set used by QR Kanji mode, also includes Greek and Cyrillic letters.&lt;/p&gt;

&lt;p&gt;Supported letters from these blocks can use the same Kanji encoding path. For example, consider &lt;code&gt;ΑΒΓ&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Kanji:  12 + 3 × 13 = 51 bits
Byte:   12 + 3 × 16 = 60 bits
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The saving is smaller than for the Japanese example: three bits per letter instead of eleven. That leaves less room to cover additional segment headers.&lt;/p&gt;

&lt;p&gt;Adding an ordinary space shows the difference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ΑΒΓ ΔΕΖ
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this input, the optimizer keeps the whole string in Byte mode.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Encoding&lt;/th&gt;
&lt;th&gt;Total bits&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;One Byte segment&lt;/td&gt;
&lt;td&gt;116&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kanji → Alphanumeric space → Kanji&lt;/td&gt;
&lt;td&gt;121&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Both choices preserve exactly the same text. Splitting it saves payload bits but spends more on headers.&lt;/p&gt;

&lt;p&gt;Longer runs can change the result. For this input, with ten Greek letters on each side of the space:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;αβγδεζηθικ λμνξοπρστυ
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the optimizer selects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Kanji("αβγδεζηθικ")
Alphanumeric(" ")
Kanji("λμνξοπρστυ")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That takes &lt;strong&gt;303 bits&lt;/strong&gt;, compared with &lt;strong&gt;340 bits&lt;/strong&gt; for one UTF-8 Byte segment.&lt;/p&gt;

&lt;p&gt;The space adds segment overhead. Whether splitting is worthwhile depends on how much the surrounding runs save.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing segments
&lt;/h2&gt;

&lt;p&gt;The optimizer uses dynamic programming with four modes: Numeric, Alphanumeric, Byte, and Kanji.&lt;/p&gt;

&lt;p&gt;It walks through the input, keeping a running cost for each mode. Continuing in the current mode adds the character cost; switching modes also adds a new segment header.&lt;/p&gt;

&lt;p&gt;Costs are represented in sixths of a bit. Numeric mode encodes groups of three digits in ten bits, while Alphanumeric mode encodes pairs of characters in eleven bits. Using sixths lets the optimizer represent their average character costs with integers and account for rounding when a segment ends.&lt;/p&gt;

&lt;p&gt;The encoder then works backward through the stored history to reconstruct the selected segments. With four modes, that history uses O(N) memory, where N is the number of input code points.&lt;/p&gt;

&lt;p&gt;This lets the encoder weigh character savings against segment overhead without requiring the caller to split the text.&lt;/p&gt;

&lt;h2&gt;
  
  
  From bits to a QR symbol
&lt;/h2&gt;

&lt;p&gt;Saving bits does not always produce a smaller QR grid. Each QR version has a fixed capacity, so the data must fit into a lower version before the grid gets smaller.&lt;/p&gt;

&lt;p&gt;The generator also tries to increase the error-correction level without increasing the selected version. A shorter bitstream can leave room for stronger error correction, even when the grid dimensions stay the same.&lt;/p&gt;

&lt;p&gt;You can try these examples in the &lt;a href="https://datalog.github.io/demo/qrcode-generator/" rel="noopener noreferrer"&gt;interactive demo&lt;/a&gt; and inspect the selected segments.&lt;/p&gt;

&lt;p&gt;The source code is available at &lt;a href="https://github.com/datalog/qrcode-generator" rel="noopener noreferrer"&gt;datalog/qrcode-generator&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>qrcode</category>
      <category>opensource</category>
    </item>
    <item>
      <title>[Boost]</title>
      <dc:creator>datalog</dc:creator>
      <pubDate>Wed, 28 Oct 2026 21:26:13 +0000</pubDate>
      <link>https://dev.to/datalog/-6hn</link>
      <guid>https://dev.to/datalog/-6hn</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/arelvox/how-to-check-your-monitor-refresh-rate-hz-in-the-browser-with-javascript-5377" class="crayons-story__hidden-navigation-link"&gt;How to Check Your Monitor Refresh Rate (Hz) in the Browser with JavaScript&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/arelvox" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4204802%2F2cf0d6f8-6ca7-4313-942b-d5126ea91231.png" alt="arelvox profile" class="crayons-avatar__image" width="" height=""&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/arelvox" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Arelvox
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Arelvox
                
                
              
              &lt;div id="story-author-preview-content-4931040" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/arelvox" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4204802%2F2cf0d6f8-6ca7-4313-942b-d5126ea91231.png" class="crayons-avatar__image" alt="" width="" height=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Arelvox&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/arelvox/how-to-check-your-monitor-refresh-rate-hz-in-the-browser-with-javascript-5377" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Oct 27&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/arelvox/how-to-check-your-monitor-refresh-rate-hz-in-the-browser-with-javascript-5377" id="article-link-4931040"&gt;
          How to Check Your Monitor Refresh Rate (Hz) in the Browser with JavaScript
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/javascript"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;javascript&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/webdev"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;webdev&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/tutorial"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;tutorial&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/hardware"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;hardware&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/arelvox/how-to-check-your-monitor-refresh-rate-hz-in-the-browser-with-javascript-5377" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/fire-f60e7a582391810302117f987b22a8ef04a2fe0df7e3258a5f49332df1cec71e.svg" width="24" height="24"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;1&lt;span class="hidden s:inline"&gt;&amp;nbsp;reaction&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/arelvox/how-to-check-your-monitor-refresh-rate-hz-in-the-browser-with-javascript-5377#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              

              &lt;span class="hidden s:inline"&gt;Add&amp;nbsp;Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            3 min read
          &lt;/small&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>tutorial</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>How I Shrunk a QR Kanji Table by 36% Using Dynamic Programming</title>
      <dc:creator>datalog</dc:creator>
      <pubDate>Sun, 25 Oct 2026 00:01:20 +0000</pubDate>
      <link>https://dev.to/datalog/how-i-shrunk-a-qr-kanji-table-by-36-using-dynamic-programming-1fmc</link>
      <guid>https://dev.to/datalog/how-i-shrunk-a-qr-kanji-table-by-36-using-dynamic-programming-1fmc</guid>
      <description>&lt;p&gt;&lt;em&gt;QR codes have a dedicated Kanji mode that encodes supported characters in 13 bits each. A character like 龍 takes 24 bits in UTF-8, so choosing Kanji mode can save useful space.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For my JavaScript implementation, I needed to ship a lookup table mapping Unicode values to QR Kanji indices. The challenge was to preserve every mapping while keeping the embedded data as small as possible.&lt;/p&gt;

&lt;p&gt;The table is only 16,384 bytes as binary data. Encode those bytes as Base64, though, and it becomes a 21,848-character string. I reduced that to &lt;strong&gt;13,984 characters&lt;/strong&gt; by describing the table with a small command language and using dynamic programming to choose the commands.&lt;/p&gt;

&lt;p&gt;That is a &lt;strong&gt;35.99% reduction in Base64 size&lt;/strong&gt;, or &lt;strong&gt;28.84% after gzip&lt;/strong&gt;, measured on the table data alone. Here is how it works—and what those numbers leave out.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the table is for
&lt;/h2&gt;

&lt;p&gt;The table maps 8,192 possible QR Kanji indices to 16-bit Unicode values. There are 6,879 occupied positions; the rest contain &lt;code&gt;0xFFFF&lt;/code&gt;, a marker for an unused slot.&lt;/p&gt;

&lt;p&gt;The table includes kanji, hiragana, katakana, and symbols. Its order matters: moving a character would change its QR Kanji index.&lt;/p&gt;

&lt;p&gt;For QR generation, the useful lookup goes in the other direction: given a Unicode value, find the corresponding 13-bit index. The project's decoder builds that reverse lookup at initialization.&lt;/p&gt;

&lt;p&gt;The task here is to store this particular reference table compactly while preserving every value and every empty position.&lt;/p&gt;

&lt;h2&gt;
  
  
  Base64 is a representation, not compression
&lt;/h2&gt;

&lt;p&gt;Each source position takes two bytes, including empty positions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8,192 positions × 2 bytes = 16,384 bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Turning those bytes into source text adds overhead:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Representation&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Raw 16-bit values&lt;/td&gt;
&lt;td&gt;16,384&amp;nbsp;bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Joined hexadecimal string&lt;/td&gt;
&lt;td&gt;32,768&amp;nbsp;chars&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Base64 of the raw bytes&lt;/td&gt;
&lt;td&gt;21,848&amp;nbsp;chars&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;String sizes exclude quotes. Both textual representations use ASCII characters, so their character counts also equal their UTF-8 byte counts.&lt;/p&gt;

&lt;p&gt;Base64 is convenient for embedding bytes in JavaScript, but it does not make the data smaller. To reduce the string, I first needed a smaller byte stream.&lt;/p&gt;

&lt;h2&gt;
  
  
  Store instructions instead of individual values
&lt;/h2&gt;

&lt;p&gt;The table is irregular, but it has useful local structure: empty runs, sequences with a constant step, and small differences between neighboring nonempty values.&lt;/p&gt;

&lt;p&gt;The format describes those patterns with five command types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;00&lt;/code&gt; — &lt;strong&gt;&lt;code&gt;A(value)&lt;/code&gt;&lt;/strong&gt;: Emit one explicit 16-bit value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;01&lt;/code&gt; — &lt;strong&gt;&lt;code&gt;E(count)&lt;/code&gt;&lt;/strong&gt;: Advance over empty positions, preserving the previous nonempty value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;02&lt;/code&gt; — &lt;strong&gt;&lt;code&gt;R(start, step, count)&lt;/code&gt;&lt;/strong&gt;: Emit at least three values from an explicit start.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;03&lt;/code&gt; — &lt;strong&gt;&lt;code&gt;P(step, count)&lt;/code&gt;&lt;/strong&gt;: Emit at least two values, beginning at &lt;code&gt;previous + step&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;04&lt;/code&gt; and above — &lt;strong&gt;&lt;code&gt;D(delta)&lt;/code&gt;&lt;/strong&gt;: Emit the previous nonempty value plus a signed difference.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tokens are shown in hexadecimal. Full values occupy two bytes, most significant byte first. Tokens, counts, and encoded steps use unsigned varints, with seven data bits per byte.&lt;/p&gt;

&lt;p&gt;ZigZag encoding maps small signed numbers to small unsigned numbers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Signed:   0  -1  +1  -2  +2
Unsigned: 0   1   2   3   4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A delta is stored directly in its token as &lt;code&gt;zigzag(delta) + 4&lt;/code&gt;. There is no separate opcode byte for each delta: &lt;code&gt;06&lt;/code&gt;, for example, means &lt;code&gt;D(+1)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Arithmetic wraps modulo 65,536. Deltas and steps use the range −32,768 through 32,767; &lt;code&gt;0xFFFF&lt;/code&gt; remains reserved for empty entries. Series never cross empty positions.&lt;/p&gt;

&lt;h2&gt;
  
  
  A sequence in five bytes
&lt;/h2&gt;

&lt;p&gt;One part of the table contains these 20 consecutive Unicode values, from &lt;code&gt;0x3041&lt;/code&gt; through &lt;code&gt;0x3054&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ぁあぃいぅうぇえぉおかがきぎくぐけげこご
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Storing them directly takes 40 bytes. The Delta encoder stores the first value in full, followed by nineteen &lt;code&gt;+1&lt;/code&gt; deltas:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A(3041), D(+1), D(+1), ...
3 bytes + 19 bytes = 22 bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Full encoder describes the same fragment with a five-byte series command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;R(3041, +1, 20)

02 30 41 02 14
│  └─┬─┘ │  └── count: 20
│    │   └───── ZigZag(+1): 2
│    └───────── start: 0x3041
└────────────── explicit-series token
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As Base64, the original fragment takes 56 characters. The series takes eight: &lt;code&gt;AjBBAhQ=&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is an especially regular fragment, so it illustrates the format rather than predicting the compression ratio for the whole table.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing commands with dynamic programming
&lt;/h2&gt;

&lt;p&gt;Having a compact series command does not tell us where to use it. At a given position, the encoder may have several valid choices: one full value, one delta, or series of different lengths.&lt;/p&gt;

&lt;p&gt;A series has setup costs. An explicit series costs five bytes when its step and count each fit in one byte. If its values can already be written as three one-byte deltas, using a series would make the stream larger.&lt;/p&gt;

&lt;p&gt;The encoder compares &lt;strong&gt;the command's cost plus the best cost of everything after it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let &lt;code&gt;cost[i]&lt;/code&gt; be the minimum number of bytes needed to encode the suffix starting at position &lt;code&gt;i&lt;/code&gt;. The terminal cost is zero:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Conceptual recurrence; candidate generation is omitted.&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;Infinity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;command&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nf"&gt;candidatesAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;command&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;command&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;byteLength&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;command&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The implementation works backward through the table, evaluates full values, deltas, and valid series lengths, and handles empty runs. It records the selected command at each position. A forward pass then writes the chosen stream.&lt;/p&gt;

&lt;p&gt;There is an important reason one cost per position is enough: &lt;strong&gt;the previous nonempty value is determined by the source table&lt;/strong&gt;, regardless of which commands encoded the prefix. A full value and a delta that emit the same value leave the same reference for the next command. The encoder precomputes that reference at each position.&lt;/p&gt;

&lt;p&gt;This gives a shortest-path interpretation: positions are nodes, commands are edges, and encoded byte lengths are weights. The optimizer minimizes the binary stream size for this format. It does &lt;strong&gt;not&lt;/strong&gt; optimize the eventual gzip size.&lt;/p&gt;

&lt;h2&gt;
  
  
  Results across the complete table
&lt;/h2&gt;

&lt;p&gt;The Delta encoder is a simplified baseline: it uses one initial full value, individual deltas, and empty-run commands. The Full encoder also selects full values and series, using dynamic programming to minimize the complete binary stream.&lt;/p&gt;

&lt;p&gt;Here are the results for all 8,192 positions:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Binary stream&lt;/th&gt;
&lt;th&gt;Base64&lt;/th&gt;
&lt;th&gt;Gzip of Base64&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Original&lt;/td&gt;
&lt;td&gt;16,384 bytes&lt;/td&gt;
&lt;td&gt;21,848&amp;nbsp;chars&lt;/td&gt;
&lt;td&gt;14,033 bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Delta encoder&lt;/td&gt;
&lt;td&gt;10,784 bytes&lt;/td&gt;
&lt;td&gt;14,380&amp;nbsp;chars&lt;/td&gt;
&lt;td&gt;10,094 bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Full encoder&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;10,488 bytes&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;13,984&amp;nbsp;chars&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;9,986 bytes&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Full encoder savings vs. Original&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;35.99%&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;35.99%&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;28.84%&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The Full encoder reduces Base64 size by &lt;strong&gt;35.99%&lt;/strong&gt; compared with the Original representation, or &lt;strong&gt;28.84% after gzip&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Gzip sizes were measured on standalone Base64 strings at level 9, excluding decoder code and JavaScript wrappers. Results for a complete bundle may differ.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens at runtime
&lt;/h2&gt;

&lt;p&gt;Only the encoded data and decoder need to ship to the client; the optimizer can run beforehand.&lt;/p&gt;

&lt;p&gt;The scripts have no external dependencies. To use the pre-encoded table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"kanji-lookup-table-base64.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"lookup-table-decoder.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;lookup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;decodeKanjiTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;KANJI_LOOKUP_TABLE_BASE64&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mh"&gt;0x9F8D&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// 4340: 龍&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The decoder returns an &lt;code&gt;Int16Array(65536)&lt;/code&gt; indexed by Unicode value, containing a QR Kanji index or &lt;code&gt;-1&lt;/code&gt; for an unsupported character. This reusable lookup occupies &lt;strong&gt;128 KiB&lt;/strong&gt; in memory; the compression savings apply to the embedded data.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I took away
&lt;/h2&gt;

&lt;p&gt;The useful step was finding a small vocabulary for the data: full values, deltas, empty runs, and arithmetic series. Dynamic programming then removed the guesswork from choosing between those representations.&lt;/p&gt;

&lt;p&gt;General-purpose compression still helped after that transformation. Whether the same approach is worthwhile elsewhere depends on the data, the decoder, and the complete application bundle.&lt;/p&gt;

&lt;p&gt;The source, format details, and demo are available at &lt;strong&gt;&lt;a href="https://github.com/datalog/kanji-base64" rel="noopener noreferrer"&gt;datalog/kanji-base64&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Have you used a small custom format to shrink static data in a JavaScript application? I'd be interested to hear which patterns made the difference.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>performance</category>
      <category>webdev</category>
      <category>kanji</category>
    </item>
  </channel>
</rss>
