Skip to content

[BUG] SemVer.compare causes spurious allocations of SemVer objects #458

Open
@kwasimensah

Description

@kwasimensah

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

https://github.com/npm/node-semver/blob/main/functions/compare.js#L3 always creates a new SemVer objects even if you're careful about making sure you're already using SemVer objects.

It relies on https://github.com/npm/node-semver/blob/main/classes/semver.js#L14 to return the original semver from the constructor (TIL Javascript constructors can have return statements). This may work but is surprising and seems to cause a spurious allocation.

Expected Behavior

https://github.com/npm/node-semver/blob/main/functions/compare.js#L3 conditionally converts it's operands to SemVer objects and doesn't create spurious allocations.

Steps To Reproduce

const a = new SemVer("1.0");
const b = new SemVer("2.0");

// Chrome's Memory profiler will tell you that new SemVer objects we're created in this call.
a.compare(b);

Environment

  • npm:
  • Node: v16
  • OS: Mac Os 12.3.1
  • platform: Macbook Pro

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixingsemver:majorbackwards-incompatible breaking changes

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions