Awesome Open Source
Awesome Open Source


Identify structurally similar functions that are duplicated across a JavaScript bundle/file seamlessly.

Running it on Inferno

CPA on Inferno


  • Works only across function boundaries
  • Matches the longest common subsequence and ignores the children
  • Ignores comments on the output
  • HTML report generation



npm install -g js-cpa


Usage: js-cpa [options] <file ...>


  -V, --version              output the version number
  -f, --filelist             read filelist from STDIN stream - if/when you cross ARG_MAX. eg: ls *.js | js-cpa -f
  -m, --module               parse files with sourceType=module
  -l, --language <language>  language (js|ts|flow)
  -t, --threshold <n>        threshold (in bytes)
  -C, --no-colors            disable colors in output
  -R, --report <type>        generate reports (html|term)
  -o, --report-file <path>   path for report generation
  -h, --help                 output usage information


const { findDuplicates, stringify }= require('js-cpa');
const fs = require("fs");
const code = fs.readFileSync(filePath, "utf-8");
const duplicates = findDuplicates(code, {
  filename: "test"

process.stdout.write(stringify(duplicates)); // prints to stdout

  • filename - name of the file used in the output
  • sourceType - denotes the mode the code should be parsed in. eg - script|module
  • language - denotes the language. eg - (js|ts|flow)
  • threshold - threshold in bytes
findDuplicates(code, opts)

Finds the optimal duplicate functions that are structurally similar. It tries to find the longest matching subexpression across the AST which ignores the children(inner function) if the parent(outer function) is already mached

findAllDuplicates(code, opts)

Finds all duplicate functions that are structurally similar.

stringify(duplicates, options)

Gets the output in a more presentable way


  • colors - enable colors on the stdout
  • newline - prints newline after each duplicates

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
javascript (70,283
code-quality (85