Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
isNaN vs Number.isNaN Boolean
(version: 1)
Comparing performance of:
global isNaN vs method isNaN
Created:
6 months ago
by:
Guest
Jump to the latest result
Tests:
global isNaN
isNaN(true)
method isNaN
Number.isNaN(true)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
global isNaN
method isNaN
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 141 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
global isNaN
68961304.0 Ops/sec
method isNaN
70814344.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 6 months ago):
The benchmark in question compares two functionalities in JavaScript for evaluating whether a value is NaN (Not-a-Number): the global `isNaN` function and the static method `Number.isNaN`. ### Test Cases Overview: 1. **Global `isNaN` function**: - **Test Name**: `global isNaN` - **Benchmark Definition**: `isNaN(true)` - **Purpose**: This test case checks the performance of the standalone `isNaN` function when it receives `true` as an argument. 2. **Static `Number.isNaN` method**: - **Test Name**: `method isNaN` - **Benchmark Definition**: `Number.isNaN(true)` - **Purpose**: This test case evaluates the performance of the `Number.isNaN` method with the same argument (`true`). ### Pros and Cons of Each Approach: #### 1. Global `isNaN`: - **Pros**: - Compatibility: It is a legacy function and available in all JavaScript environments. - Familiarity: Many developers are accustomed to using it since it has been around for a long time. - **Cons**: - Type Coercion: `global isNaN` performs type coercion, meaning it will return `true` for any non-numeric value that cannot be converted to a number (e.g., `isNaN("hello")` returns `true`). - Potential for unexpected results: This can lead to confusion when checking if a value is specifically NaN. #### 2. `Number.isNaN`: - **Pros**: - Precision: `Number.isNaN` does not perform type coercion. It will only return `true` if the argument is precisely the value `NaN`, making it a more reliable option for checking NaN values. - Better performance: Generally, `Number.isNaN` is faster than the global `isNaN` function, especially in cases where the argument can be of various types (this is illustrated by the benchmark performance results). - **Cons**: - Limited compatibility: `Number.isNaN` was introduced in ECMAScript 2015 (ES6), so it may not be supported in very old JavaScript environments. ### Performance Results Analysis: - The benchmark shows that: - `Number.isNaN(true)` has a higher execution rate (`231,519,168.0 executions per second`) compared to `isNaN(true)` with `224,391,904.0 executions per second`. - Both tests were conducted in the same environment (Chrome 141 on Mac OS X 10.15.7), which allows for a fair comparison. ### Alternative Approaches: - **Using `Object.is()`**: Another method to check for NaN could be using `Object.is(value, NaN)`, which is also strict and does not coerce the type. - **Custom utility functions**: Developers can create their own utility functions for checking NaN, but this is usually unnecessary given the existence of the JavaScript built-ins. ### Conclusion: This benchmark effectively highlights the differences between two ways of checking for NaN—one that is traditional with potential pitfalls (global `isNaN`) and a more modern, strict, and performant approach (`Number.isNaN`). Software engineers should consider using `Number.isNaN` in new code to ensure accuracy and potentially better performance when dealing with numeric values.
Related benchmarks:
typeof x === 'number' vs isNaN(x)
typeof number vs. Number.isNan vs. isNan
isNaN vs +
isnan vs typeof
typeof vs typeof isNaN
isNaN vs Number.isNaN
isNaN vs isFinite
isFinite vs isNaN
typeof number vs. Number.isNan vs. isNan vs self comparison
typeof vs isNaN
Comments
Confirm delete:
Do you really want to delete benchmark?