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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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 435 +326 (+299%)

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
@radashi-bot
Copy link

radashi-bot commented Apr 27, 2025

Benchmark Results

Name Current Baseline Change
cluster ▶︎ with default cluster size 1,468,502.04 ops/sec ±0.28% 3,253,380.41 ops/sec ±0.77% 🔗 🐢 -54.86%
cluster ▶︎ specified cluster size of 3 1,454,172.44 ops/sec ±0.42% 3,140,667.44 ops/sec ±0.68% 🔗 🐢 -53.7%

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.

@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.

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