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

feat(es/utils): Support for arrays using cast_to_number #9212

Merged
merged 4 commits into from
Jul 12, 2024

Conversation

levi-nz
Copy link
Contributor

@levi-nz levi-nz commented Jul 11, 2024

Description:
This PR allows ArrayLits to be converted to numbers in the cast_to_number function. This allows expressions using arrays to be converted to numbers. See some example expressions below that were previously not able to be computed, but are now able to due to this change.

+[] // 0
+[[]] // 0
+[1] // 1
+[undefined] // 0
+[null] // 0
+[[1]] // 1
+[,] // 0
+[,,] // NaN

Regarding the implementation, arrays are converted to strings, and the string is then parsed as a number. So arrays like [] and [undefined] return "" which then return 0 when parsed as a string. This is also why arrays with more than one element can't be parsed because e.g. [1, 2] returns "1,2". This procedure follows the ECMAScript specification.
https://262.ecma-international.org/6.0/#sec-tonumber
https://262.ecma-international.org/6.0/#sec-toprimitive

BREAKING CHANGE:

Related issue (if exists):

@levi-nz levi-nz requested a review from a team as a code owner July 11, 2024 16:35
Copy link

codspeed-hq bot commented Jul 11, 2024

CodSpeed Performance Report

Merging #9212 will degrade performances by 38.94%

Comparing levi-nz:arrays-to-numbers (bb7ea9d) with main (92879b1)

Summary

❌ 6 regressions
✅ 168 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main levi-nz:arrays-to-numbers Change
es/full/bugs-1 587.4 µs 612 µs -4.03%
es/full/base/resolver_and_hygiene 328.7 µs 538.3 µs -38.94%
es/visitor/base-perf/boxing_boxed 3.8 µs 4 µs -3.66%
es/visitor/base-perf/boxing_boxed_clone 2.6 µs 2.8 µs -5.25%
es/visitor/base-perf/boxing_unboxed 3.7 µs 3.9 µs -5.18%
es/visitor/base-perf/boxing_unboxed_clone 2.6 µs 2.8 µs -8.35%

@levi-nz
Copy link
Contributor Author

levi-nz commented Jul 11, 2024

ff2749d might be better for performance, and does the same thing as far as I'm aware, but the current code is more correct.

The performance change might also simply be because of arrays not being casted to numbers before.

@kdy1
Copy link
Member

kdy1 commented Jul 11, 2024

No worries, those performance issues are not related to this PR

@kdy1 kdy1 self-assigned this Jul 11, 2024
@kdy1 kdy1 added this to the Planned milestone Jul 11, 2024
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

swc-bump:

  • swc_ecma_utils
  • swc_core

@kdy1 kdy1 enabled auto-merge (squash) July 11, 2024 19:58
@levi-nz
Copy link
Contributor Author

levi-nz commented Jul 11, 2024

Sounds good, also I'm not sure if the return statements are correct regarding purity but I assume you've looked over that already

@kdy1 kdy1 disabled auto-merge July 12, 2024 03:14
@kdy1 kdy1 merged commit 2aef14d into swc-project:main Jul 12, 2024
150 of 151 checks passed
@kdy1 kdy1 modified the milestones: Planned, v1.7.0 Jul 17, 2024
@swc-project swc-project locked as resolved and limited conversation to collaborators Aug 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants