Skip to content
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

path: change path.join to use array dynamic allocation #54397

Closed
wants to merge 1 commit into from

Conversation

HBSPS
Copy link
Contributor

@HBSPS HBSPS commented Aug 15, 2024

Change path.join to use array dynamic allocation instead of push.

                                                               confidence improvement accuracy (*)   (**)  (***)
path/join-posix.js n=100000 paths='/foo|bar||baz/asdf|quux|..'                 0.19 %       ±0.53% ±0.71% ±0.92%

Refs: #54331

Change path.join to use array dynamic allocation instead of push.

Refs: nodejs#54331
@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. path Issues and PRs related to the path subsystem. labels Aug 15, 2024
@aduh95
Copy link
Contributor

aduh95 commented Aug 15, 2024

Benchmark CI: https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1606/

                                                               confidence improvement accuracy (*)   (**)  (***)
path/join-posix.js n=100000 paths='/foo|bar||baz/asdf|quux|..'                -0.15 %       ±0.75% ±1.00% ±1.30%

@aduh95
Copy link
Contributor

aduh95 commented Aug 15, 2024

I the benchmark results you shared can be generalized, we should probably replace all instances of ArrayPrototypePush with this instead.

@targos
Copy link
Member

targos commented Aug 16, 2024

Why would we do that?

@targos
Copy link
Member

targos commented Aug 16, 2024

If we don't care about prototype pollution, array.push() is more straightforward IMO.

@HBSPS
Copy link
Contributor Author

HBSPS commented Aug 16, 2024

If we don't care about prototype pollution, array.push() is more straightforward IMO.

I agree with you.

I think this could be a solution for those who are worried about prototype pollution and those who are focusing on performance, although other points will need to check more.

This is because there is no significant difference in performance from array.push and does not use built-in object methods.

@targos
Copy link
Member

targos commented Aug 16, 2024

But:

  • array[array.length] = value is also subject to prototype pollution
  • the benchmarks show that it's not faster

@aduh95
Copy link
Contributor

aduh95 commented Aug 16, 2024

  • array[array.length] = value is also subject to prototype pollution

Just as much as ArrayPrototypePush, so if it has better performance, it just makes sense to use it.

@targos
Copy link
Member

targos commented Aug 16, 2024

My point is that it doesn't have better performance than array.push(). What's the reason to use it now that we have the benchmark results?

@aduh95
Copy link
Contributor

aduh95 commented Aug 16, 2024

I don't disagree with your point. Let me try to better explain my perspective:

Method Performance Reliance on %Array.prototype%.push Reliance on %Array.prototype% not having number properties
require("internal/util").setOwnProperty(arr, arr.length, …) 🐌 👍 👍
ArrayPrototypePush(arr, …) 🐢 👍
arr[arr.length] = … 👍 👍
arr.push(…) 👍

My point is that we should use arr[arr.length] = … because that seems to be the one balancing best performance and "security" (equivalent to ArrayPrototypePush for prototype pollution, and equivalent to arr.push(…) for performance).

@targos
Copy link
Member

targos commented Aug 16, 2024

Are you sure that ArrayPrototypePush is slower? It's not what #54331 (comment) shows.
I personally prefer keeping ArrayPrototypePush if the "safety" is the same. It is more readable IMO.

@HBSPS HBSPS closed this Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. path Issues and PRs related to the path subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants