sloc vs cloc
Code Analysis Tools Comparison
3 Years
sloccloc
What's Code Analysis Tools?

Code analysis tools are utilities that analyze source code to gather various metrics, such as lines of code (LOC), complexity, and code quality. These tools help developers and teams understand their codebase better, track progress, identify areas for improvement, and ensure code quality. They are often used in software development to generate reports, facilitate code reviews, and support project management by providing quantitative data about the code. cloc (Count Lines of Code) is a popular tool that counts the number of lines in source code files, categorizing them into blank lines, comment lines, and actual code lines. It supports multiple programming languages and can process entire directories recursively. sloc (Simple Lines of Code) is a lightweight tool that counts lines of code in a project, providing a simple breakdown of total lines, code lines, comment lines, and blank lines. It is designed for quick and easy analysis, making it suitable for small to medium-sized projects.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
sloc48,343
96135.3 kB412 years agoMIT
cloc35,548
362830 kB12 months agoGPL-2.0
Feature Comparison: sloc vs cloc

Line Counting Accuracy

  • sloc:

    sloc also counts blank lines, comment lines, and code lines, but it may not be as precise as cloc in handling comments across all languages. It provides a good estimate but lacks the depth of language-specific analysis.

  • cloc:

    cloc provides accurate line counting by distinguishing between blank lines, comment lines, and code lines. It uses language-specific heuristics to identify comments and can handle complex file types, making its counts reliable for detailed analysis.

Output Formats

  • sloc:

    sloc provides output in a simple text format, which is easy to read but lacks the versatility of multiple formats. It is suitable for quick checks but may require additional processing for integration with other tools.

  • cloc:

    cloc supports multiple output formats, including plain text, CSV, JSON, and XML. This flexibility allows users to integrate the results into reports, spreadsheets, or other tools easily.

Configuration and Customization

  • sloc:

    sloc is designed to be simple and does not offer much in the way of configuration. It counts lines as-is, with no options for excluding files or customizing the counting process, which keeps it straightforward but limits flexibility.

  • cloc:

    cloc offers extensive configuration options, including the ability to exclude specific files or directories, define custom languages, and adjust how comments are counted. This makes it highly customizable for different projects and needs.

Performance

  • sloc:

    sloc is lightweight and fast, making it ideal for quick analysis of small to medium-sized projects. Its simplicity means it uses fewer resources and completes counting quickly, but it may not handle very large codebases as efficiently as cloc.

  • cloc:

    cloc is efficient but can be slower on very large codebases due to its detailed analysis and language-specific processing. The performance impact is generally acceptable for most projects, but it may take longer for extensive repositories.

Ease of Use: Code Examples

  • sloc:

    To use sloc, run it with the target directory or file:

    sloc /path/to/your/project
    

    It will display a quick summary of the line counts in the terminal.

  • cloc:

    To use cloc, simply run it from the command line with the directory or file path as an argument:

    cloc /path/to/your/project
    

    This will generate a report with the line counts for each file and language.

How to Choose: sloc vs cloc
  • sloc:

    Choose sloc if you prefer a lightweight and straightforward tool for quick line counting with minimal setup. It provides a simple output and is ideal for projects where you need a fast overview of the codebase without extensive configuration.

  • cloc:

    Choose cloc if you need detailed line counting with support for multiple languages, the ability to ignore specific files or directories, and generate output in various formats (e.g., CSV, JSON). It is more feature-rich and suitable for comprehensive analysis.

README for sloc

sloc

Create stats of your source code:

  • physical lines
  • lines of code (source)
  • lines with comments
  • lines with single-line comments
  • lines with block comments
  • lines mixed up with source and comments
  • empty lines within block comments
  • empty lines
  • lines with TODO's

NPM version License Minified size

Supported outputs

In addition to the default terminal output (see examples below), sloc provides an alternative set of output formatters:

  • CSV
  • JSON
  • Command line table

Install

To use sloc as an application install it globally:

sudo npm install -g sloc

If you're going to use it as a Node.js module within your project:

npm install --save sloc

Browser

You can also use sloc within your browser application.

Link sloc.js in your HTML file:

<script src="lib/sloc.js"></script>

Usage

CLI

sloc [option] <file>|<directory>

Options:

-h, --help                  output usage information
-V, --version               output the version number
-e, --exclude <regex>       regular expression to exclude files and folders
-i, --include <regex>       regular expression to include files and folders
-f, --format <format>       format output: json, csv, cli-table
    --format-option [value] add formatter option
-k, --keys <keys>           report only numbers of the given keys
-d, --details               report stats of each analyzed file
-a, --alias <custom ext>=<standard ext> alias custom ext to act like standard ext (eg. php5=php,less=css)

e.g.:

$ sloc src/

---------- Result ------------

            Physical :  1202
              Source :  751
             Comment :  322
 Single-line comment :  299
       Block comment :  23
               Mixed :  116
               Empty :  245

Number of files read :  10

------------------------------

or

$ sloc --details \
       --format cli-table \
       --keys total,source,comment \
       --exclude i18n*.\.coffee \
       --format-option no-head src/

┌─────────────────────────────────┬──────────┬────────┬─────────┐
│ src/cli.coffee                  │ 98       │ 74     │ 7       │
├─────────────────────────────────┼──────────┼────────┼─────────┤
│ src/helpers.coffee              │ 26       │ 20     │ 0       │
├─────────────────────────────────┼──────────┼────────┼─────────┤
│ src/sloc.coffee                 │ 196      │ 142    │ 20      │
├─────────────────────────────────┼──────────┼────────┼─────────┤
│ src/formatters/simple.coffee    │ 44       │ 28     │ 7       │
├─────────────────────────────────┼──────────┼────────┼─────────┤
│ src/formatters/csv.coffee       │ 25       │ 14     │ 5       │
├─────────────────────────────────┼──────────┼────────┼─────────┤
│ src/formatters/cli-table.coffee │ 22       │ 13     │ 0       │
└─────────────────────────────────┴──────────┴────────┴─────────┘

Node.js

Or use it in your own node module

var fs    = require('fs');
var sloc  = require('sloc');

fs.readFile("mySourceFile.coffee", "utf8", function(err, code){

  if(err){ console.error(err); }
  else{
    var stats = sloc(code,"coffee");
    for(i in sloc.keys){
      var k = sloc.keys[i];
      console.log(k + " : " + stats[k]);
    }
  }
});

Browser usage

var sourceCode = "foo();\n /* bar */\n baz();";

var stats = window.sloc(sourceCode,"javascript");

Contribute an new formatter

  1. Fork this repo

  2. add the new formatter into src/formatters/ that exports a method with three arguments:

    1. results (object)
    2. global options (object)
    3. formatter specific options (array)
  3. add the formatter in src/cli.coffee

  4. open a pull request

sloc adapters

Supported languages

  • Assembly
  • Agda
  • Brightscript
  • C / C++
  • C#
  • Clojure / ClojureScript
  • CoffeeScript / IcedCoffeeScript
  • Crystal
  • CSS / SCSS / SASS / LESS / Stylus
  • Dart
  • Erlang
  • F#
  • Fortran
  • Go
  • Groovy
  • Handlebars
  • Haskell
  • Haxe
  • Hilbert
  • HTML
  • hy
  • Jade
  • Java
  • JavaScript
  • JSX
  • Julia
  • Kotlin
  • LaTeX
  • LilyPond
  • LiveScript
  • Lua
  • MJS
  • Mochi
  • Monkey
  • Mustache
  • Nim
  • Nix
  • Objective-C / Objective-C++
  • OCaml
  • Perl 5
  • PHP
  • PRQL
  • Pug
  • Python
  • R
  • Racket
  • Ren'Py
  • Ruby
  • Rust
  • Scala
  • Squirrel
  • SVG
  • Swift
  • Typescript
  • Visual Basic
  • XML
  • Yaml

Run tests

npm test

Build

npm run prepublish

Changelog

see CHANGELOG.md

License

sloc is licensed under the MIT license