Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 10, 2025

This PR adds support for optimizing the global JavaScript functions isNaN, isFinite, parseFloat, and parseInt during constant evaluation in the minifier.

Changes

Core Implementation

  • Extended try_fold_global_functions in crates/oxc_ecmascript/src/constant_evaluation/call_expr.rs to handle the four new global functions
  • Uses existing evaluate_value_to_number() and evaluate_value_to_string() methods for proper ECMAScript-compliant type coercion
  • Implemented proper radix handling for parseInt using to_int_32()

Key Features

  • Global isNaN(value): Converts value to Number then tests if NaN, supporting string literals, null, and numeric literals
  • Global isFinite(value): Converts value to Number then tests if finite, with same type support
  • parseFloat(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

// Before optimization
isNaN('abc')        // → isNaN('abc')
parseFloat('1.23')  // → parseFloat('1.23') 
parseInt('ff', 16)  // → parseInt('ff', 16)

// After optimization  
isNaN('abc')        // → true
parseFloat('1.23')  // → 1.23
parseInt('ff', 16)  // → 255

ECMAScript Compliance

  • Implements proper whitespace trimming for string literals in global functions (e.g., isNaN(' ')false)
  • Handles edge cases like isFinite(' ') correctly (space converts to 0, which is finite)
  • Maintains conservative approach for complex expressions to avoid incorrect optimizations

Testing

  • Added comprehensive test suites covering edge cases, scientific notation, radix handling, and precision considerations
  • All 292 minifier tests pass with zero regressions

Conservative Approach

The implementation is conservative about edge cases to avoid incorrect optimizations:

  • Only handles string literals, template literals without interpolation, and numeric literals
  • Avoids optimizing very long decimal numbers that could have precision issues
  • Handles invalid radix values appropriately

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.

@graphite-app
Copy link
Contributor

graphite-app bot commented Aug 10, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

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.

Copilot AI changed the title [WIP] Add 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... Add constant evaluation support for global isNaN, isFinite, parseFloat, parseInt functions Aug 10, 2025
Copilot AI requested a review from sapphi-red August 10, 2025 08:29
Copilot finished work on behalf of sapphi-red August 10, 2025 08:29
Copilot AI requested a review from sapphi-red August 10, 2025 09:05
Copilot finished work on behalf of sapphi-red August 10, 2025 09:05
Copilot finished work on behalf of sapphi-red August 10, 2025 09:30
Copilot AI requested a review from sapphi-red August 10, 2025 09:30
Copilot AI and others added 8 commits August 11, 2025 02:19
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>
@sapphi-red sapphi-red force-pushed the copilot/fix-cea5421b-0011-45f0-b26d-f569bf574ca1 branch from 580c13a to 1bd6234 Compare August 10, 2025 17:21
@github-actions github-actions bot added the A-minifier Area - Minifier label Aug 10, 2025
Copy link
Member


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

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.

@sapphi-red sapphi-red changed the title Add constant evaluation support for global isNaN, isFinite, parseFloat, parseInt functions feat(ecmascript): add global isNaN, isFinite, parseFloat, parseInt functions support to constant evaluation Aug 10, 2025
@github-actions github-actions bot added the C-enhancement Category - New feature or request label Aug 10, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Aug 10, 2025

CodSpeed Instrumentation Performance Report

Merging #12954 will not alter performance

Comparing copilot/fix-cea5421b-0011-45f0-b26d-f569bf574ca1 (1bd6234) with main (451bc07)

Summary

✅ 34 untouched benchmarks

@sapphi-red sapphi-red marked this pull request as ready for review August 10, 2025 17:42
Copilot AI review requested due to automatic review settings August 10, 2025 17:42
Copy link
Contributor

Copilot AI left a 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 parseFloat with scientific notation and partial parsing support
  • Added radix handling for parseInt with 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

@Boshen Boshen merged commit 33c0e9f into main Aug 11, 2025
30 of 31 checks passed
@Boshen Boshen deleted the copilot/fix-cea5421b-0011-45f0-b26d-f569bf574ca1 branch August 11, 2025 06:01
taearls pushed a commit to taearls/oxc that referenced this pull request Aug 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-minifier Area - Minifier C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants