Skip to content

fix: cluster size of 0 leads to infinite loop #397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 15, 2025

Conversation

fResult
Copy link
Contributor

@fResult fResult commented Apr 27, 2025

Summary

Fixes infinite loop bug when cluster() size is 0.
I chose error throwing over returning an input array due to Cluster<T, Size> type constraints.

Additional test cases were added to verify handling of various edge cases, including negative and non-integer sizes.

Related issue, if any:

#392

For any code change,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed
  • Related benchmarks have been added or updated, if needed
  • Release notes in next-minor.md or next-major.md have been added, if needed

Does this PR introduce a breaking change?

No

Bundle impact

Status File Size 1 Difference
M src/array/cluster.ts 116 +7 (+6%)

Footnotes

  1. Function size includes the import dependencies of the function.

@fResult fResult requested a review from aleclarson as a code owner April 27, 2025 16:24
@fResult fResult force-pushed the fix/cluster-size-errors branch from 260b096 to 42ca8be Compare April 27, 2025 16:26
Copy link
Member

@aleclarson aleclarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @fResult, good to see more contributions from you 👍

I'll get this merged after my review is addressed

@@ -13,6 +15,14 @@ export function cluster<T, Size extends number = 2>(
array: readonly T[],
size: Size = 2 as Size,
): Cluster<T, Size>[] {
if (size <= 0 || Number.isNaN(size)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NaN check is unnecessary, since the loop will exit when size is NaN.

throw new IllegalSizeError(`Size must be 1 or more, the size is ${size}`)
}

if (size === Number.POSITIVE_INFINITY) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. The loop will exit just fine when size is infinite.

@@ -13,6 +15,14 @@ export function cluster<T, Size extends number = 2>(
array: readonly T[],
size: Size = 2 as Size,
): Cluster<T, Size>[] {
if (size <= 0 || Number.isNaN(size)) {
throw new IllegalSizeError(`Size must be 1 or more, the size is ${size}`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think returning [] when size <= 0 would be better. It's more concise, makes logical sense, and works fine with the Cluster<T, Size>[] return type.

@fResult fResult force-pushed the fix/cluster-size-errors branch from 42ca8be to 39ad3a1 Compare May 10, 2025 04:05
@fResult fResult marked this pull request as draft May 12, 2025 18:27
@fResult
Copy link
Contributor Author

fResult commented May 13, 2025

Hi @aleclarson, I have already updated the code following your review.
Thanks for your review. 🙏

@fResult fResult marked this pull request as ready for review May 13, 2025 18:26
@fResult fResult requested a review from aleclarson May 13, 2025 18:26
@aleclarson aleclarson modified the milestones: v12.5.0, v12.6.0 May 15, 2025
@radashi-bot
Copy link

Benchmark Results

Name Current Baseline Change
cluster ▶︎ with default cluster size 3,040,367.96 ops/sec ±0.84% 2,990,410.51 ops/sec ±0.91% 🔗 🚀 +1.67%
cluster ▶︎ specified cluster size of 3 2,925,924.9 ops/sec ±0.97% 3,161,291.75 ops/sec ±1.02% 🔗 🐢 -7.45%

Performance regressions of 30% or more should be investigated, unless they were anticipated. Smaller regressions may be due to normal variability, as we don't use dedicated CI infrastructure.

@aleclarson aleclarson merged commit 214ce5d into radashi-org:main May 15, 2025
10 checks passed
@aleclarson
Copy link
Member

Appreciate the extra tests! Thanks @fResult

@radashi-bot
Copy link

A stable release 12.5.0 has been published to NPM. 🚀

To install:

See the changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants