{"id":64621,"date":"2025-05-12T10:44:45","date_gmt":"2025-05-12T07:44:45","guid":{"rendered":"https:\/\/wpcalc.com\/en\/?p=64621"},"modified":"2026-09-28T10:11:46","modified_gmt":"2026-09-28T07:11:46","slug":"javascript-compress","status":"publish","type":"post","link":"https:\/\/wpcalc.com\/en\/web-tools\/javascript-compress\/","title":{"rendered":"JavaScript Compress"},"content":{"rendered":"<section class=\"not-prose calculator-box\" aria-labelledby=\"calculator-title\"><div class=\"flex items-baseline sm:items-center justify-between gap-2 sm:gap-3\"><div class=\"flex flex-col sm:flex-row sm:items-center gap-2\"><span class=\"icon icon-calculator text-primary-700! text-base! dark:text-primary-300!\" aria-hidden=\"true\"><\/span><h2 id=\"calculator-title\" class=\"text-lg font-display font-bold\">JavaScript Minifier Tool<\/h2><\/div><div class=\"calcdash-favorite group\">\n  <button type=\"button\" class=\"favorite\" id=\"favorite\" data-favorite-id=\"64621\" data-favorite-title=\"JavaScript Compress\" data-favorite-url=\"https:\/\/wpcalc.com\/en\/web-tools\/javascript-compress\/\" data-favorite-excerpt=\"Paste JavaScript into the field to remove unnecessary characters. Copy the compressed code and compare the displayed before and after sizes.\" aria-label=\"Add to Favorites\" data-favorite-icon=\"icon icon-conversion\">\n    <span class=\"icon icon-shape-star-empty\" aria-hidden=\"true\"><\/span>\n  <\/button>\n  <div class=\"calcdash-favorite__tip\" aria-hidden=\"true\">\n   <span class=\"favorite-tooltip\" id=\"favorite-tooltip\"><\/span>\n  <\/div>\n  <span class=\"sr-only\" role=\"status\" id=\"favorite-status\"><\/span>\n<\/div><\/div><form action=\"https:\/\/wpcalc.com\/en\/web-tools\/javascript-compress\/\" method=\"POST\" class=\"calculator\" id=\"calculator-64621\" data-post=\"64621\"><fieldset class=\"fieldset-input is-1\"><legend class=\"sr-only\">Input Fields<\/legend><div class=\"field is-single\" id=\"input-1\"><label for=\"field-1\">JavaScript Code to Compress<\/label><textarea id=\"field-1\" name=\"javascript_code_to_compress\">function test() {\n  console.log(&quot;hello&quot;);\n}<\/textarea><\/div><\/fieldset><div class=\"buttons\"><button type=\"submit\" data-text=\"Re-Calculate\" id=\"calculate-button\" data-post=\"64621\">Calculate<\/button><button type=\"reset\">Reset<\/button><\/div><div class=\"field is-checkbox hidden!\" id=\"field-auto-calc\"><input type=\"checkbox\" id=\"auto-calc\"><label for=\"auto-calc\">Calculate automatically<\/label><small>If enabled, the result will update automatically when you change any value.<\/small><\/div><div class=\"fieldset-result is-hidden\" aria-labelledby=\"result-title\" aria-live=\"polite\" role=\"region\"> <h3 class=\"result-title bg-gradient-to-r from-primary-50 to-gray-50 dark:from-primary-900 dark:to-gray-800\" id=\"result-title\"><span class=\"icon icon-s-pulse\" aria-hidden=\"true\"><\/span> Your Results<\/h3><div class=\"result-box\"><div class=\"field-result is-column\" id=\"output-1\"><span class=\"field-title\"><span>Compressed Code<\/span><\/span><span class=\"field-value\" id=\"result-1\"><\/span><button type=\"button\" class=\"copy-result\" data-tooltip=\"Copy Result\" aria-label=\"Copy Result\"><span class=\"copy-icon icon icon-document-copy\" aria-hidden=\"true\"><\/span><\/button><\/div><div class=\"field-result is-column\" id=\"output-2\"><span class=\"field-value\" id=\"result-2\"><\/span><button type=\"button\" class=\"copy-result\" data-tooltip=\"Copy Result\" aria-label=\"Copy Result\"><span class=\"copy-icon icon icon-document-copy\" aria-hidden=\"true\"><\/span><\/button><\/div><\/div><div class=\"result-actions\"><button type=\"button\" class=\"copy-link\"><svg viewBox=\"0 0 20 20\" fill=\"currentColor\" aria-hidden=\"true\"><path d=\"M12.232 4.232a2.5 2.5 0 0 1 3.536 3.536l-1.225 1.224a.75.75 0 0 0 1.061 1.06l1.224-1.224a4 4 0 0 0-5.656-5.656l-3 3a4 4 0 0 0 .225 5.865.75.75 0 0 0 .977-1.138 2.5 2.5 0 0 1-.142-3.667l3-3Z\"\/><path d=\"M11.603 7.963a.75.75 0 0 0-.977 1.138 2.5 2.5 0 0 1 .142 3.667l-3 3a2.5 2.5 0 0 1-3.536-3.536l1.225-1.224a.75.75 0 0 0-1.061-1.06l-1.224 1.224a4 4 0 1 0 5.656 5.656l3-3a4 4 0 0 0-.225-5.865Z\"\/><\/svg><span class=\"copy-link__text\">Copy link to this calculation<\/span><\/button><span class=\"sr-only\" role=\"status\" id=\"copy-link-status\"><\/span><\/div><\/div><a href=\"#respond\" class=\"hidden transition-opacity duration-300 opacity-0 w-50 text-sm justify-center items-center gap-2 px-4 py-2 rounded bg-gray-200 text-gray-700 hover:bg-gray-300\" id=\"leave-comment\"><span class=\"icon icon-comments\" aria-hidden=\"true\"><\/span>Leave a Comment<\/a><\/form><\/section><section id=\"calc-reactions\" class=\"not-prose hidden my-12 bg-gradient-to-r from-primary-50 to-gray-50 border border-indigo-100 rounded-xl px-6 py-4 shadow-sm\" data-post=\"64621\" aria-live=\"polite\" aria-labelledby=\"calc-reactions-title\"><p id=\"calc-reactions-title\" class=\"text-sm text-gray-500 text-center mb-2\">How did this result make you feel?<\/p><div role=\"group\" aria-labelledby=\"calc-reactions-title\" class=\"grid grid-cols-3 sm:flex sm:flex-row sm:justify-around gap-4 sm:items-center sm:flex-wrap\"><button type=\"button\" aria-pressed=\"false\" class=\"reaction-btn flex flex-col items-center gap-1 text-sm text-gray-700 hover:text-primary-600 cursor-pointer transition-transform hover:scale-105\" data-reaction=\"like\"><span class=\"reaction-count\" data-reaction-count=\"like\">0<\/span><span class=\"reaction text-3xl\" aria-hidden=\"true\">\ud83d\ude00 <\/span><span class=\"reaction-description font-medium\">Like <\/span><\/button><button type=\"button\" aria-pressed=\"false\" class=\"reaction-btn flex flex-col items-center gap-1 text-sm text-gray-700 hover:text-primary-600 cursor-pointer transition-transform hover:scale-105\" data-reaction=\"helpful\"><span class=\"reaction-count\" data-reaction-count=\"helpful\">0<\/span><span class=\"reaction text-3xl\" aria-hidden=\"true\">\ud83d\udca1 <\/span><span class=\"reaction-description font-medium\">Helpful <\/span><\/button><button type=\"button\" aria-pressed=\"false\" class=\"reaction-btn flex flex-col items-center gap-1 text-sm text-gray-700 hover:text-primary-600 cursor-pointer transition-transform hover:scale-105\" data-reaction=\"confused\"><span class=\"reaction-count\" data-reaction-count=\"confused\">0<\/span><span class=\"reaction text-3xl\" aria-hidden=\"true\">\ud83d\ude15 <\/span><span class=\"reaction-description font-medium\">Confused <\/span><\/button><button type=\"button\" aria-pressed=\"false\" class=\"reaction-btn flex flex-col items-center gap-1 text-sm text-gray-700 hover:text-primary-600 cursor-pointer transition-transform hover:scale-105\" data-reaction=\"disappointed\"><span class=\"reaction-count\" data-reaction-count=\"disappointed\">0<\/span><span class=\"reaction text-3xl\" aria-hidden=\"true\">\ud83d\ude1e <\/span><span class=\"reaction-description font-medium\">Disappointed <\/span><\/button><button type=\"button\" aria-pressed=\"false\" class=\"reaction-btn flex flex-col items-center gap-1 text-sm text-gray-700 hover:text-primary-600 cursor-pointer transition-transform hover:scale-105\" data-reaction=\"inaccurate\"><span class=\"reaction-count\" data-reaction-count=\"inaccurate\">1<\/span><span class=\"reaction text-3xl\" aria-hidden=\"true\">\u274c <\/span><span class=\"reaction-description font-medium\">Inaccurate <\/span><\/button><\/div><div id=\"reaction-message\" class=\"hidden mt-8 rounded-md border border-primary-100 bg-white\/50 backdrop-blur-sm px-4 py-2 text-sm text-center shadow-sm\"><\/div><\/section><div class=\"calcdash-ad not-prose calcdash-ad--calc_result calcdash-ad--in-article\"><ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"ca-pub-1721569815777345\" data-ad-slot=\"6317458308\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\"><\/ins><script>(adsbygoogle = window.adsbygoogle || []).push({});<\/script><\/div>\n<style id=\"wpcoderpro-inline-1\">.formula-box {\r\n\tborder-bottom: none !important;\r\n}<\/style><section class=\"formula-box\" aria-labelledby=\"formula-title\">\r\n\t<div id=\"formula-title\" class=\"not-prose font-display mb-4\"><span class=\"icon icon-formula\" aria-hidden=\"true\"><\/span><h2>JavaScript Compress Formula<\/h2><\/div>\r\n\t<figure class=\"not-prose formula\">\r\n\t\t<figcaption class=\"formula-title\">Formula<\/figcaption>\r\n\t\t<div class=\"text-base text-gray-800\" id=\"formula\">\n\\[d=\\begin{cases}L_{\\mathrm{in}}+N_{\\mathrm{LF}},&amp;\\text{if the input has no carriage returns}\\\\L_{\\mathrm{in}},&amp;\\text{otherwise}\\end{cases}\\qquad R=\\frac{\\operatorname{round}\\!\\left(1000\\frac{d-L_{\\mathrm{out}}}{d}\\right)}{10}\\%\\]\n<\/div>\r\n\t<\/figure>\r\n\t<\/p>\n<p><strong>Where:<\/strong><\/p>\n<ul>\n<li>\\(L_{\\mathrm{in}}\\) \u2014 the input string length as measured by JavaScript.<\/li>\n<li>\\(N_{\\mathrm{LF}}\\) \u2014 the number of line-feed characters in the input.<\/li>\n<li>\\(L_{\\mathrm{out}}\\) \u2014 the compressed code\u2019s string length.<\/li>\n<li>\\(d\\) \u2014 the value shown as \u201cBefore\u201d; \\(R\\) \u2014 the displayed compression ratio.<\/li>\n<\/ul>\n<p>The tool compresses the input with a JavaScript packer, then compares the result\u2019s length with its adjusted input length. A positive ratio means the output is shorter by this measure.<\/p>\n<p>\r\n<\/section>\n<style id=\"wpcoderpro-inline-459\">.calculation-box {\r\n\tborder-bottom: none !important;\r\n}<\/style><section id=\"calculation\" class=\"calculation-box\" aria-labelledby=\"calculation-title\">\r\n\t<div id=\"calculation-title\" class=\"not-prose font-display\"><span class=\"icon icon-unordered-list\" aria-hidden=\"true\"><\/span><h2>JavaScript Compress \u2013 Calculation Example<\/h2><\/div>\r\n\r\n<\/p>\n<p><strong>Example:<\/strong> use the default function, which logs \u201chello.\u201d Its source has 43 characters, including two line feeds. The compressed result is <code>function test(){console.log(\"hello\")}<\/code>, with a length of 37 characters.<\/p>\n<p>\\[d=43+2=45\\qquad R=\\frac{\\operatorname{round}\\!\\left(1000\\frac{45-37}{45}\\right)}{10}\\%=17.8\\%\\]<\/p>\n<p><strong>Result:<\/strong> Before: 45B; After: 37B; Compression ratio: 17.8%. The \u201cBefore\u201d value is greater than the source\u2019s 43-character string length because this tool adds one count for each line feed when the input has no carriage returns.<\/p>\n<p>\n\r\n<\/section>\n<h2>How to use the calculator<\/h2>\n<ol>\n<li>Paste or type your source into <strong>JavaScript Code to Compress<\/strong>. The field starts with a short sample function that you can replace.<\/li>\n<li>Run the calculator to generate <strong>Compressed Code<\/strong>. An empty field produces an \u201cEnter code\u201d message rather than a result.<\/li>\n<li>Review the output and the <strong>Before<\/strong>, <strong>After<\/strong>, and <strong>Compression ratio<\/strong> figures. Use the copy control to take the compressed code into your project.<\/li>\n<li>Test the copied code in its intended environment before deploying it.<\/li>\n<\/ol>\n<h2>What JavaScript compression means here<\/h2>\n<p>This tool makes JavaScript source more compact by running it through a packer with its additional encoding and variable-shrinking options turned off. The goal is to remove characters that are unnecessary to the code\u2019s syntax, such as eligible comments and whitespace. The result is usually harder to read than the original, so keep an editable copy of your source.<\/p>\n<p>A smaller source string can reduce the amount of JavaScript transferred to a browser, but the displayed ratio is not a prediction of page-load improvement. Servers may apply gzip or Brotli, which can change the difference in transferred bytes. Download time also depends on caching, network conditions, and the rest of the page. For a closer look at file or text length, compare the result with the <a href=\"https:\/\/wpcalc.com\/en\/web-tools\/text-size\/\">Text Size calculator<\/a>.<\/p>\n<p>Minification is different from obfuscation. Compact code is still JavaScript source and may be relatively easy to inspect. If you are comparing these approaches, the <a href=\"https:\/\/wpcalc.com\/en\/web-tools\/javascript-obfuscator-eval\/\">JavaScript Obfuscator (eval)<\/a> produces a different kind of output; the <a href=\"https:\/\/wpcalc.com\/en\/web-tools\/javascript-eval-decode\/\">JavaScript Eval Decode<\/a> tool is intended for examining eval-packed code, not for reversing ordinary minification.<\/p>\n<h2>Assumptions and practical limitations<\/h2>\n<p>The size labels say \u201cB,\u201d but the calculation uses JavaScript string lengths, not the number of bytes in a saved UTF-8 file. JavaScript counts UTF-16 code units, so non-ASCII characters can make the reported values differ from actual file sizes. The tool also adds one count per line feed to the input length when there are no carriage returns. That adjustment affects the reported ratio; it does not add characters to the compressed code.<\/p>\n<p>Compression may save very little on code that is already compact, and a negative ratio is possible if the output is longer under the tool\u2019s measurement. Do not treat a positive ratio as proof that behavior is unchanged. JavaScript syntax can be sensitive to line breaks and to particular comments, and older packers may not handle every modern language feature. Test the result, especially when your code uses recent syntax or relies on comments being preserved.<\/p>\n<p>For production work, retain the readable source and validate the compressed version with your usual tests and browser targets. Compare the actual served file sizes if transfer size matters. If you need to check a source string\u2019s character count separately, use the <a href=\"https:\/\/wpcalc.com\/en\/web-tools\/character-counter\/\">Character Counter<\/a>; its count need not match this tool\u2019s adjusted \u201cBefore\u201d figure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Paste JavaScript into the field to remove unnecessary characters. Copy the compressed code and compare the displayed before and after sizes.<\/p>\n","protected":false},"author":70,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[63],"tags":[64],"class_list":["post-64621","post","type-post","status-publish","format-standard","hentry","category-web-tools","tag-converter-tools"],"acf":[],"_links":{"self":[{"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/posts\/64621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/users\/70"}],"replies":[{"embeddable":true,"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/comments?post=64621"}],"version-history":[{"count":3,"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/posts\/64621\/revisions"}],"predecessor-version":[{"id":65337,"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/posts\/64621\/revisions\/65337"}],"wp:attachment":[{"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/media?parent=64621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/categories?post=64621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/tags?post=64621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}