Giter Site home page Giter Site logo

Exposing Options about gzip-size HOT 4 CLOSED

sindresorhus avatar sindresorhus commented on July 22, 2024
Exposing Options

from gzip-size.

Comments (4)

sindresorhus avatar sindresorhus commented on July 22, 2024

I made this to be able to quickly approximate how much gain I would get from gzipping. Not super interested in adding options to it. Would it make sense to default to -9? What level do browsers use?

from gzip-size.

srcspider avatar srcspider commented on July 22, 2024

It's a server option; I believe browsers support all levels. I don't ever recall anything about browsers asking for a specific level. In general unless I'm mistaken the recommended setting for production is to have -9 or level 9 (ie. best possible compression). Default however is for servers like nginx is level 6 (ie. middle of the road); but default for worker processes is also something like 2-4 instead of auto, so it's probably just sensible options for dev boxes.

Reason for having maximum compression, even though the difference is very small a lot of the time, is that processing power is much cheaper then bandwidth.

I think defaulting to 9 is better then defaulting to 6 since it would then show "just how good it can get" instead of what you would get on non-production settings.

Incidentally, setting that seems to be a bit more troublesome then it would first appear. I've actually tried just adding the options to your code snippet (which according to the node docs should work) but it borks and I'm not sure why.

To get it to work, for my purposes (printing a table of files), I've had to write something like this,

var gulp = require('gulp');
var prettyBytes = require('pretty-bytes');
var zlib = require('zlib');
var glob = require('glob');
var fs = require('fs');
var c = require('chalk');
var _ = require('shadow.lodash');
var Table = require('cli-table');
// ----------------------------------------------------------------------------

var pathRoot = './public/web/';

gulp.task('gzip:info', function (resolve) {

    var table = new Table({
        head: [
            c.reset.bold.underline('file'),
            c.reset.bold.underline('raw'),
            c.reset.bold.underline('gzip')
        ]
    });

    console.log('');

    glob(pathRoot + '*.{js,css}', function (err, files) {

        var pending = files.length;
        var checkDone = function () {
            if (pending == 0) {
                console.log(table.toString());
                console.log('');
                resolve();
            }
        }

        _.each(files.sort(), function (filename) {

            var options = {
                level: 9
            };

            // create a new gzip object
            var gzip = zlib.createGzip(options), buffers=[], nread=0;

            gzip.on('error', function(err) {
                gzip.removeAllListeners();
                gzip=null;
                pending -= 1;
                checkDone();
            });

            gzip.on('data', function(chunk) {
                buffers.push(chunk);
                nread += chunk.length;
            });

            gzip.on('end', function() {
                var buffer;
                switch (buffers.length) {
                    case 0: // no data.  return empty buffer
                        buffer = new Buffer(0);
                        break;
                    case 1: // only one chunk of data.  return it.
                        buffer = buffers[0];
                        break;
                    default: // concatenate the chunks of data into a single buffer.
                        buffer = new Buffer(nread);
                        var n = 0;
                        buffers.forEach(function(b) {
                            var l = b.length;
                            b.copy(buffer, n, 0, l);
                            n += l;
                        });
                        break;
                }

                gzip.removeAllListeners();
                gzip=null;

                var data = buffer.toString();

                table.push([ 
                    filename.replace(pathRoot, ''),
                    c.red(prettyBytes(file.length)),
                    c.yellow(prettyBytes(data.length)) 
                ]);

                pending -= 1;
                checkDone();
            });

            var file = fs.readFileSync(filename);
            gzip.write(file);
            gzip.end();
        })

        checkDone();
    });

});

Which (gulp boilerplating aside) is a bit more verbose then I would like.

ps. not sure if you're using the sync version from browserify for special reasons but there is a sync version in the normal zlib

from gzip-size.

sindresorhus avatar sindresorhus commented on July 22, 2024

Fixed in 2.0.0 => 4483839

ps. not sure if you're using the sync version from browserify for special reasons but there is a sync version in the normal zlib

The sync method was introduced in node 0.12. This module still supports 0.10.

from gzip-size.

sindresorhus avatar sindresorhus commented on July 22, 2024

Actually. I just dropped node 0.10 compat. Not worth the trouble.

from gzip-size.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.