Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
typeof x === 'number' vs isNaN(x) F302
(version: 1)
Comparing performance of:
isNaNx vs isNaN vs isNaN2 vs isNaNx2
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var myNum = Math.random(); var myString = Math.round(Math.random()*999+888).toString(16); var isNaNx = Number.isNaN;
Tests:
isNaNx
let u = 0; if(isNaNx(myNum))u++; if(isNaNx(myString))u++; if(isNaNx(myNum))u++; if(isNaNx(myString))u++;
isNaN
let u = 0; if(isNaN(myNum))u++; if(isNaN(myString))u++; if(isNaN(myNum))u++; if(isNaN(myString))u++;
isNaN2
let u = 0; const p = (isNaN(myNum) ? 1 : 0) + (isNaN(myString) ? 2 : 0); if(p&1)u++; if(p&2)u++; if(p&1)u++; if(p&2)u++;
isNaNx2
let u = 0; const p = (isNaNx(myNum) ? 1 : 0) + (isNaNx(myString) ? 2 : 0); if(p&1)u++; if(p&2)u++; if(p&1)u++; if(p&2)u++;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
isNaNx
isNaN
isNaN2
isNaNx2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:133.0) Gecko/20100101 Firefox/133.0
Browser/OS:
Firefox 133 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
isNaNx
135472512.0 Ops/sec
isNaN
17953822.0 Ops/sec
isNaN2
32341288.0 Ops/sec
isNaNx2
135150816.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark being tested here evaluates different methods for checking whether a value is NaN (Not-a-Number) in JavaScript. Specifically, it compares the use of the standard `isNaN` function versus a custom function named `isNaNx`, which is derived from `Number.isNaN`. The focus is on performance, specifically the execution speed of these checks within a controlled environment. ### Benchmark Options Compared 1. **isNaN(x)**: This is a built-in global function that checks whether the passed value is NaN. It coerces its argument into a number before performing the check, which can lead to unintended results if the argument is not of type number. **Pros**: - Simple and widely used. - Handles both primitives and objects. **Cons**: - Performs type coercion, which can produce false positives. For example, `isNaN('string')` returns `true`. 2. **isNaNx(x)**: This custom function uses `Number.isNaN`, which doesn’t coerce the value. It strictly checks if the value is actually NaN (of type Number). **Pros**: - More reliable for checking NaN, as no type coercion occurs. - Avoids false positives by ensuring only values that are strictly NaN return true. **Cons**: - Slightly less familiarity as it's less commonly used in comparison to `isNaN`, though it is part of the ES6 standard. ### Test Cases Analyzed The benchmark consists of four individual test cases, each attempting to measure the speed of different ways to check NaN values: 1. **Test Name: isNaNx** - Checks values with `isNaNx`. The code executes multiple checks against a number and a string, incrementing a counter for each NaN check that returns true. 2. **Test Name: isNaN** - Similar to the above but uses the `isNaN` approach. 3. **Test Name: isNaN2** - Also checks using `isNaN` but uses bitwise operations to evaluate the results—specifically checking for NaN with `&` to see the corresponding flags. 4. **Test Name: isNaNx2** - Like `isNaN2`, it uses `isNaNx` with bitwise checks instead. ### Results Interpretation - **Executions Per Second**: The benchmark results show the number of executions per second for each method. `isNaNx` had the highest execution rate, followed closely by `isNaNx2`, indicating that avoiding coercion in checks is more performant. `isNaN2` and `isNaN` were significantly slower, showcasing that bitwise operations may not offer a performance gain and that the coercion of types in `isNaN` can introduce overhead. ### Other Considerations and Alternatives - **Alternative Approaches**: The benchmark specifically contrasts these two approaches, but there are other methods that could be considered in practice: - **Using a type check**: You could explicitly check the type using `typeof x === 'number'` to ensure the data type before checking for NaN. - **Custom NaN Checks**: Depending on the context, developers often create specialized functions that combine type checks with NaN checks, enhancing safety and readability. - **Performance Indicators**: The raw execution speed is important for performance-intensive applications. However, development overhead, team familiarity with specific functions, and code maintainability should also be considered when choosing between these options. In conclusion, the benchmark provides valuable insights into the performance differences between `isNaN` and `isNaNx`, with clear advantages for the latter due to lack of type coercion and the potential for reduced false positives, reflecting a more rigorous approach to checking NaN values in JavaScript.
Related benchmarks:
Set vs object
Set vs object
random String for loop vs while loop
String.split: Random vs Fixed strings
for vs find in 20
Compare TextEncoder, Blob, new TextEncoder
regex vs loop for word count
set.add test4
+ '' vs .toString() v2
Comments
Confirm delete:
Do you really want to delete benchmark?