Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
typeof number vs. Number.isNan vs. isNan vs self comparison. Versus let
(version: 0)
Comparing performance of:
typeof vs Number.isNan vs isNaN vs Self comparison
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = 5
Tests:
typeof
typeof a === 'number'
Number.isNan
Number.isNaN(a)
isNaN
isNaN(a)
Self comparison
a !== a
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
typeof
Number.isNan
isNaN
Self comparison
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 133 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
typeof
10999733.0 Ops/sec
Number.isNan
10217771.0 Ops/sec
isNaN
10064553.0 Ops/sec
Self comparison
9888287.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested, compared, and considered in this JavaScript microbenchmark. **Benchmark Definition** The benchmark measures the performance of four different approaches to compare the value `a` with itself: 1. `typeof a === 'number'` 2. `Number.isNaN(a)` 3. `isNaN(a)` 4. `a !== a` (self-comparison) These tests are compared to determine which one is the most efficient. **Options Compared** The options being compared have pros and cons: * `typeof a === 'number'`: This approach checks if the type of `a` is `'number'`, which can be expensive due to the overhead of the `typeof` operator. However, it's a straightforward and concise way to check. * `Number.isNaN(a)`: This function is specifically designed to test for NaN (Not a Number) values. It's more efficient than `typeof` because it uses an internal optimization to avoid unnecessary checks. On the other hand, it only checks for NaN, not all number types. * `isNaN(a)`: This function checks if the value of `a` is NaN. While it's faster than `Number.isNaN`, it has a broader scope and may return incorrect results for non-NaN numbers. * `a !== a`: This self-comparison approach is the simplest and most efficient, as it only requires comparing the value with itself. However, it may not be suitable for all use cases, especially if you're working with complex data structures. **Library Usage** In this benchmark, the `Number.isNaN` function from the JavaScript standard library is used to test for NaN values. This function is specifically designed to provide a fast and efficient way to check for NaNs. **Special JS Feature/Syntax** There are no special JavaScript features or syntax being tested in this benchmark. The tests only use built-in functions and variables. **Other Considerations** * The benchmark is running on a Chrome browser with version 120, which may affect the results due to changes in the engine's behavior. * The executions per second (EPS) values indicate the number of times each test was executed within a second. A higher EPS value typically indicates better performance. * The `DevicePlatform` and `OperatingSystem` fields are likely used for device-specific optimizations or statistical analysis. **Alternatives** If you were to implement this benchmark yourself, some alternative approaches could be: * Using `Number.EPSILON` instead of `isNaN(a)` to check for very small values * Implementing a custom NaN test using bitwise operations (e.g., `a === 0 && a + 1 !== a`) * Adding additional tests for other number types, such as BigInt or complex numbers * Optimizing the benchmark for specific use cases or browsers Keep in mind that this is just a microbenchmark, and the results may not be representative of real-world performance. The goal is to provide insights into the relative efficiency of different approaches, rather than to optimize performance for a particular use case.
Related benchmarks:
typeof number vs. Number.isNan vs. isNan
typeof x === 'number' vs isNaN(x) avi
typeof x === 'number' vs Number.isNaN(x)
typeof x === 'number' vs isNaN(x) rtestset22
Comments
Confirm delete:
Do you really want to delete benchmark?