GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: optimize Range parsing and formatting by jviide · Pull Request #726 · npm/node-semver

Conversation

@jviide

@jviide jviide commented Jul 7, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This pull request optimizes the Range class in the following ways:

  1. Produce fewer intermediate objects when reducing a range's space characters to single spaces. This seems to improve bench-subset scores by up to 5%, and bench-satisfies scores to a lesser degree.
  2. Optimize Range formatting with explicit for loops instead, avoiding an intermediate array creation. This seems to improve bench-satisfies and bench-subset scores by up to 20%.
  3. Calculate Range's .range string (used by .format() and .toString()) lazily. This seems to improve bench-satisfies and bench-subset scores by up to 9%.

The external interface for the class stays the same, except for the new internal .formatted property used to cache its lazily calculated string. Range#range property is now also read-only.

There is a new test lazy formatting to ensure full test coverage.

The benchmarks bench-satisfies and bench-subset benefit from these changes, sometimes by up to 40%. Other benchmark results seem to stay the same. Here are the affected benchmarks before:

$ node benchmarks/bench-satisfies.js
satisfies(1.0.6, 1.0.3||^2.0.0) x 695,094 ops/sec ±0.68% (97 runs sampled)
satisfies(1.0.6, 2.2.2||~3.0.0) x 764,115 ops/sec ±0.40% (99 runs sampled)
satisfies(1.0.6, 2.3.0||<4.0.0) x 805,593 ops/sec ±0.62% (97 runs sampled)
satisfies(1.0.6, 1.0.3||^2.0.0, {"includePrelease":true}) x 695,045 ops/sec ±0.73% (95 runs sampled)
satisfies(1.0.6, 2.2.2||~3.0.0, {"includePrelease":true}) x 750,433 ops/sec ±0.66% (99 runs sampled)
satisfies(1.0.6, 2.3.0||<4.0.0, {"includePrelease":true}) x 787,903 ops/sec ±0.39% (99 runs sampled)
satisfies(1.0.6, 1.0.3||^2.0.0, {"includePrelease":true,"loose":true}) x 652,166 ops/sec ±0.34% (99 runs sampled)
satisfies(1.0.6, 2.2.2||~3.0.0, {"includePrelease":true,"loose":true}) x 696,377 ops/sec ±0.36% (96 runs sampled)
satisfies(1.0.6, 2.3.0||<4.0.0, {"includePrelease":true,"loose":true}) x 721,729 ops/sec ±0.35% (98 runs sampled)
satisfies(1.0.6, 1.0.3||^2.0.0, {"includePrelease":true,"loose":true,"rtl":true}) x 585,692 ops/sec ±0.75% (95 runs sampled)
satisfies(1.0.6, 2.2.2||~3.0.0, {"includePrelease":true,"loose":true,"rtl":true}) x 631,653 ops/sec ±0.33% (96 runs sampled)
satisfies(1.0.6, 2.3.0||<4.0.0, {"includePrelease":true,"loose":true,"rtl":true}) x 650,110 ops/sec ±0.64% (95 runs sampled)

$ node benchmarks/bench-subset.js
subset(1.2.3, *) x 633,342 ops/sec ±0.61% (95 runs sampled)
subset(^1.2.3, *) x 743,036 ops/sec ±0.47% (97 runs sampled)
subset(^1.2.3-pre.0, *) x 680,087 ops/sec ±0.76% (98 runs sampled)
subset(^1.2.3-pre.0, *) x 680,948 ops/sec ±0.46% (96 runs sampled)
subset(1 || 2 || 3, *) x 330,669 ops/sec ±0.53% (98 runs sampled)

And after:

$ node benchmarks/bench-satisfies.js
satisfies(1.0.6, 1.0.3||^2.0.0) x 896,936 ops/sec ±0.53% (94 runs sampled)
satisfies(1.0.6, 2.2.2||~3.0.0) x 998,214 ops/sec ±0.40% (95 runs sampled)
satisfies(1.0.6, 2.3.0||<4.0.0) x 1,000,593 ops/sec ±0.43% (97 runs sampled)
satisfies(1.0.6, 1.0.3||^2.0.0, {"includePrelease":true}) x 890,369 ops/sec ±0.41% (100 runs sampled)
satisfies(1.0.6, 2.2.2||~3.0.0, {"includePrelease":true}) x 977,239 ops/sec ±0.48% (97 runs sampled)
satisfies(1.0.6, 2.3.0||<4.0.0, {"includePrelease":true}) x 983,682 ops/sec ±0.95% (96 runs sampled)
satisfies(1.0.6, 1.0.3||^2.0.0, {"includePrelease":true,"loose":true}) x 805,330 ops/sec ±0.84% (98 runs sampled)
satisfies(1.0.6, 2.2.2||~3.0.0, {"includePrelease":true,"loose":true}) x 894,117 ops/sec ±0.43% (99 runs sampled)
satisfies(1.0.6, 2.3.0||<4.0.0, {"includePrelease":true,"loose":true}) x 911,742 ops/sec ±0.42% (96 runs sampled)
satisfies(1.0.6, 1.0.3||^2.0.0, {"includePrelease":true,"loose":true,"rtl":true}) x 741,254 ops/sec ±0.35% (97 runs sampled)
satisfies(1.0.6, 2.2.2||~3.0.0, {"includePrelease":true,"loose":true,"rtl":true}) x 807,380 ops/sec ±0.42% (99 runs sampled)
satisfies(1.0.6, 2.3.0||<4.0.0, {"includePrelease":true,"loose":true,"rtl":true}) x 820,390 ops/sec ±0.37% (99 runs sampled)

$ node benchmarks/bench-subset.js
subset(1.2.3, *) x 905,030 ops/sec ±0.63% (96 runs sampled)
subset(^1.2.3, *) x 1,026,457 ops/sec ±0.63% (95 runs sampled)
subset(^1.2.3-pre.0, *) x 923,789 ops/sec ±0.41% (97 runs sampled)
subset(^1.2.3-pre.0, *) x 923,136 ops/sec ±0.44% (96 runs sampled)
subset(1 || 2 || 3, *) x 432,037 ops/sec ±0.67% (96 runs sampled)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

TheDevMinerTV, marvinhagemeister, SuperchupuDev, trivikr, kravetsone, and mbtools reacted with thumbs up emoji
All reactions
  • 👍 6 reactions
@jviide
jviide requested a review from a team as a code owner July 7, 2024 03:02
kurtextrem
kurtextrem reviewed Jul 9, 2024
View reviewed changes
Comment thread classes/range.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

kurtextrem
kurtextrem reviewed Jul 9, 2024
View reviewed changes
Comment thread classes/range.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

kurtextrem
kurtextrem reviewed Jul 9, 2024
View reviewed changes
Comment thread classes/range.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

@jviide jviide changed the title refactor: optimize Range and SemVer parsing and formatting fix: optimize Range and SemVer parsing and formatting Jul 10, 2024
@jviide

jviide commented Jul 10, 2024

Copy link
Copy Markdown
Contributor Author

The "refactor:" pull request name prefix was not allowed so I picked "fix:", and changed the commit messages to use the same prefix as well.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@wraithgar

wraithgar commented Jul 12, 2024

Copy link
Copy Markdown
Contributor

We generally squash-merge external PRs unless there's a compelling reason to keep each commit. linting will pass if either the PR title or every commit has a valid conventional prefix.

Don't worry about keeping the commit messages "valid" as we go through this PR, as we'll be using the PR title if and when this lands.

jviide reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

wraithgar
wraithgar reviewed Jul 12, 2024
View reviewed changes
Comment thread classes/range.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

wraithgar
wraithgar reviewed Jul 12, 2024
View reviewed changes
Comment thread classes/semver.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

wraithgar
wraithgar reviewed Jul 12, 2024
View reviewed changes
Comment thread functions/inc.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

wraithgar
wraithgar reviewed Jul 12, 2024
View reviewed changes
Comment thread test/classes/range.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

@wraithgar

wraithgar commented Jul 12, 2024

Copy link
Copy Markdown
Contributor

Thanks for the help in reviewing this @kurtextrem. Yes, that comment was referring to s* which is a classic ReDOS vector. Historically we've used the .split approach to fix that, because it's rarely in a hot path where performance matters as much as it does here. Good to know that String.replace is faster.

kurtextrem reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jviide jviide changed the title fix: optimize Range and SemVer parsing and formatting fix: optimize Range parsing and formatting Jul 13, 2024
jviide added 3 commits July 13, 2024 09:18
@jviide
fix(range): optimize space character normalization
f4083bd
Produce fewer intermediate objects when a range's space characters are reduced to single spaces.

This appears to have about 5% performance benefit for bench-subset, and smaller but detectable impact on bench-satisfies.
@jviide
fix(range): optimize range formatting
a9f61b8
This speeds bench-subset and bench-satisfies by up to 20%.
@jviide
fix(range): compute formatted value lazily
3f5ec0b
This speeds bench-subset and bench-satisfies by up to 9%.

The external interface of the Range class is kept as-is except that the .range property is not writable anymore.

format test
wraithgar
wraithgar approved these changes Jul 16, 2024
View reviewed changes
@wraithgar
wraithgar merged commit 73a3d79 into npm:main Jul 16, 2024
@github-actions github-actions Bot mentioned this pull request Jul 16, 2024
chore: release 7.6.3 #720
Merged
@jviide
jviide deleted the opt branch July 16, 2024 15:51
hashtagchris pushed a commit that referenced this pull request Jul 16, 2024
@github-actions
chore: release 7.6.3 (#720)
0a12d6c
🤖 I have created a release *beep* *boop*
---


## [7.6.3](v7.6.2...v7.6.3)
(2024-07-16)

### Bug Fixes

*
[`73a3d79`](73a3d79)
[#726](#726) optimize Range
parsing and formatting (#726) (@jviide)

### Documentation

*
[`2975ece`](2975ece)
[#719](#719) fix extra backtick
typo (#719) (@stdavis)

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@wojtekmaj wojtekmaj mentioned this pull request Jul 16, 2024
chore(deps): bump semver from 7.6.2 to 7.6.3 nodejs/corepack#534
Merged
aduh95 pushed a commit to nodejs/corepack that referenced this pull request Jul 20, 2024
@wojtekmaj
chore(deps): bump semver from 7.6.2 to 7.6.3 (#534)
93a49c8
This bugfix version of semver includes significant performance improvements that I guess we would like to see in corepack.

See: npm/node-semver#726
@Mhmonicox Mhmonicox mentioned this pull request Aug 7, 2024
[Snyk] Upgrade semver from 7.3.6 to 7.6.3 Mhmonicox/actions-setup-cmake#36
Open
@devcurt696 devcurt696 mentioned this pull request Aug 7, 2024
[Snyk] Upgrade semver from 7.0.0 to 7.6.3 devcurt696/resume-builder#81
Open
@hashim21223445 hashim21223445 mentioned this pull request Aug 7, 2024
[Snyk] Upgrade semver from 7.6.2 to 7.6.3 hashim21223445/build-plugin-template#2
Merged
@Exkaleburx Exkaleburx mentioned this pull request Aug 7, 2024
[Snyk] Upgrade semver from 7.6.0 to 7.6.3 Exkaleburx/vscode-languageserver-node#13
Open
@mcsaur mcsaur mentioned this pull request Aug 7, 2024
[Snyk] Upgrade semver from 7.6.2 to 7.6.3 bartonmalow/delete-old-releases#2
Merged
@fanibahar fanibahar mentioned this pull request Aug 7, 2024
[Snyk] Upgrade semver from 7.3.5 to 7.6.3 fanibahar/juice-shop1#85
Open
@rsaz rsaz mentioned this pull request Aug 7, 2024
[Snyk] Upgrade semver from 7.6.2 to 7.6.3 expressots/expressots-cli#62
Closed
@prathik2401 prathik2401 mentioned this pull request Aug 15, 2024
[Snyk] Upgrade semver from 7.6.0 to 7.6.3 prathik2401/green-commute#27
Merged
@NOUIY NOUIY mentioned this pull request Aug 17, 2024
[Snyk] Upgrade semver from 7.3.7 to 7.6.3 NOUIY/npm-groovy-lint#83
Closed
@gregory567 gregory567 mentioned this pull request Aug 21, 2024
[Snyk] Upgrade semver from 7.5.2 to 7.6.3 gregory567/ContInt-backend#5
Open
@Bbjj88h Bbjj88h mentioned this pull request Aug 21, 2024
[Snyk] Upgrade semver from 7.5.4 to 7.6.3 Bbjj88h/setup-node#1
Open
@NOUIY NOUIY mentioned this pull request Aug 23, 2024
[Snyk] Upgrade semver from 7.3.8 to 7.6.3 NOUIY/security-wg#56
Closed
@rickybright rickybright mentioned this pull request Aug 29, 2024
[Snyk] Upgrade semver from 7.3.5 to 7.6.3 rickybright/juice-shop#6
Open
@NickyMartinez NickyMartinez mentioned this pull request Aug 30, 2024
[Snyk] Upgrade semver from 7.3.5 to 7.6.3 NickyMartinez/juice-shop#5
Open
@Mhmonicox Mhmonicox mentioned this pull request Sep 7, 2024
[Snyk] Upgrade: , , semver, typed-rest-client Mhmonicox/actions-setup-cmake#37
Open
@NOUIY NOUIY mentioned this pull request Sep 7, 2024
[Snyk] Upgrade: , , react, react-dom, semver, , , clsx, , , cross-fetch, file-loader, lunr, next, raw-loader, react-syntax-highlighter, url-loader NOUIY/layer0-docs#478
Open
@YoYBaBy YoYBaBy mentioned this pull request Sep 9, 2024
[Snyk] Upgrade semver from 7.3.2 to 7.6.3 YoYBaBy/nodejs.org#282
Open
@Exkaleburx Exkaleburx mentioned this pull request Sep 9, 2024
[Snyk] Upgrade: minimatch, semver Exkaleburx/vscode-languageserver-node#14
Open
@NOUIY NOUIY mentioned this pull request Sep 13, 2024
[Snyk] Upgrade: , , semver NOUIY/setup-java#213
Open
@Thompson1985 Thompson1985 mentioned this pull request Sep 13, 2024
[Snyk] Upgrade semver from 7.6.0 to 7.6.3 Thompson1985/vscode-eslint#3
Open
@snyk-io snyk-io Bot mentioned this pull request Sep 23, 2024
[Snyk] Upgrade: , , semver SherfeyInv/setup-python#1
Open
@m-heyda m-heyda mentioned this pull request Sep 24, 2024
[Snyk] Upgrade semver from 7.5.2 to 7.6.3 m-heyda/hain#35
Open
@marklacasse marklacasse mentioned this pull request Sep 26, 2024
[Snyk] Upgrade semver from 7.6.0 to 7.6.3 marklacasse/setup-dotnet#1
Open
@phoebeyusnyk phoebeyusnyk mentioned this pull request Sep 29, 2024
[Snyk] Upgrade semver from 7.3.5 to 7.6.3 phoebeyusnyk/juice-shop#4
Open
@tahmid198 tahmid198 mentioned this pull request Sep 30, 2024
[Snyk] Upgrade semver from 7.3.5 to 7.6.3 tahmid198/juice-shop#4
Open
@AeonArriving AeonArriving mentioned this pull request Sep 30, 2024
[Snyk] Upgrade semver from 7.3.8 to 7.6.3 AeonArriving/audit-ci#2
Open
@chambridge chambridge mentioned this pull request Oct 2, 2024
[Snyk] Upgrade semver from 7.6.0 to 7.6.3 RedHatInsights/ocp-advisor-frontend#803
Merged
@InfiniteLove2020 InfiniteLove2020 mentioned this pull request Oct 3, 2024
[Snyk] Upgrade semver from 7.3.2 to 7.6.3 InfiniteLove2020/nodejs.org#6
Open
@CASABECI CASABECI mentioned this pull request Oct 6, 2024
[Snyk] Upgrade semver from 7.6.0 to 7.6.3 CASABECI/setup-node#1
Open
@ms-bot ms-bot mentioned this pull request Oct 7, 2024
chore(deps): bump 🧳 semver to ^7.6.3 mobsuccess-devops/pmd-github-action#11
Closed
@nerdy-tech-com-gitub nerdy-tech-com-gitub mentioned this pull request Oct 24, 2024
[Snyk] Upgrade semver from 6.3.1 to 7.6.3 nerdy-tech-com-gitub/toolkit#6
Open
@lkeff lkeff mentioned this pull request Oct 26, 2024
[Snyk] Upgrade semver from 7.5.4 to 7.6.3 lkeff/vscode-go#5
Open
@suculent suculent mentioned this pull request Oct 30, 2024
[Snyk] Upgrade semver from 7.5.3 to 7.6.3 suculent/thinx-device-api#484
Merged
@HaLaGu1L HaLaGu1L mentioned this pull request Nov 1, 2024
[Snyk] Upgrade semver from 7.5.2 to 7.6.3 HaLaGu1L/vscode#13
Open
@doperiddle doperiddle mentioned this pull request Nov 10, 2024
[Snyk] Upgrade semver from 7.3.2 to 7.6.3 doperiddle/paystring#6
Merged
@nickvadala-snyk nickvadala-snyk mentioned this pull request Nov 11, 2024
[Snyk] Upgrade semver from 7.3.5 to 7.6.3 nickvadala-snyk/juice-shop#4
Open
@jdmiranda jdmiranda mentioned this pull request Oct 4, 2025
Performance Optimization Opportunities: Caching, Memoization, and Allocation Reduction #814
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

3 more reviewers
@kurtextrem kurtextrem kurtextrem left review comments
@marvinhagemeister marvinhagemeister marvinhagemeister left review comments
@wraithgar wraithgar wraithgar approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@jviide @wraithgar @kurtextrem @marvinhagemeister
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.