Skip to content

[Refactor] Handle pathological strings better#13

Merged
ljharb merged 1 commit into
es-shims:mainfrom
ericcornelissen:patch-1
Jun 5, 2026
Merged

[Refactor] Handle pathological strings better#13
ljharb merged 1 commit into
es-shims:mainfrom
ericcornelissen:patch-1

Conversation

@ericcornelissen

@ericcornelissen ericcornelissen commented Nov 16, 2025

Copy link
Copy Markdown
Contributor

Improve input string that are pathological by virtue of containing lots of whitespace but not at the end or start of the string. Such strings can lead to quadratic runtime of the rightWhitespace regular expression, unnecessarily consuming computational resources.

This patches addresses the problem by ensuring that the string does end with a whitespace character before removing whitespace character from the end of the string. This ensures that pathological inputs cannot cause quadratic runtime because such inputs won't reach the rightWhitespace expression.

An alternative solution would be to have a negative lookbehind in the rightWhitespace expression. But, this regexp feature was not yet available in versions of JavaScript for which this polyfill is meant, hence it is not viable.

@ljharb

ljharb commented Nov 16, 2025

Copy link
Copy Markdown
Member

If there's no regression test I'm not sure it's worth fixing.

@ericcornelissen

ericcornelissen commented Nov 17, 2025

Copy link
Copy Markdown
Contributor Author

I added a test case for it now1. Let me know what you think.

I will update the use of String.prototype.repeat for older Node.js versions later.

I'm open to creating an abstraction for it to hide the relative complexity of the test logic (e.g. in the form of a library/extension for tape).

Footnotes

  1. I'll update my other PRs once we settle on a solution here.

Comment thread implementation.js Outdated
Comment thread test/tests.js Outdated
@ericcornelissen

Copy link
Copy Markdown
Contributor Author

@ljharb friendly ping, in case this fell of your radar.

The CI failure is due to a timeout while installing Node.js/npm dependencies.

@ericcornelissen

Copy link
Copy Markdown
Contributor Author

@ljharb, anything I can do to move this forward?

Improve input string that are pathological by virtue
of containing lots of whitespace but not at the end
or start of the string. Such strings can lead to
quadratic runtime of the `rightWhitespace` regular
expression, unnecessarily consuming computational
resources.

This patch addresses the problem by ensuring that
the string does end with a whitespace character
before removing whitespace character from the end of
the string. This ensures that pathological inputs
cannot cause quadratic runtime because such inputs
won't reach the `rightWhitespace` expression.

An alternative solution would be to have a negative
lookbehind in the `rightWhitespace` expression. But,
this regexp feature was not yet available in versions
of JavaScript for which this polyfill is meant, hence
it is not viable.

The change is tested with a pathalogical input in
the test suite. This is supported by the assert-time
package which faciliates enforcing a maximum runtime
on the function call.
@socket-security

socket-security Bot commented Jun 5, 2026

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addednpm/​@​ericcornelissen/​assert-time@​1.0.37010010091100
Addednpm/​string.prototype.repeat@​1.0.01001008175100

View full report

Comment thread test/tests.js Outdated
@ljharb

ljharb commented Jun 5, 2026

Copy link
Copy Markdown
Member

sorry for the errant push; i'm still reviewing this.

@ljharb ljharb merged commit 91e5eb6 into es-shims:main Jun 5, 2026
839 checks passed
@ericcornelissen ericcornelissen deleted the patch-1 branch June 5, 2026 22:25
ljharb added a commit to es-shims/String.prototype.trimEnd that referenced this pull request Jun 5, 2026
…itespace runs

The `$`-anchored `endWhitespace` regexp retried its match at every index,
running in O(n^2) on inputs with a large internal whitespace run followed by a non-whitespace character (e.g. `'A' + ' '.repeat(1e5) + 'A'`).

Trim trailing whitespace by scanning back from the end for the boundary instead;
that is linear regardless of where the whitespace lives,
and needs no new dependencies
(`call-bound` is already a dependency).
`slice(this, 0)`
preserves the original RequireObjectCoercible + ToString coercion.

see es-shims/String.prototype.trim#13
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.

2 participants