Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
"isNumber" - isNaN vs + vs typeof
(version: 0)
Comparing performance of:
isNaN vs + vs typeof
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var k = "sadasljdaw";
Tests:
isNaN
eval(''); with({}); var r = !Number.isNaN(k);
+
eval(''); with({}); var r = +k !== NaN;
typeof
eval(''); with({}); var r = typeof k !== "number";
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
isNaN
+
typeof
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_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
isNaN
3968174.5 Ops/sec
+
3823263.5 Ops/sec
typeof
5415408.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Benchmark Definition** The benchmark measures the performance of three different approaches to check if a value is a number: 1. `isNaN(k)`: checks if the value is not a number (i.e., NaN, or "Not a Number") 2. `+k !== NaN`: converts the value to a number using unary `+` and then checks if it's not equal to NaN 3. `typeof k !== "number"`: uses the `typeof` operator to check if the value is of type "number" **Options Compared** The benchmark compares the performance of these three approaches in different scenarios. * `isNaN(k)`: a simple function call that directly checks if the value is not a number. * `+k !== NaN`: converts the value to a number using unary `+` and then checks if it's not equal to NaN. This approach has an additional overhead due to the conversion step. * `typeof k !== "number"`: uses the `typeof` operator to check if the value is of type "number". This approach also has an additional overhead due to the operator call. **Pros and Cons** * `isNaN(k)`: simple, direct, and fast, but may not work as expected for certain edge cases (e.g., NaN itself). * `+k !== NaN`: converts the value to a number, which can lead to precision issues, especially for non-integer values. However, this approach is widely used in JavaScript. * `typeof k !== "number"`: uses an operator that's not as performance-critical as function calls or unary conversions. **Library and Special JS Feature** There are no libraries mentioned in the benchmark definition. However, it's worth noting that the `Number.isNaN()` function was introduced in ECMAScript 2015 (ES6) to provide a safer alternative to `isNaN()`. This function checks if a value is NaN or not, without relying on the behavior of `isNaN()` for non-numeric values. The benchmark uses this function as an alternative to `isNaN(k)`. **Other Alternatives** In addition to the three approaches mentioned in the benchmark, other alternatives could include: * Using a regular expression (`^\d+$`) to check if a value is a number * Using a utility library like `lodash.isNumber()` or `moment.isNumber()` * Using a specialized function like `is-number` from `@stdlib/number-is` However, these alternatives are not mentioned in the benchmark definition. Overall, the benchmark provides a useful comparison of different approaches to check if a value is a number, allowing developers to make informed decisions about which approach to use depending on their specific requirements and performance constraints.
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?