Introduction

Measuring lines of code (LOC) is a valuable practice for assessing project complexity, tracking development progress, and optimizing code efficiency. In this article, we’ll explore the most efficient ways to count LOC across any software project, covering:

  • Built-in command-line methods
  • Third-party tools
  • Pros and cons of each approach
  • Hands-on demonstrations
  • A comparison of tokei and cloc to determine the best choice

Methods to Count LOC

tokei is a high-performance, Rust-based tool designed for speed and accuracy in LOC analysis.

Installation

cargo install tokei

Usage

tokei

Example Output

-------------------------------------------------------------------------------
Language          files    blank    comment    code
-------------------------------------------------------------------------------
JavaScript        12       320      90         1250
TypeScript        8        190      60         920
CSS               4        110      25         450
-------------------------------------------------------------------------------
Total             24       620      175        2620
-------------------------------------------------------------------------------

If cargo and rust are not installed, you can use the following command:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
cargo install tokei

alternatively, you can use the following command:

sudo apt install rustc cargo

Pros & Cons

✅ Exceptional speed and performance

✅ Provides a detailed breakdown by language

✅ Supports multiple programming languages

❌ Requires Rust & Cargo installation

2. cloc

cloc (Count Lines of Code) is a widely used tool that accurately counts lines while ignoring empty lines and comments.

Installation

npm install -g cloc

Usage

cloc --include-ext=js,jsx,ts,tsx,css,scss,html .

Example Output

-------------------------------------------------------------------------------
Language          files    blank    comment    code
-------------------------------------------------------------------------------
JavaScript        12       300      100        1200
TypeScript        8        200      50         900
CSS               4        100      30         400
-------------------------------------------------------------------------------
Total             24       600      180        2500
-------------------------------------------------------------------------------

Pros & Cons

✅ Simple installation and usage

✅ Ignores blank lines and comments for accuracy

✅ Works across multiple platforms

c Slower than tokei

3. wc -l (Basic CLI Method)

The wc -l command provides a quick count of lines but lacks filtering capabilities.

Usage

find . -name "*.js" -o -name "*.ts" -o -name "*.tsx" -o -name "*.jsx" | xargs wc -l

Example Output

2300 total

Pros & Cons

✅ No additional installations required

✅ Works on Linux and macOS

❌ Counts blank lines and comments

❌ Not available on Windows by default

4. git ls-files (For Git Projects)

This method counts lines only in files tracked by Git, making it useful for repositories.

Usage

git ls-files '*.js' '*.jsx' '*.ts' '*.tsx' | xargs wc -l

Example Output

1800 total

Pros & Cons

✅ Works efficiently within Git repositories

✅ Filters out ignored and unnecessary files

❌ Ignores untracked files

Choosing Between tokei and cloc

Featuretokeicloc
Speed⭐⭐⭐⭐⭐⭐⭐⭐
Accuracy⭐⭐⭐⭐⭐⭐⭐
Detailed Breakdown
Ignores Comments
Windows Support
Installation ComplexityRequires RustEasy

When to Use tokei

  • You need the fastest LOC counting tool
  • Your project is large and performance is a priority
  • You want support for multiple languages

When to Use cloc

  • You prefer an easy-to-install solution
  • You need an accurate count while ignoring empty lines
  • You work on smaller projects where speed is less critical

For professional development workflows, tokei is the superior choice due to its unparalleled speed and detailed output. However, cloc remains a strong alternative for those who prioritize ease of installation over performance.

Conclusion

MethodSpeedAccuracyWorks on Windows?Requires Install?
tokei⭐⭐⭐⭐⭐⭐⭐⭐
cloc⭐⭐⭐⭐⭐⭐⭐
wc -l⭐⭐⭐⭐⭐⭐
git ls-files⭐⭐⭐⭐⭐⭐

For most developers, tokei is the best choice due to its high-speed performance and detailed reporting. If you require a simpler setup, cloc remains a viable alternative.

Measure your project’s complexity with tokei today and enhance your development workflow!