⚡️ Speed up function isValidTimespan by 32%#1
Open
codeflash-ai[bot] wants to merge 1 commit intodevelopfrom
Open
⚡️ Speed up function isValidTimespan by 32%#1codeflash-ai[bot] wants to merge 1 commit intodevelopfrom
isValidTimespan by 32%#1codeflash-ai[bot] wants to merge 1 commit intodevelopfrom
Conversation
This optimization achieves a **32% runtime improvement** (2.78ms → 2.10ms) by replacing expensive built-in method calls with faster primitive comparisons. **Key Changes:** 1. **Added upfront `typeof` check**: Directly checks if the input is a number primitive before validation, which is faster than relying on `Number.isFinite` to implicitly handle non-number types. 2. **Replaced `Number.isNaN()` with NaN self-comparison**: Uses `timespan !== timespan` instead of `Number.isNaN(timespan)`. NaN is the only JavaScript value that isn't equal to itself, making this comparison significantly faster by avoiding function call overhead. 3. **Replaced `Number.isFinite()` with direct Infinity checks**: Uses `timespan === Infinity || timespan === -Infinity` instead of calling `Number.isFinite()`. This eliminates a more expensive built-in method call that performs multiple internal checks. **Why This Is Faster:** - **Reduced function call overhead**: Built-in methods like `Number.isNaN()` and `Number.isFinite()` have dispatch costs. Primitive comparisons (`!==`, `===`) are direct CPU operations. - **Line profiler evidence**: The original code spent 66.9% of time in the `Number.isFinite()` call (9.292ms out of 13.888ms total). The optimized version distributes this work across cheaper operations, with no single line exceeding 50% of runtime. - **Test results confirm selective improvements**: - NaN checks are **30-50% faster** (1.79μs → 1.37μs, 625ns → 416ns) - Infinity checks are **14-33% faster** (833ns → 625ns for positive Infinity) - Valid positive numbers show minor slowdowns (4-10%) due to the extra `typeof` check, but this is vastly outweighed by the speedup on special value checks **Impact on Workloads:** Based on `function_references`, this function is called in `useRetentionPolicy.ts` for room retention validation. The optimization particularly benefits scenarios where: - Invalid inputs (NaN, Infinity, non-numbers) are frequently validated - The function is called repeatedly during UI rendering or policy calculations - Room retention settings are checked across multiple rooms in succession The 32% speedup means faster UI responsiveness when processing retention policies, especially when validating edge cases or bulk operations across multiple rooms.
misrasaurabh1
approved these changes
Feb 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📄 32% (0.32x) speedup for
isValidTimespaninapps/meteor/client/lib/convertTimeUnit.ts⏱️ Runtime :
2.78 milliseconds→2.10 milliseconds(best of11runs)📝 Explanation and details
This optimization achieves a 32% runtime improvement (2.78ms → 2.10ms) by replacing expensive built-in method calls with faster primitive comparisons.
Key Changes:
Added upfront
typeofcheck: Directly checks if the input is a number primitive before validation, which is faster than relying onNumber.isFiniteto implicitly handle non-number types.Replaced
Number.isNaN()with NaN self-comparison: Usestimespan !== timespaninstead ofNumber.isNaN(timespan). NaN is the only JavaScript value that isn't equal to itself, making this comparison significantly faster by avoiding function call overhead.Replaced
Number.isFinite()with direct Infinity checks: Usestimespan === Infinity || timespan === -Infinityinstead of callingNumber.isFinite(). This eliminates a more expensive built-in method call that performs multiple internal checks.Why This Is Faster:
Number.isNaN()andNumber.isFinite()have dispatch costs. Primitive comparisons (!==,===) are direct CPU operations.Number.isFinite()call (9.292ms out of 13.888ms total). The optimized version distributes this work across cheaper operations, with no single line exceeding 50% of runtime.typeofcheck, but this is vastly outweighed by the speedup on special value checksImpact on Workloads:
Based on
function_references, this function is called inuseRetentionPolicy.tsfor room retention validation. The optimization particularly benefits scenarios where:The 32% speedup means faster UI responsiveness when processing retention policies, especially when validating edge cases or bulk operations across multiple rooms.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-isValidTimespan-mm3tdviyand push.