Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Number.isFinite(Number(value)) vis global isNaN
(version: 1)
Tests relative performance of Number method vs global
Comparing performance of:
Number methods vs global isNaN
Created:
7 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*your preparation JavaScript code goes here To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/ async function globalMeasureThatScriptPrepareFunction() { // This function is optional, feel free to remove it. // await someThing(); }
Tests:
Number methods
Number.isFinite(Number("NaN")); Number.isFinite(Number(undefined)); Number.isFinite(Number({})); Number.isFinite(Number("blabla")); Number.isFinite(Number(true)); Number.isFinite(Number(null)); Number.isFinite(Number("37")); Number.isFinite(Number("37.37")); Number.isFinite(Number("")); Number.isFinite(Number(" "));
global isNaN
isNaN("NaN"); isNaN(undefined); isNaN({}); isNaN("blabla"); isNaN(true); isNaN(null); isNaN("37"); isNaN("37.37"); isNaN(""); isNaN(" ");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Number methods
global isNaN
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Number methods
11950894.0 Ops/sec
global isNaN
4559722.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 7 months ago):
### Benchmark Overview The benchmark defined in the provided JSON compares the performance of two different methods for checking if a value is a finite number: `Number.isFinite()` and the global `isNaN()` function. Both methods are part of JavaScript's handling of numeric values, but they behave differently in terms of their input validation. #### 1. Options Compared - **Option 1: Number.isFinite()** - **Function Signature**: `Number.isFinite(value)` - This method determines whether the passed value is a finite number. It returns `true` for any finite number, and `false` for values like `NaN`, `Infinity`, and `-Infinity`, as well as for non-numeric types like strings and objects when they are coerced to numbers. - **Option 2: global isNaN()** - **Function Signature**: `isNaN(value)` - This global function determines whether a value is `NaN` (Not-a-Number). However, it is important to note that it coerces the input into a number, which can lead to misleading results. For example, `isNaN('hello')` would return `true`, since `'hello'` is converted to `NaN`, whereas `Number.isFinite('hello')` returns `false`. #### 2. Pros and Cons - **Number.isFinite()** - **Pros**: - More precise in determining whether the input is a finite number. - Does not coerce non-numeric values. It immediately returns `false` for such types, leading to fewer ambiguities. - **Cons**: - Only available in ECMAScript 2015 (ES6) and later. Older environments may not support it. - **global isNaN()** - **Pros**: - More inclusive, as it can detect if a value is `NaN`, which is helpful in certain contexts. - Available in all JavaScript environments, including older versions, which enhances compatibility. - **Cons**: - Can yield unexpected results due to type coercion, which may lead to bugs if not understood properly. For instance, it also returns `true` for strings that cannot be converted to numbers. #### 3. Other Considerations - **Performance**: The benchmark results show that `Number.isFinite()` achieves significantly higher executions per second (approximately 11.95 million) compared to `isNaN()` (approximately 4.56 million). This indicates that `Number.isFinite()` is more efficient for repeated checks, which can be critical in performance-sensitive applications. - **Use Cases**: While both methods serve to check the validity of numbers, they should be used in contexts that align with their behavior. For checking if values are finite numbers, `Number.isFinite()` is recommended, whereas `isNaN()` may be useful when needing to identify `NaN` values, keeping in mind its coercion behavior. #### 4. Alternatives Besides `Number.isFinite()` and `global isNaN()`, there are a few alternatives and additional considerations: - **isFinite()**: This is a global function related to `Number.isFinite()` but performs coercion. It is not recommended due to the same coercion issues inherent in `isNaN()`. - **Custom Function**: In specific applications, developers can create custom validation functions tailored to their needs, allowing for more granular control over input validation and the handling of edge cases. In summary, the benchmark highlights the practical differences in performance and behavior between `Number.isFinite()` and `isNaN()`, guiding developers in choosing the right method for different scenarios. Understanding the nuances of both functions is essential for writing robust JavaScript, especially in applications that demand accurate numeric validations.
Related benchmarks:
Counter Increasement v2
Counter Increasement v4
Counter Increasement v5
undefined to boolean
Parse number to bigint
float toString() vs toFixed() for a number with many decimals
numeric vs string property accessor
test slice czourhgirzjgprz
key number vs string number
Comments
Confirm delete:
Do you really want to delete benchmark?