Table of Contents
Install
Usage
Command line tool
CommonJS module
grunt task
Results
Metrics
Read more
Dev hints
Home Web Front-end HTML Tutorial analyze-css:CSS 选择器的复杂度和性能分析器_html/css_WEB-ITnose

analyze-css:CSS 选择器的复杂度和性能分析器_html/css_WEB-ITnose

Jun 21, 2016 am 08:56 AM

analyze-css

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).

  • Analyze CSS Demo

Install

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

1

npm install --global analyze-css

Copy after login

Usage

Command line tool

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

1

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

Copy after login

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

1

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

Copy after login

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

Basic HTTP authentication can be provided through the options --auth-user and --auth-pass.

CommonJS module

1

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

Copy after login

1

// options can be providedvar opts = {  'noOffenders': true};new analyzer('.foo {margin: 0 !important}', opts, function(err, results) {  console.error(err);  console.log(results); // example? see below});```

Copy after login

grunt task

Created by @DeuxHuitHuit

1

npm i grunt-contrib-analyze-css

Copy after login

It uses configurable threshold and compares the analyze-css result with it.

Results

1

"generator": "analyze-css v0.8.0""metrics": {    "base64Length": 11332,    "redundantBodySelectors": 0,    "redundantChildNodesSelectors": 1,    "colors": 106,    "comments": 1,    "commentsLength": 68,    "complexSelectors": 37,    "complexSelectorsByAttribute": 3,    "duplicatedSelectors": 7,    "duplicatedProperties": 24,    "emptyRules": 0,    "expressions": 0,    "oldIEFixes": 51,    "imports": 0,    "importants": 3,    "mediaQueries": 0,    "multiClassesSelectors": 74,    "parsingErrors": 0,    "oldPropertyPrefixes": 79,    "propertyResets": 0,    "qualifiedSelectors": 28,    "specificityIdAvg": 0.04,    "specificityIdTotal": 25,    "specificityClassAvg": 1.27,    "specificityClassTotal": 904,    "specificityTagAvg": 0.79,    "specificityTagTotal": 562,    "selectors": 712,    "selectorLengthAvg": 1.5722460658082975,    "selectorsByAttribute": 92,    "selectorsByClass": 600,    "selectorsById": 25,    "selectorsByPseudo": 167,    "selectorsByTag": 533,    "universalSelectors": 5,    "length": 55173,    "rules": 433,    "declarations": 1288  },  "offenders": {    "importants": [      ".foo {margin: 0 !important}"    ]  }}

Copy after login

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)
  • redundantChildNodesSelectors: number of redundant child nodes selectors (e.g. ul li, table tr)
  • colors: number of unique colors used in CSS
  • 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)
  • complexSelectorsByAttribute: number of selectors with complex matching by attribute (e.g. [class$="foo"])
  • duplicatedSelectors: number of CSS selectors defined more than once in CSS source
  • duplicatedProperties: number of CSS property definitions duplicated within a selector
  • 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)
  • imports number of @import rules
  • importants: number of properties with value forced by !important
  • mediaQueries: number of media queries (e.g. @media screen and (min-width: 1370px))
  • multiClassesSelectors: reports selectors with multiple classes (e.g. span.foo.bar)
  • parsingErrors: number of CSS parsing errors
  • oldPropertyPrefixes: number of properties with no longer needed vendor prefix, powered by data provided by autoprefixer (e.g. --moz-border-radius)
  • propertyResets: number of accidental property resets
  • 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
  • selectors: number of selectors (e.g. .foo, .bar { color: red } is counted as two selectors - .foo and .bar)
  • selectorLengthAvg: average length of selector (e.g. for .foo .bar, #test div > span { color: red } will be set as 2.5)
  • 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)
  • declarations: number of declarations (e.g. .foo, .bar { color: red } is counted as one declaration - color: red)

Read more

  • Writing Efficient CSS (by Mozilla)
  • Optimize browser rendering (by Google)
  • Profiling CSS for fun and profit. Optimization notes.
  • CSS specificity
  • CSS Selector Performance has changed! (For the better) (by Nicole Sullivan)
  • CSS Performance (by Paul Irish)
  • GitHub's CSS Performance (by Joh Rohan)

Dev hints

Running tests and linting the code:

1

npm test && npm run-script lint

Copy after login

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

1

DEBUG=analyze-css* analyze-css ...

Copy after login

项目地址: https://github.com/macbre/analyze-css

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

See all articles