Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
strict equality, Number.isNan vs. isNan
(version: 0)
Comparing performance of:
Number.isNaN vs isNan vs isNaN field vs strict inequality
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var myIsNaN = Number.isNaN; var x = Math.random() > 0.5 ? Math.random() : NaN;
Tests:
Number.isNaN
Number.isNaN(x)
isNan
isNaN(x)
isNaN field
myIsNaN(x)
strict inequality
x !== x
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Number.isNaN
isNan
isNaN field
strict inequality
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Number.isNaN
11403118.0 Ops/sec
isNan
11375462.0 Ops/sec
isNaN field
15775912.0 Ops/sec
strict inequality
15809571.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and discussed. **Overview** The benchmark is designed to compare the performance of three different ways to check for NaN (Not a Number) values in JavaScript: `Number.isNaN()`, `isNaN()`, and a custom implementation `myIsNaN`. The benchmark also includes two additional tests: `strict inequality` (`x !== x`) and `Number.isNaN(x)`. **Library** The library being used is the built-in `Number` object, which provides several methods for working with numbers in JavaScript. Specifically, `Number.isNaN()` is a method that returns `true` if a number is NaN, while `isNaN()` is an alias for `Number.isNaN()`. **Custom implementation** The custom implementation `myIsNaN(x)` is assigned to a variable `myIsNaN` and then used in the benchmark. This suggests that the test is verifying that this custom implementation has similar performance characteristics as the built-in `Number.isNaN()` method. **Special JS feature or syntax** None of the tests specifically use any special JavaScript features or syntax beyond what's typically available in most modern JavaScript engines. **Test cases and comparison** The benchmark consists of four test cases: 1. `Number.isNaN(x)`: This test measures the performance of the built-in `Number.isNaN()` method. 2. `isNaN(x)`: This test is an alias for `Number.isNaN()`, so it's essentially measuring the same performance as the first test case. 3. `myIsNaN(x)`: This test measures the performance of the custom implementation `myIsNaN`. 4. `x !== x`: This test is not directly related to NaN values, but rather checks for strict inequality between a number and itself. However, in some cases, this can lead to unexpected behavior when dealing with NaN values. The benchmark appears to be comparing the performance of these four different approaches to checking for NaN values on a modern JavaScript engine (specifically, Chrome 123). **Pros and cons** Here are some pros and cons associated with each approach: * `Number.isNaN()`: Pros: widely available, well-documented, and optimized for performance. Cons: may be slower in some cases due to the overhead of method calls. * `isNaN()`: Pros: simple alias for `Number.isNaN()`, so it's essentially identical performance-wise. Cons: not as explicit or readable as using `Number.isNaN()` directly. * `myIsNaN(x)`: Pros: custom implementation may be optimized for specific use cases, potentially leading to better performance. Cons: requires manual maintenance and debugging, which can be error-prone. **Other alternatives** Other alternatives for checking NaN values in JavaScript include: * Using the `Object.is()`, which is a more modern method that can detect NaN values without relying on `Number` methods. * Implementing a custom NaN check using bitwise operations or other techniques. However, these alternatives are not part of the provided benchmark and may have different performance characteristics or trade-offs.
Related benchmarks:
NaN speed
Native isNaN() vs Custom isNaN method
Number constructor vs double tilde
typeof x === 'number' vs isNaN(x) rtestset22
Comments
Confirm delete:
Do you really want to delete benchmark?