Browse Source

Clarify GZIP

Arjun Barrett 4 years ago
parent
commit
3ce133c424
2 changed files with 31 additions and 2 deletions
  1. 1 1
      README.md
  2. 30 1
      demo/components/code-box/index.tsx

+ 1 - 1
README.md

@@ -38,7 +38,7 @@ import * as fflate from 'fflate';
 // However, you should import ONLY what you need to minimize bloat.
 // However, you should import ONLY what you need to minimize bloat.
 // So, if you just need GZIP compression support:
 // So, if you just need GZIP compression support:
 import { gzipSync } from 'fflate';
 import { gzipSync } from 'fflate';
-// Woo! You just saved 20 kB off your bundle with one line.
+// Woo! You just saved 15 kB off your bundle with one line.
 ```
 ```
 
 
 If your environment doesn't support ES Modules (e.g. Node.js):
 If your environment doesn't support ES Modules (e.g. Node.js):

+ 30 - 1
demo/components/code-box/index.tsx

@@ -21,6 +21,14 @@ const presets: Record<string, Preset> = {
   'Basic GZIP compression': {
   'Basic GZIP compression': {
     fflate: `var left = files.length;
     fflate: `var left = files.length;
 var filesLengths = {};
 var filesLengths = {};
+
+// In a real app, use a list of file types to avoid compressing for better
+// performance
+var ALREADY_COMPRESSED = [
+  'zip', 'gz', 'png', 'jpg', 'jpeg', 'pdf', 'doc', 'docx', 'ppt', 'pptx',
+  'xls', 'xlsx', 'heic', 'heif', '7z', 'bz2', 'rar', 'gif', 'webp', 'webm'
+];
+
 // This function binds the variable "file" to the local scope, which makes
 // This function binds the variable "file" to the local scope, which makes
 // parallel processing possible.
 // parallel processing possible.
 // If you use ES6, you can declare variables with "let" to automatically bind
 // If you use ES6, you can declare variables with "let" to automatically bind
@@ -29,12 +37,33 @@ var processFile = function(i) {
   var file = files[i];
   var file = files[i];
   fileToU8(file, function(buf) {
   fileToU8(file, function(buf) {
     fflate.gzip(buf, {
     fflate.gzip(buf, {
+
+      // In a real app, instead of always compressing at a certain level,
+      // you'll want to check if the file is already compressed. For fairness,
+      // that's not done here.
+
+      /*
+      level: ALREADY_COMPRESSED.indexOf(
+        file.name.slice(file.name.lastIndexOf('.') + 1)
+      ) == -1 ? 6 : 0
+      */
+
       level: 6,
       level: 6,
 
 
-      // These are optional, but fflate supports the metadata
+      // You can uncomment the below for a contest of pure algorithm speed.
+      // In a real app, you'll probably not need to set the memory level
+      // because fflate picks a reasonable level based on file size by default.
+      // If fflate performs worse than UZIP, you're probably passing in 
+      // incompressible files; switching the level or the mem will fix it.
+      
+      /*
+      mem: 4
+      */
 
 
+      // The following are optional, but fflate supports metadata if you want
       mtime: file.lastModified,
       mtime: file.lastModified,
       filename: file.name
       filename: file.name
+
     }, function(err, data) {
     }, function(err, data) {
       if (err) callback(err);
       if (err) callback(err);
       else {
       else {