<?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: Toolsy Hub</title>
    <description>The latest articles on DEV Community by Toolsy Hub (@toolsyhub).</description>
    <link>https://dev.to/toolsyhub</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%2F4199854%2Fa847f63d-5033-4d13-9bd8-2895096bc323.png</url>
      <title>DEV Community: Toolsy Hub</title>
      <link>https://dev.to/toolsyhub</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/toolsyhub"/>
    <language>en</language>
    <item>
      <title>Why two tools give different SHA-256 hashes for the same text</title>
      <dc:creator>Toolsy Hub</dc:creator>
      <pubDate>Thu, 03 Dec 2026 00:50:40 +0000</pubDate>
      <link>https://dev.to/toolsyhub/why-two-tools-give-different-sha-256-hashes-for-the-same-text-1oh3</link>
      <guid>https://dev.to/toolsyhub/why-two-tools-give-different-sha-256-hashes-for-the-same-text-1oh3</guid>
      <description>&lt;p&gt;You paste a string into one hash tool, run the same string through &lt;code&gt;sha256sum&lt;/code&gt;, and the digests do not match. Nothing is broken. A hash is computed over bytes, and "the same text" can be different bytes in three common ways.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A trailing newline is part of the input.&lt;/strong&gt; &lt;code&gt;echo&lt;/code&gt; adds one, &lt;code&gt;printf&lt;/code&gt; does not:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'hello'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; sha256sum  &lt;span class="c"&gt;# 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; hello    &lt;span class="o"&gt;|&lt;/span&gt; sha256sum  &lt;span class="c"&gt;# 5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second digest is the hash of &lt;code&gt;hello\n&lt;/code&gt;, six bytes. Windows line endings add a third variant: &lt;code&gt;hello\r\n&lt;/code&gt; gives &lt;code&gt;cd2eca35...&lt;/code&gt;. If a file hash differs after a copy through an editor or git, check the line endings first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The encoding changes the bytes.&lt;/strong&gt; Hash functions never see characters, only bytes, so the text has to be encoded first. Most tools use UTF-8. The same word in UTF-8, UTF-16 and Latin-1 is three different inputs:&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;createHash&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"node:crypto"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;sha&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;createHash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"sha256"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hex"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;sha&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"café"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                              &lt;span class="c1"&gt;// 850f7dc4...&lt;/span&gt;
&lt;span class="nf"&gt;sha&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"café"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"utf16le"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;// 8c9f3eed...&lt;/span&gt;
&lt;span class="nf"&gt;sha&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"café"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"latin1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;// dafd66c0...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A tool that encodes the text as UTF-16 will not match one that used UTF-8, even though you typed the same word into both.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Looks identical, is not identical.&lt;/strong&gt; Unicode lets an accented letter be one character or a letter plus a combining mark:&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"café"&lt;/span&gt;   &lt;span class="c1"&gt;// 4 characters&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"café"&lt;/span&gt;  &lt;span class="c1"&gt;// 5 characters, renders the same&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;                 &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nf"&gt;sha&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;// 850f7dc4&lt;/span&gt;
&lt;span class="nf"&gt;sha&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;// 81ef060b&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"NFC"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"NFC"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you compare hashes of user-typed text, normalize first (NFC is the usual choice) and say so in the spec.&lt;/p&gt;

&lt;p&gt;To rule out all three, hash a known value on both sides, for example the five bytes &lt;code&gt;hello&lt;/code&gt;, which must give the &lt;code&gt;2cf24dba...&lt;/code&gt; digest above, and then compare your real input byte for byte.&lt;/p&gt;

&lt;p&gt;The Hash Generator on Toolsy Hub hashes text as UTF-8 and shows MD5, SHA-1, SHA-256, SHA-384 and SHA-512 together, in your browser: &lt;a href="https://www.toolsy-hub.com/tools/hash-generator" rel="noopener noreferrer"&gt;https://www.toolsy-hub.com/tools/hash-generator&lt;/a&gt;&lt;/p&gt;

</description>
      <category>security</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Getting a PDF under the LinkedIn document limit: 100 MB and 300 pages</title>
      <dc:creator>Toolsy Hub</dc:creator>
      <pubDate>Fri, 27 Nov 2026 04:07:10 +0000</pubDate>
      <link>https://dev.to/toolsyhub/getting-a-pdf-under-the-linkedin-document-limit-100-mb-and-300-pages-31kh</link>
      <guid>https://dev.to/toolsyhub/getting-a-pdf-under-the-linkedin-document-limit-100-mb-and-300-pages-31kh</guid>
      <description>&lt;p&gt;A LinkedIn document post takes a PDF, but it has to pass two checks at once: 100 MB or less, and 300 pages or less. LinkedIn's help page words it as "The file size cannot exceed 100MB and 300 pages." A deck that fails one check will not pass by fixing the other. This is how to find out which one you broke and fix just that.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Check both numbers
&lt;/h2&gt;

&lt;p&gt;On Linux or macOS, with poppler installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pdfinfo deck.pdf &lt;span class="o"&gt;|&lt;/span&gt; grep Pages
ls -l deck.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With qpdf, which also reports a broken file instead of guessing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;qpdf --show-npages deck.pdf
qpdf --check deck.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Windows and macOS file properties give the size, and any PDF viewer gives the page count.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Too many pages: split by range
&lt;/h2&gt;

&lt;p&gt;Do not compress a file that is over 300 pages, because compressing keeps every page. Cut it into parts instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;qpdf deck.pdf --pages . 1-150 -- part1.pdf
qpdf deck.pdf --pages . 151-300 -- part2.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each part becomes its own post. Pick the split point at a section break, not mid-argument.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Too big: find out what is heavy
&lt;/h2&gt;

&lt;p&gt;Most oversized decks are heavy because of a few full-resolution photos or screenshots, not the text. Ghostscript can re-sample the images:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.5 -dPDFSETTINGS=/ebook \
   -dNOPAUSE -dQUIET -dBATCH -sOutputFile=deck-small.pdf deck.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;/ebook&lt;/code&gt; aims at about 150 dpi images, &lt;code&gt;/screen&lt;/code&gt; at about 72 dpi. For a slide deck that is only ever read on a phone or laptop, &lt;code&gt;/ebook&lt;/code&gt; is usually indistinguishable on screen. Text stays selectable, since only the images are re-sampled.&lt;/p&gt;

&lt;p&gt;One thing to watch: a PDF made by printing slides to images (every page is a picture) will barely shrink with a text-preserving method, because there is no text to squeeze. There the only lever is image quality and resolution.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. No command line, or you do not want to upload the file
&lt;/h2&gt;

&lt;p&gt;If you would rather not install anything, or the deck has client information in it, a browser tool that works locally means the file never leaves your machine. I wrote a short guide covering the LinkedIn limits with links to LinkedIn's own help pages: &lt;a href="https://www.toolsy-hub.com/blog/linkedin-document-upload-size-limit" rel="noopener noreferrer"&gt;LinkedIn document upload size limit&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other places with a much smaller cap
&lt;/h2&gt;

&lt;p&gt;LinkedIn is generous. Job portals, university application systems and government forms often cap a PDF at 1 MB or even less, and the same two steps apply: check the size, then shrink to a target. For those, lowering image resolution is what gets a scan under the line. If you want a browser tool that aims for a target size directly, there is a preset for 1 MB here: &lt;a href="https://www.toolsy-hub.com/tools/compress-pdf/1mb" rel="noopener noreferrer"&gt;compress PDF to 1 MB&lt;/a&gt;. It reports when a file cannot reach the target, so you know whether to scan at a lower resolution instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Count pages and size before you upload, not after the error message.&lt;/li&gt;
&lt;li&gt;Pages over the limit: split. Size over the limit: re-sample images.&lt;/li&gt;
&lt;li&gt;Keep the original. Compression is one-way.&lt;/li&gt;
&lt;li&gt;Open the result and read one page at full zoom before posting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I build free browser tools at Toolsy Hub, so the two links above are mine. The commands work without them.&lt;/p&gt;

</description>
      <category>pdf</category>
      <category>linkedin</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
    <item>
      <title>JSON that parses without an error and still comes out different</title>
      <dc:creator>Toolsy Hub</dc:creator>
      <pubDate>Thu, 26 Nov 2026 00:50:42 +0000</pubDate>
      <link>https://dev.to/toolsyhub/json-that-parses-without-an-error-and-still-comes-out-different-1bm9</link>
      <guid>https://dev.to/toolsyhub/json-that-parses-without-an-error-and-still-comes-out-different-1bm9</guid>
      <description>&lt;p&gt;A syntax error is the easy JSON bug: the parser tells you the line and column. The harder ones are documents that parse cleanly and hand you different data than the file held. Two come up often.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Big integers lose digits.&lt;/strong&gt; JSON has no integer limit, but &lt;code&gt;JSON.parse&lt;/code&gt; reads every number as a double, which is exact only up to 2^53 - 1.&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="nb"&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="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'{"id": 9007199254740993}'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 9007199254740992&lt;/span&gt;
&lt;span class="nb"&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="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;MAX_SAFE_INTEGER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                    &lt;span class="c1"&gt;// 9007199254740991&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The id ends in 2 instead of 3, no error. Twitter-style snowflake ids and database bigints hit this. The fix on the wire is to send such ids as strings. If you cannot change the sender, current Node and browsers let a reviver see the original text:&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="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'{"id":9007199254740993}'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"number"&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;v&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="n"&gt;id&lt;/span&gt;
&lt;span class="c1"&gt;// '9007199254740993'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Duplicate keys keep the last value.&lt;/strong&gt; The spec says names "SHOULD be unique" and leaves the rest to the parser. In practice the last one wins and the first disappears:&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="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'{"a":1,"a":2}'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// {"a":2}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Merge two config files by hand and this is how a setting silently reverts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax errors, for completeness.&lt;/strong&gt; A trailing comma gives &lt;code&gt;Expected double-quoted property name in JSON at position 7 (line 1 column 8)&lt;/code&gt;, and single quotes give &lt;code&gt;Unexpected token ''', "{"a":'x'}" is not valid JSON&lt;/code&gt;. Messages differ between engines, so a formatter that prints the line and column itself is easier to scan.&lt;/p&gt;

&lt;p&gt;The JSON Formatter on Toolsy Hub checks all three: it shows the line and column of a syntax error, counts integers past the safe limit and lists keys that repeat inside one object, before it formats or minifies. It runs in your browser: &lt;a href="https://www.toolsy-hub.com/tools/json-formatter" rel="noopener noreferrer"&gt;https://www.toolsy-hub.com/tools/json-formatter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>json</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Why Base64 is a third bigger, and why btoa breaks on accented text</title>
      <dc:creator>Toolsy Hub</dc:creator>
      <pubDate>Thu, 19 Nov 2026 04:13:50 +0000</pubDate>
      <link>https://dev.to/toolsyhub/why-base64-is-a-third-bigger-and-why-btoa-breaks-on-accented-text-5dbo</link>
      <guid>https://dev.to/toolsyhub/why-base64-is-a-third-bigger-and-why-btoa-breaks-on-accented-text-5dbo</guid>
      <description>&lt;p&gt;Base64 turns bytes into text so they can travel through places that only accept text: JSON bodies, data URLs, email, a CSS file. The price is size. Every 3 bytes become 4 characters, so the output is a third bigger, plus padding at the end.&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&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="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 4 4 4 136 1336&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Measured on random bytes from &lt;code&gt;crypto.randomBytes&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;bytes     base64 chars   ratio
1         4              4.0000
2         4              2.0000
3         4              1.3333
100       136            1.3600
1000      1336           1.3360
1048576   1398104        1.3333
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Small inputs look worse because of padding. A 1 MB file ends up as 1,398,104 characters, which is the number to keep in mind when an API caps the request body. A 5 MB file becomes 6.67 MB:&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;
&lt;span class="nb"&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="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;3&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="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1048576&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// 5242880 6990508 6.67&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is why the Base64 tool on Toolsy Hub stops encoding files at 5 MB: the encoded text has to sit in memory and on the clipboard, and it is a third larger than the file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Padding
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;=&lt;/code&gt; signs fill the last group of 4. One leftover byte gives &lt;code&gt;==&lt;/code&gt;, two give &lt;code&gt;=&lt;/code&gt;, three give none:&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="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="n"&gt;s&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'ab'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'abc'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&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="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// "a" YQ==&lt;/span&gt;
&lt;span class="c1"&gt;// "ab" YWI=&lt;/span&gt;
&lt;span class="c1"&gt;// "abc" YWJj&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Browsers are forgiving about missing padding in &lt;code&gt;atob&lt;/code&gt; (&lt;code&gt;atob('YQ')&lt;/code&gt; returns &lt;code&gt;a&lt;/code&gt;), but a length that leaves a single stray character is an error:&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="nf"&gt;atob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Y'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// InvalidCharacterError: The string to be decoded is not correctly encoded.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A decoder that adds the padding back before calling &lt;code&gt;atob&lt;/code&gt; accepts both forms. The tool does this, and it also drops a &lt;code&gt;data:...;base64,&lt;/code&gt; prefix and ignores whitespace, because that is how Base64 usually arrives when you copy it from somewhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  The btoa trap with non-ASCII text
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;btoa&lt;/code&gt; takes a "binary string" where every character is one byte. Text outside Latin-1 throws:&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="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'héllo €'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// InvalidCharacterError: Invalid character&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sneaky case is text that is inside Latin-1. &lt;code&gt;btoa('héllo')&lt;/code&gt; does not throw, but it encodes the single byte 0xE9 for the é, not the two UTF-8 bytes. Anything that decodes it as UTF-8 will not see the same text:&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="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'héllo'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                                                  &lt;span class="c1"&gt;// aOlsbG8=&lt;/span&gt;
&lt;span class="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCharCode&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;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TextEncoder&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="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'héllo'&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="c1"&gt;// aMOpbGxv&lt;/span&gt;
&lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'héllo'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'base64'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                        &lt;span class="c1"&gt;// aMOpbGxv&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last two match, and the first does not. Encode to UTF-8 bytes first, then turn the bytes into a binary string, then call &lt;code&gt;btoa&lt;/code&gt;. Decoding goes the other way with &lt;code&gt;TextDecoder&lt;/code&gt;:&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;enc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCharCode&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;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TextEncoder&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="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TextDecoder&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="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;atob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;enc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'héllo €'&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;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charCodeAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&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="nb"&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="nf"&gt;enc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'héllo €'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// aMOpbGxvIOKCrA== héllo €&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Do not spread a big array
&lt;/h2&gt;

&lt;p&gt;The one-liner &lt;code&gt;String.fromCharCode(...bytes)&lt;/code&gt; works for text but not for files. Spreading 200,000 bytes into arguments overflows the stack:&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="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCharCode&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;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// RangeError: Maximum call stack size exceeded&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Build the binary string in chunks instead. The tool uses 0x8000 bytes at a time:&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="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;bin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&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;let&lt;/span&gt; &lt;span class="n"&gt;i&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="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mh"&gt;0x8000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;bin&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCharCode&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;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subarray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mh"&gt;0x8000&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="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A 1 MB array goes through this in one pass and comes out at 1,398,104 characters.&lt;/p&gt;

&lt;h2&gt;
  
  
  URL-safe Base64
&lt;/h2&gt;

&lt;p&gt;Standard Base64 uses &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;/&lt;/code&gt;, which mean something in URLs. The URL-safe variant swaps them for &lt;code&gt;-&lt;/code&gt; and &lt;code&gt;_&lt;/code&gt; and usually drops the padding. The three bytes &lt;code&gt;fb ff fe&lt;/code&gt; show the difference:&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCharCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0xfb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0xff&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0xfe&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&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="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="o"&gt;+/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'-'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="c1"&gt;//g, '_').replace(/=+$/, ''))&lt;/span&gt;
&lt;span class="c1"&gt;// +//+ -__-&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the way back, swap the characters again and re-pad. JWTs use the URL-safe form, which is why pasting one into a plain decoder sometimes fails.&lt;/p&gt;

&lt;h2&gt;
  
  
  Guessing the file type from the text
&lt;/h2&gt;

&lt;p&gt;Because the first bytes of a file are fixed, the first characters of its Base64 are fixed too. A PDF starts with &lt;code&gt;%PDF-&lt;/code&gt;, a PNG with &lt;code&gt;89 50 4e 47 0d 0a 1a 0a&lt;/code&gt;, a JPEG with &lt;code&gt;ff d8 ff&lt;/code&gt;:&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="nb"&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="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCharCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x44&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x46&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x2d&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="c1"&gt;// JVBERi0=&lt;/span&gt;
&lt;span class="nb"&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="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCharCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x89&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x4e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x47&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x0d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x0a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x1a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x0a&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="c1"&gt;// iVBORw0KGgo=&lt;/span&gt;
&lt;span class="nb"&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="nf"&gt;btoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCharCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0xff&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0xd8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0xff&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="c1"&gt;// /9j/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So &lt;code&gt;JVBERi0&lt;/code&gt; means PDF, &lt;code&gt;iVBORw0KGgo&lt;/code&gt; means PNG and &lt;code&gt;/9j/&lt;/code&gt; means JPEG. The tool checks these signatures after decoding. A decoded PDF can be plain ASCII, so it checks the file type before it tries UTF-8 text. If the bytes match a known type, or are not valid UTF-8, you get a download button instead of garbled text.&lt;/p&gt;

&lt;p&gt;To try any of this without writing code, the encoder and decoder is at &lt;a href="https://www.toolsy-hub.com/tools/base64" rel="noopener noreferrer"&gt;https://www.toolsy-hub.com/tools/base64&lt;/a&gt;. It runs in the browser tab, handles UTF-8 text and files, and decodes both standard and URL-safe input.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Is this timestamp in seconds or milliseconds? Guessing the unit from the digit count</title>
      <dc:creator>Toolsy Hub</dc:creator>
      <pubDate>Thu, 12 Nov 2026 03:48:48 +0000</pubDate>
      <link>https://dev.to/toolsyhub/is-this-timestamp-in-seconds-or-milliseconds-guessing-the-unit-from-the-digit-count-3jnc</link>
      <guid>https://dev.to/toolsyhub/is-this-timestamp-in-seconds-or-milliseconds-guessing-the-unit-from-the-digit-count-3jnc</guid>
      <description>&lt;p&gt;You paste 1700000000 into a date converter and get November 2023. You paste 1700000000123 and get the same day, but only if the tool knows the second number is milliseconds. Feed that value to code that expects seconds and you get a year about 53,000 years from now. Feed the first value to &lt;code&gt;new Date()&lt;/code&gt; and you get January 1970.&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="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1700000000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toISOString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// "2023-11-14T22:13:20.000Z"&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1700000000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toISOString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;// "1970-01-20T16:13:20.000Z"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second line is the classic bug: seconds passed where JavaScript wants milliseconds. The date lands 19 days after the epoch, which looks plausible enough to survive a code review.&lt;/p&gt;

&lt;h2&gt;
  
  
  Count the digits
&lt;/h2&gt;

&lt;p&gt;Today a timestamp in seconds has 10 digits, milliseconds has 13, microseconds 16 and nanoseconds 19. That is enough to guess the unit when you cannot ask where the number came from. The converter on Toolsy Hub uses this rule on the whole part of the number:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;up to 11 digits: seconds (this holds until the year 5138)&lt;/li&gt;
&lt;li&gt;12 to 14 digits: milliseconds&lt;/li&gt;
&lt;li&gt;15 to 17 digits: microseconds&lt;/li&gt;
&lt;li&gt;18 or more: nanoseconds&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The cut points sit between the common lengths, so a 12 or 14 digit value still gets a sensible unit. It is a guess, so the tool also lets you pick the unit by hand.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why nanoseconds need BigInt
&lt;/h2&gt;

&lt;p&gt;A nanosecond timestamp for today is around 1.7 x 10^18. JavaScript numbers are 64-bit floats and only hold whole numbers exactly up to 9,007,199,254,740,991. Past that, digits get rounded:&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="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1700000000123456789n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// 1700000000123456800&lt;/span&gt;
&lt;span class="mi"&gt;1700000000123456789n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// "1700000000123456789"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the converter keeps every input as whole nanoseconds in a BigInt and only turns it into a Date at the last step, after dividing down to milliseconds. Microsecond and nanosecond input comes out exact.&lt;/p&gt;

&lt;h2&gt;
  
  
  Negative timestamps round the other way
&lt;/h2&gt;

&lt;p&gt;Dates before 1970 are negative. Truncating division gets them wrong by a second:&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="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1500&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// -1  (rounds toward zero)&lt;/span&gt;
&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1500&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// -2  (rounds toward the past)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-1500 ms is 1.5 seconds before the epoch, which falls inside the second that starts at -2. BigInt division truncates (&lt;code&gt;-1500n / 1000n&lt;/code&gt; is &lt;code&gt;-1n&lt;/code&gt;), so the converter has its own floor division that steps down by one when the remainder is not zero and the value is negative.&lt;/p&gt;

&lt;h2&gt;
  
  
  The range limit
&lt;/h2&gt;

&lt;p&gt;A JavaScript Date stops at 8,640,000,000,000,000 ms either side of 1970. One millisecond past that is an invalid date:&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="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;8.64e15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toISOString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// "+275760-09-13T00:00:00.000Z"&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;8.64e15&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// NaN&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A converter should say "out of range" and not print "Invalid Date". The one on Toolsy Hub does, and it also rejects text that is not a number with its own message.&lt;/p&gt;

&lt;p&gt;If you want to check a value without writing code, the converter is at &lt;a href="https://www.toolsy-hub.com/tools/unix-timestamp-converter" rel="noopener noreferrer"&gt;https://www.toolsy-hub.com/tools/unix-timestamp-converter&lt;/a&gt;. It runs in the browser tab, shows seconds, milliseconds, microseconds, nanoseconds, ISO 8601 and UTC for any input, and converts a pasted list one value per line.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to shrink a PDF to a target size in the browser: render pages, split a byte budget, retry three times</title>
      <dc:creator>Toolsy Hub</dc:creator>
      <pubDate>Thu, 05 Nov 2026 09:36:33 +0000</pubDate>
      <link>https://dev.to/toolsyhub/how-to-shrink-a-pdf-to-a-target-size-in-the-browser-render-pages-split-a-byte-budget-retry-three-2l28</link>
      <guid>https://dev.to/toolsyhub/how-to-shrink-a-pdf-to-a-target-size-in-the-browser-render-pages-split-a-byte-budget-retry-three-2l28</guid>
      <description>&lt;p&gt;Court portals, job sites and university forms often cap uploads at a number like 200 KB or 2 MB. Most PDF compressors give you a quality slider and leave you to guess. If you know the target, you can make the code hit it.&lt;/p&gt;

&lt;p&gt;Here is how the Compress PDF tool on Toolsy Hub does it, all in the browser with pdf-lib and pdf.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two modes, because they trade different things
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Light&lt;/strong&gt; re-saves the file with pdf-lib and object streams turned on. Text stays selectable and nothing is re-encoded, so the saving is small. If the PDF has pictures that are the real weight, light mode will not help.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Images&lt;/strong&gt; renders every page to a JPEG with pdf.js and rebuilds the PDF from those pictures. A scanned document shrinks a lot. The cost is that text becomes part of the picture, so it is no longer selectable. The tool says this before you pick it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Render once at the sharpest setting
&lt;/h2&gt;

&lt;p&gt;There are three presets: 150 dpi at JPEG quality 0.8, 120 dpi at 0.65, and 96 dpi at 0.5. When you type a target size, the tool ignores the preset and starts every page at the sharpest one, 150 dpi at 0.8. It lowers quality only as far as the target needs. Starting low and going up would leave a sharp document blurry for no reason.&lt;/p&gt;

&lt;h2&gt;
  
  
  Split the byte budget by pixel area
&lt;/h2&gt;

&lt;p&gt;If the first PDF is already under the target, you are done. If not, the target in bytes is divided across the pages in proportion to each page's pixel area (width times height). A big page gets a bigger share than a small one, so one large scan does not eat the whole budget.&lt;/p&gt;

&lt;p&gt;Each page image is then recompressed to its share with a quality search, and the PDF is rebuilt.&lt;/p&gt;

&lt;h2&gt;
  
  
  Retry, but only three times
&lt;/h2&gt;

&lt;p&gt;Page shares add up, but PDF structure adds overhead on top of the images, so the rebuilt file can land a little over. The tool then tightens the budget by &lt;code&gt;target / size * 0.95&lt;/code&gt;, never by more than half in one step, and tries again. Three attempts at most. If it still misses, it hands back the smallest result and says the target was not reached, rather than looping or silently returning a file that breaks your limit.&lt;/p&gt;

&lt;p&gt;The "say so" part matters. A file that is 30 KB over a 200 KB limit gets rejected by the portal, and the person finds out only after the upload fails.&lt;/p&gt;

&lt;h2&gt;
  
  
  What stays in the browser
&lt;/h2&gt;

&lt;p&gt;Nothing is sent to a server for this. pdf-lib is loaded on demand (about 180 KB) so the page itself stays light. Files you add stay in your browser; Google Analytics counts visits to the site.&lt;/p&gt;

&lt;p&gt;Try it: &lt;a href="https://www.toolsy-hub.com/tools/compress-pdf" rel="noopener noreferrer"&gt;https://www.toolsy-hub.com/tools/compress-pdf&lt;/a&gt;. For a fixed limit there is a page with the size already set: &lt;a href="https://www.toolsy-hub.com/tools/compress-pdf/200kb" rel="noopener noreferrer"&gt;https://www.toolsy-hub.com/tools/compress-pdf/200kb&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What I would improve: the retry factor is a heuristic. A proper search over per-page quality would hit the target more closely on files with mixed pages. If you have a PDF that misses its target, I would like to hear about it.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>pdf</category>
    </item>
    <item>
      <title>How to compress an image to an exact KB limit in the browser (canvas, binary search, and when to stop lowering quality)</title>
      <dc:creator>Toolsy Hub</dc:creator>
      <pubDate>Thu, 29 Oct 2026 02:52:26 +0000</pubDate>
      <link>https://dev.to/toolsyhub/how-to-compress-an-image-to-an-exact-kb-limit-in-the-browser-canvas-binary-search-and-when-to-2d7i</link>
      <guid>https://dev.to/toolsyhub/how-to-compress-an-image-to-an-exact-kb-limit-in-the-browser-canvas-binary-search-and-when-to-2d7i</guid>
      <description>&lt;p&gt;Upload forms for visas, exams and job portals often say "photo must be under 100 KB". A phone photo is 3 to 5 MB, and the usual advice is to drag a quality slider until the number looks right. You can automate that in the browser with the canvas API, and the part that matters is knowing when to stop lowering quality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Encode at a quality and read the size&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;canvas.toBlob(callback, "image/jpeg", quality)&lt;/code&gt; gives you a Blob. &lt;code&gt;blob.size&lt;/code&gt; is the file size in bytes. That is all you need to measure.&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quality&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;canvas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;canvas&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt;
  &lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2d&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fillStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#fff&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;// JPEG has no alpha; transparent PNGs turn black without this&lt;/span&gt;
  &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillRect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&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;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;imageSmoothingQuality&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;high&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drawImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&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;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toBlob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;image/jpeg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quality&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;Load the bitmap with &lt;code&gt;createImageBitmap(file, { imageOrientation: "from-image" })&lt;/code&gt;, otherwise phone photos come out sideways because the EXIF rotation is ignored.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Binary search on quality&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;File size grows with quality, so you can search for the highest quality that still fits:&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;low&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;high&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.95&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;best&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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="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;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;6&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;low&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;high&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;q&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;blob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;targetBytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;best&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;low&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;high&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;q&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;Six rounds narrows the range to about 1% of quality, which is more precision than the file size needs. Count 1 KB as 1000 bytes, not 1024, so the file passes whether the form counts KB one way or the other.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Know when to stop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is the part the sliders do not tell you. Below roughly 30% JPEG quality a photo breaks into visible blocks and the colours shift. A file that is technically under the limit but looks like that gets rejected by a human reviewer or looks bad on an ID. So the search never goes below 0.3. First it encodes once at 0.3 for the current size. If that is still over the limit, it makes the picture smaller instead: width and height times 0.8, and try again. It tries up to 12 sizes, which is down to about a tenth of the original width. Only at the last size does it allow quality all the way down to 0.05, because a file that fits beats no file.&lt;/p&gt;

&lt;p&gt;PNG has no quality setting, so for PNG only the dimensions change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I would do differently&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Offer a "keep exact width and height" switch for forms that demand fixed pixels, and then accept a lower quality or a missed target. The tool I built has one, and it tells the user when the target was missed.&lt;/li&gt;
&lt;li&gt;Test the search with a fake encoder. The function takes the encoder as a parameter, so unit tests can pass a function that returns a Blob of a size you choose, with no canvas involved.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The tool built on this, with nothing uploaded anywhere:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Image compressor with a target size: &lt;a href="https://www.toolsy-hub.com/tools/image-compressor" rel="noopener noreferrer"&gt;https://www.toolsy-hub.com/tools/image-compressor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Photo resizer for exact KB and pixel presets: &lt;a href="https://www.toolsy-hub.com/tools/photo-signature-resizer" rel="noopener noreferrer"&gt;https://www.toolsy-hub.com/tools/photo-signature-resizer&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Google Analytics counts visits on the site; image files are never sent anywhere.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>performance</category>
    </item>
    <item>
      <title>Building PDF tools that never upload your file: pdf-lib, pdf.js and qpdf in WebAssembly</title>
      <dc:creator>Toolsy Hub</dc:creator>
      <pubDate>Thu, 22 Oct 2026 14:16:17 +0000</pubDate>
      <link>https://dev.to/toolsyhub/building-pdf-tools-that-never-upload-your-file-pdf-lib-pdfjs-and-qpdf-in-webassembly-16eh</link>
      <guid>https://dev.to/toolsyhub/building-pdf-tools-that-never-upload-your-file-pdf-lib-pdfjs-and-qpdf-in-webassembly-16eh</guid>
      <description>&lt;p&gt;Most "free PDF tools" upload your file to a server, process it there and send it back. For a bank statement or a CNIC scan that is a lot of trust to hand a site you found a minute ago. I wanted merge, split, compress and unlock to work with no upload at all, so I built them to run entirely in the browser. This post is what that took, and where it falls short.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three engines
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;pdf-lib&lt;/strong&gt; (1.17) reads and writes the PDF structure: merging, splitting, picking pages, building a PDF from JPGs. It is plain JavaScript and works the same in every modern browser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pdf.js&lt;/strong&gt; (Mozilla's renderer) draws a page onto a canvas. I use it to turn pages into images for "PDF to JPG" and for the stronger compress mode.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;qpdf&lt;/strong&gt;, compiled to WebAssembly (about 1.3 MB), removes the password and restrictions from a PDF when the user types the password they already know. pdf-lib cannot decrypt, so this is the one job that needed a native tool.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of them load with the page. Each is imported inside the click handler with &lt;code&gt;await import(...)&lt;/code&gt;, so someone who only wants the word counter never downloads a PDF engine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caching the engines for a year
&lt;/h2&gt;

&lt;p&gt;The WASM and worker files are copied at build time into a folder named after the package version, for example &lt;code&gt;/engines/qpdf-&amp;lt;version&amp;gt;/&lt;/code&gt;, and served with &lt;code&gt;Cache-Control: public, max-age=31536000, immutable&lt;/code&gt;. A new version gets a new folder, so a browser never mixes an old worker with new code, and a returning visitor does not download 1-2 MB again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compress: two modes, and why
&lt;/h2&gt;

&lt;p&gt;There is no magic "compress PDF" call. What shrinks a PDF depends on what is inside it.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Light mode&lt;/strong&gt; re-saves the file with pdf-lib using compressed object streams. Text stays selectable. The saving is small, often a few percent, because the pictures inside are untouched.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Image mode&lt;/strong&gt; renders every page with pdf.js to a JPEG (150 dpi at quality 0.8 for "high", down to 96 dpi at 0.5 for "low") and builds a new PDF from those images. For scanned documents, which are pictures anyway, this is where the big savings are. The cost: text becomes part of the picture and is no longer selectable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The tool says which mode does what before you click, rather than after.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hitting an exact size
&lt;/h2&gt;

&lt;p&gt;Upload forms in Pakistan ask for "PDF under 200 KB" or "under 100 KB", so a slider is not enough. With a target set, the pages are rendered at the sharpest level first, and the packer lowers JPEG quality only as far as it has to until the whole PDF fits. If it cannot get there, it says so instead of quietly handing back a file that is still too big.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does not work well
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Very large files on phones.&lt;/strong&gt; Rendering 60 pages at 150 dpi in a phone browser uses a lot of memory. Server tools win here.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Image mode on text PDFs.&lt;/strong&gt; A text-only PDF can come out bigger as images. Light mode is the right choice there. Image mode is still the default, because most files people bring to a "PDF under 200 KB" form are scans.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Password cracking.&lt;/strong&gt; The unlock tool opens a file with the password you type. It does not guess passwords, on purpose.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;The tools are free with no sign-up: &lt;a href="https://www.toolsy-hub.com/tools/compress-pdf" rel="noopener noreferrer"&gt;Compress PDF&lt;/a&gt;, &lt;a href="https://www.toolsy-hub.com/tools/merge-pdf" rel="noopener noreferrer"&gt;Merge PDF&lt;/a&gt;, &lt;a href="https://www.toolsy-hub.com/tools/unlock-pdf" rel="noopener noreferrer"&gt;Unlock PDF&lt;/a&gt;. Open the network tab while you use one and you will see no request carrying your file.&lt;/p&gt;

&lt;p&gt;If you have built something similar, I would like to hear how you dealt with memory on phones.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>pdf</category>
      <category>webassembly</category>
    </item>
  </channel>
</rss>
