Skip to content

[Replacement]: compute-lcm #1208

Open

Description

@outslept

Package to replace

compute-lcm

Package link

https://npmx.dev/compute-lcm

Suggested replacement

const gcd = (a, b) => (b === 0 ? Math.abs(a) : gcd(b, a % b));
const lcm = (a, b) => (a === 0 || b === 0 ? 0 : Math.abs((a / gcd(a, b)) * b));

Rationale

:)

Availability

No response

Code example

Activity

  1. added
    accepted
    on Nov 9, 2026
  2. kgryte commented on Nov 10, 2026

    @kgryte

    The above is not a drop-in replacement for the referenced package.

  3. 43081j commented on Nov 10, 2026

    @43081j
    Contributor

    @kgryte do you have an input which works in the referenced package but not in the suggested code?

    afaict both compute-gcd and compute-lcm seem to exist to implement specific algorithms by-the-book. i.e. they are implementing a different algorithm to solve the same problem. if that is the case, it seems this is a valid replacement if you're not concerned with which underlying algorithm has been chosen

  4. kgryte commented on Nov 12, 2026

    @kgryte

    if you're not concerned with which underlying algorithm has been chosen

    Yes, except consumers should be concerned with the underlying algorithm. Reduction + modulo arithmetic is entirely what those packages are intended to avoid. These packages exist so that consumers don't have to reimplement the underlying algorithms themselves, and you're effectively encouraging imperative inlining versus a declarative import. That, to me, does not seem ideal, especially as lcm/gcd can often be found deep within hot loops. The suggested replacements are difficult for optimizing compilers to optimize.

  5. 43081j commented on Nov 12, 2026

    @43081j
    Contributor

    can you expand on why the pattern should be avoided?

    presumably this is the reason:

    The suggested replacements are difficult for optimizing compilers to optimize.

    do you have anywhere we can learn about that? anything that shows us that is the case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions