npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mauriciolobo/semantic-comparer

v0.1.0

Published

Semantic text comparison using ML embeddings

Downloads

55

Readme

semantic-comparer

Semantic text comparison using ML embeddings. Compare texts by meaning, not just characters.

Installation

npm install semantic-comparer

Optional: Pre-download Model

On first use, the package downloads an embedding model (~80MB). To download it during installation:

npx semantic-comparer download-model

Benefits:

  • No network delay on first use
  • Works offline after pre-download
  • Faster startup time

Check if model is cached:

npx semantic-comparer download-model --verify

Suppress postinstall tip:

SEMANTIC_COMPARER_SILENT=1 npm install semantic-comparer

Note: The model downloads to node_modules/@xenova/transformers/.cache/. Without pre-downloading, the model downloads automatically on first use.

CLI Usage

Compare two texts directly from the command line:

npx semantic-comparer "The cat sat on the mat" "A feline rested on the rug"
# Output: 0.7234

Special characters and quotes:

# Use single quotes for texts with double quotes
npx semantic-comparer 'He said "hello"' 'She replied "hi"'

# Or escape double quotes
npx semantic-comparer "He said \"hello\"" "She replied \"hi\""

Script usage:

SCORE=$(npx semantic-comparer "text one" "text two")
echo "Similarity: $SCORE"

Requirements

  • Node.js >= 18

Usage

import { SemanticComparer } from 'semantic-comparer';

const comparer = await SemanticComparer.create();

// Compare two texts
const result = await comparer.compare(
  "The cat sat on the mat",
  "A feline was resting on the rug"
);

console.log(result.score); // ~0.75 (0-1 scale, higher = more similar)

API

SemanticComparer.create(config?)

Creates a new comparer instance (async factory).

Options:

  • config.model - Model identifier (default: "Xenova/all-MiniLM-L6-v2")

Returns: Promise<SemanticComparer>

Example:

// Use default model
const comparer = await SemanticComparer.create();

// Use custom model (if supported)
const comparer = await SemanticComparer.create({ 
  model: 'Xenova/all-MiniLM-L6-v2' 
});

comparer.compare(text1, text2)

Compares two texts semantically.

Parameters:

  • text1 (string) - First text to compare
  • text2 (string) - Second text to compare

Returns: Promise<{ score: number }> where score is between 0 (unrelated) and 1 (identical meaning).

Example:

const result = await comparer.compare(
  "Machine learning is transforming technology",
  "AI is revolutionizing tech"
);
console.log(result.score); // ~0.72

SEMANTIC_THRESHOLD

Exported constant with the default similarity threshold (default: 0.7).

import { SEMANTIC_THRESHOLD } from 'semantic-comparer';
console.log(SEMANTIC_THRESHOLD); // 0.7

Environment Variables

  • MODEL - Override the default embedding model
  • SEMANTIC_THRESHOLD - Default similarity threshold (default: 0.7)
  • SEMANTIC_COMPARER_SILENT - Set to 1 to suppress postinstall tip message

How It Works

  1. Embeddings: Converts text into high-dimensional vectors (384 dimensions) using @xenova/transformers
  2. Normalization: Vectors are normalized to unit length during embedding generation
  3. Similarity: Calculates semantic similarity using optimized dot product (equivalent to cosine similarity for normalized vectors)
  4. Score: Returns a value from 0 (unrelated) to 1 (identical meaning)

Performance: ~40% faster than standard cosine similarity by leveraging normalized embeddings.

Supported Models

Currently supports:

  • Xenova/all-MiniLM-L6-v2 (default, ~80MB)

License

MIT