-
-
Notifications
You must be signed in to change notification settings - Fork 722
feat(ecmascript): add global isNaN, isFinite, parseFloat, parseInt functions support to constant evaluation
#12954
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(ecmascript): add global isNaN, isFinite, parseFloat, parseInt functions support to constant evaluation
#12954
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. |
isNaN, isFinite, parseFloat, parseInt support to @oxc-project/oxc/files/crates/oxc_ecmascript/src/constant_evaluation/call_expr.rs . To make sure the implementation is correct, download https://tc39.es/ecma262/2025/ locally and read the cor...Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
… fix StringToNumber whitespace handling Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
…ng_to_number.rs changes Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
580c13a to
1bd6234
Compare
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
isNaN, isFinite, parseFloat, parseInt functions support to constant evaluation
CodSpeed Instrumentation Performance ReportMerging #12954 will not alter performanceComparing Summary
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for optimizing the global JavaScript functions isNaN, isFinite, parseFloat, and parseInt during constant evaluation in the minifier, allowing these functions to be evaluated at compile-time when their arguments are known constant values.
Key changes include:
- Extended constant evaluation to handle four new global functions with proper ECMAScript-compliant type coercion
- Implemented string parsing logic for
parseFloatwith scientific notation and partial parsing support - Added radix handling for
parseIntwith automatic hex detection and validation
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
crates/oxc_syntax/src/identifier.rs |
Adds whitespace detection functions for proper ECMAScript string trimming |
crates/oxc_ecmascript/src/string_to_number.rs |
Updates string-to-number conversion to use proper whitespace trimming |
crates/oxc_ecmascript/src/constant_evaluation/call_expr.rs |
Implements the core constant evaluation logic for the four global functions |
crates/oxc_minifier/src/peephole/replace_known_methods.rs |
Updates tests to reflect new optimization capabilities |
…eInt` functions support to constant evaluation (oxc-project#12954)

This PR adds support for optimizing the global JavaScript functions
isNaN,isFinite,parseFloat, andparseIntduring constant evaluation in the minifier.Changes
Core Implementation
try_fold_global_functionsincrates/oxc_ecmascript/src/constant_evaluation/call_expr.rsto handle the four new global functionsevaluate_value_to_number()andevaluate_value_to_string()methods for proper ECMAScript-compliant type coercionparseIntusingto_int_32()Key Features
isNaN(value): Converts value to Number then tests if NaN, supporting string literals, null, and numeric literalsisFinite(value): Converts value to Number then tests if finite, with same type supportparseFloat(string): Parses strings as floating point with scientific notation support and partial parsing (e.g.,"3.14more"→3.14)parseInt(string, radix): Parses strings as integers with automatic hex detection and explicit radix support (2-36)Examples
ECMAScript Compliance
isNaN(' ')→false)isFinite(' ')correctly (space converts to 0, which is finite)Testing
Conservative Approach
The implementation is conservative about edge cases to avoid incorrect optimizations:
This optimization improves JavaScript bundle sizes by eliminating unnecessary function calls for constant values while maintaining full semantic correctness according to the ECMAScript 2025 specification.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.