Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
typeof number vs. Number.isNan vs. isNan vs self comparison
(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
Tests:
typeof
typeof 5 === 'number'
Number.isNan
Number.isNaN(5)
isNaN
isNaN(5)
Self comparison
5 !== 5
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:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
typeof
1003869952.0 Ops/sec
Number.isNan
17585696.0 Ops/sec
isNaN
17496074.0 Ops/sec
Self comparison
1001263872.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmarking test case created on MeasureThat.net. The benchmark tests four different approaches to check if a value is a number: `typeof`, `Number.isNaN`, `isNaN`, and self-comparison using the `!==` operator. **Options Compared** 1. **`typeof 5 === 'number'`**: This approach uses the `typeof` operator in combination with a string comparison (`===`) to check if the value is a number. The `typeof` operator returns a string indicating the type of the variable, and by comparing it to `'number'`, we can determine if the value is indeed a number. 2. **`Number.isNaN(5)`**: This approach uses the `Number.isNaN()` function, which checks if a given value is NaN (Not a Number). 3. **`isNaN(5)`**: This approach uses the `isNaN()` function, which returns `true` if the value is NaN and `false` otherwise. 4. **`5 !== 5`**: This approach uses self-comparison with the `!==` operator to check if the value is not equal to itself (i.e., it's a number). **Pros and Cons of Each Approach** 1. **`typeof 5 === 'number'`**: * Pros: Fast and simple. * Cons: May be slower for non-numeric values, as `typeof` returns a string literal, which can lead to unnecessary overhead. 2. **`Number.isNaN(5)`**: This approach is not available in older browsers that don't support ECMAScript 2017 (ES7) standard. 3. **`isNaN(5)`**: * Pros: Similar speed to `typeof` for numeric values, as it only checks if the value is NaN. * Cons: May be slower for non-numeric values, as it's a separate function call. 4. **`5 !== 5`**: This approach is simple and fast, but it assumes that the value being compared is a number. **Library Used** None of these approaches rely on external libraries. **Special JS Feature or Syntax** There are no special features or syntax used in this benchmark. **Considerations** * For numeric values, all four approaches have similar performance. * For non-numeric values (e.g., strings), the `typeof` approach may be slower due to unnecessary string comparisons. * Older browsers may not support the `Number.isNaN()` and `isNaN()` functions. **Other Alternatives** If you need a more robust or modern way of checking if a value is a number, consider using: 1. **`Number` constructor**: Creates a new number object from a given value. 2. **`instanceof Number` operator**: Checks if an object is an instance of the `Number` class. However, these alternatives may not be supported in older browsers and may have different performance characteristics compared to the original approaches tested here.
Related benchmarks:
typeof number vs. Number.isNan vs. isNan
typeof vs typeof isNaN
typeof x === 'number' vs Number.isNaN(x)
typeof x === 'number' vs isNaN(x) rtestset22
typeof number vs. Number.isNan vs. isNan vs self comparison. Versus let
Comments
Confirm delete:
Do you really want to delete benchmark?