Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Checking numbers (non signed integers): "Number.isFinite()" vs "!== null" (equal to null)
(version: 0)
Comparing performance of:
Number.isFinite() vs !== null
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var numbersCount = 10000; var numbers = Array.from({ length: numbersCount }).map(() => Math.round(Math.random() * numbersCount));
Tests:
Number.isFinite()
numbers.filter((number) => Number.isFinite(number));
!== null
numbers.filter((number) => number !== null);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Number.isFinite()
!== null
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Main Goal:** The main goal of this benchmark is to compare the performance of two approaches for filtering out non-finite numbers from an array: 1. Using `Number.isFinite()` (a JavaScript method) 2. Using `!== null` (a loose equality check) **Options Compared:** * **Number.isFinite()**: This method checks if a number is finite, i.e., it's neither positive nor negative infinity, nor NaN (Not a Number). It returns a boolean value (`true` for finite numbers and `false` otherwise). * **!== null**: This is a loose equality check that compares the value to `null`. In JavaScript, `null` represents an uninitialized or non-existent value. When comparing values using `===` or `!==`, JavaScript uses strict equality checks (i.e., type coercion). Therefore, this approach doesn't actually filter out non-finite numbers, as NaN is not considered equal to null. **Pros and Cons:** * **Number.isFinite():** + Pros: - Correctly filters out non-finite numbers - Returns a boolean value for clarity + Cons: - May be slower due to the extra function call - Might not be as efficient on very large datasets * **!== null:** + Pros: - Faster, since it's just a simple comparison + Cons: - Incorrectly filters out non-finite numbers (NaN is not equal to null) - Returns a boolean value, which might lead to confusion **Library and Purpose:** There are no external libraries used in this benchmark. The only library mentioned is `Math`, which provides mathematical functions like `round()`. **Special JS Feature/Syntax:** None of the approaches rely on special JavaScript features or syntax. Both methods use standard JavaScript operations. **Other Alternatives:** If you're looking for alternative ways to filter out non-finite numbers, you could consider using: * `Number.EPSILON`: This is a very small value that represents the difference between 1 and zero in the IEEE floating-point representation of numbers. You can use this value to check if a number is close enough to finite. * `isNaN()`: This function returns `true` if its argument is NaN, which means it's not a number. However, these alternatives might not be as straightforward or efficient as using `Number.isFinite()` or `!== null`.
Related benchmarks:
Number to String
parseFloat
Math.sqrt(x) vs x**0.5
parse float
Comments
Confirm delete:
Do you really want to delete benchmark?