analyze-css

CSS selectors complexity and performance analyzer

npm install analyze-css
45 downloads in the last day
527 downloads in the last week
1 500 downloads in the last month

analyze-css

NPM version Build Status Bitdeli Badge

CSS selectors complexity and performance analyzer. analyze-css is built as a set of rules bound to events fired by CSS parser. Each rule can generate metrics and add "offenders" with more detailed information (see Usage section for an example).

Install

analyze-css comes as a "binary" for command-line and as CommonJS module. Run the following to install them globally:

npm install --global analyze-css

Usage

Command line tool

You can use analyze-css "binary" to analyze local CSS files or remote CSS assets:

analyze-css --file examples/elecena.css
analyze-css --url http://jigsaw.w3.org/css-validator/style/base.css

You can provide CSS via stdin as well (notice the dash: -):

echo ".foo {margin: 0 \!important}" | analyze-css -

This will emit JSON formatted results on stdout. Use --pretty (or -p shortcut) option to make the output readable for human beings.

CommonJS module

var analyzer = require('analyze-css');

new analyzer('.foo {margin: 0 !important}', function(err, results) {
  console.error(err);
  console.log(results); // example? see below
});

Results

{
  "generator": "analyze-css v0.2.0",
  "metrics": {
    "base64Length": 9308,
    "redundantBodySelectors": 0,
    "comments": 1,
    "commentsLength": 68,
    "complexSelectors": 32,
    "duplicatedSelectors": 7,
    "emptyRules": 0,
    "expressions": 0,
    "oldIEFixes": 51,
    "importants": 3,
    "mediaQueries": 0,
    "oldPropertyPrefixes": 65,
    "qualifiedSelectors": 28,
    "specificityIdAvg": 0.05,
    "specificityIdTotal": 35,
    "specificityClassAvg": 1.25,
    "specificityClassTotal": 872,
    "specificityTagAvg": 0.78,
    "specificityTagTotal": 548,
    "selectorsByAttribute": 93,
    "selectorsByClass": 568,
    "selectorsById": 35,
    "selectorsByPseudo": 166,
    "selectorsByTag": 519,
    "universalSelectors": 4,
    "length": 51665,
    "rules": 422,
    "selectors": 699,
    "declarations": 1240
  },
  "offenders": {
    "importants": [
      ".foo {margin: 0 !important}"
    ]
  }
}

Metrics

  • base64Length: total length of base64-encoded data in CSS source (will warn about base64-encoded data bigger than 4 kB)
  • redundantBodySelectors: number of redundant body selectors (e.g. body .foo, section body h2, but not body > h1)
  • comments: number of comments in CSS source
  • commentsLength: length of comments content in CSS source
  • complexSelectors: number of complex selectors (consisting of more than three expressions, e.g. header ul li .foo)
  • duplicatedSelectors: number of CSS selectors defined more than once in CSS source
  • emptyRules: number of rules with no properties (e.g. .foo { })
  • expressions: number of rules with CSS expressions (e.g. expression( document.body.clientWidth > 600 ? "600px" : "auto" ))
  • oldIEFixes: number of fixes for old versions of Internet Explorer (e.g. * html .foo {} and .foo { *zoom: 1 }, read more)
  • importants: number of properties with value forced by !important
  • mediaQueries: number of media queries (e.g. @media screen and (min-width: 1370px))
  • oldPropertyPrefixes: number of properties with no longer needed vendor prefix, powered by data provided by autoprefixer (e.g. --moz-border-radius)
  • qualifiedSelectors: number of qualified selectors (e.g. header#nav, .foo#bar, h1.title)
  • specificityIdAvg: average specificity for ID
  • specificityIdTotal: total specificity for ID
  • specificityClassAvg: average specificity for class, pseudo-class or attribute
  • specificityClassTotal: total specificity for class, pseudo-class or attribute
  • specificityTagAvg: average specificity for element
  • specificityTagTotal: total specificity for element
  • selectorsByAttribute: number of selectors by attribute (e.g. .foo[value=bar])
  • selectorsByClass: number of selectors by class
  • selectorsById: number of selectors by ID
  • selectorsByPseudo: number of pseudo-selectors (e,g. :hover)
  • selectorsByTag: number of selectors by tag name
  • universalSelectors: number of selectors trying to match every element (e.g. .foo > *)
  • length: length of CSS source (in bytes)
  • rules: number of rules (e.g. .foo, .bar { color: red } is counted as one rule)
  • selectors: number of selectors (e.g. .foo, .bar { color: red } is counted as two selectors - .foo and .bar)
  • declarations: number of declarations (e.g. .foo, .bar { color: red } is counted as one declaration - color: red)

Read more

Dev hints

Running tests and linting the code:

npm test && npm run-script lint

Turning on debug mode (i.e. verbose logging to stderr via debug module):

DEBUG=analyze-css* analyze-css ...
npm loves you