Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Checking if number is integer
(version: 0)
Compares using num % 1 to number.isInteger
Comparing performance of:
Modulus 1 vs Number.isInteger
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var numbers = []; for(let i = 0; i < 100000;i++) { numbers.push(Math.random()*100000); numbers.push(Math.floor(Math.random()*100000)); } numbers.sort( (a,b) => (0.5)-Math.random());
Tests:
Modulus 1
let integerCount = 0; for(let i = 0; i < numbers.length;i++) { if(!(numbers[i]%1)) { integerCount+=1; } }
Number.isInteger
let integerCount = 0; for(let i = 0; i < numbers.length;i++) { if(Number.isInteger(numbers[i])) { integerCount+=1; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Modulus 1
Number.isInteger
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Modulus 1
288.9 Ops/sec
Number.isInteger
407.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and explain what's being tested. **Benchmark Definition:** The benchmark measures the performance of two approaches to check if a number is an integer: 1. Using the modulo operator (`%`) to check if the remainder of the division by 1 is zero. 2. Using the `Number.isInteger()` method, which returns `true` for integers and `false` otherwise. **Script Preparation Code:** The script generates two arrays of random numbers with a range of 0 to 100,000. The arrays are then sorted randomly using the `sort()` method with a custom comparison function. **Html Preparation Code:** There is no HTML preparation code provided for this benchmark. **Individual Test Cases:** There are two test cases: 1. "Modulus 1": This test case uses a simple loop to iterate through the generated numbers and checks if each number modulo 1 is zero. 2. "Number.isInteger": This test case also uses a loop to iterate through the generated numbers, but this time it uses the `Number.isInteger()` method to check if each number is an integer. **Library:** There are no external libraries used in these test cases, apart from the built-in JavaScript methods (`Math.random()`, `Math.floor()`, and `sort()`). **Special JS Feature or Syntax:** The only special feature used in these test cases is the custom comparison function passed to the `sort()` method. However, this is a standard JavaScript syntax and not specific to any particular library or module. Now, let's discuss the pros and cons of each approach: **Modulus 1:** Pros: * This approach is simple and easy to understand. * It doesn't require the use of any external libraries or methods. Cons: * It may be slower than other approaches due to the modulo operation being performed on each number, which can be costly for large numbers. * It's not as concise or expressive as using `Number.isInteger()`. **Number.isInteger:** Pros: * This approach is more concise and expressive than the modulus-based approach. * It's likely to be faster since it avoids the modulo operation and uses a specialized method designed for this purpose. Cons: * It may not work as expected for very large numbers due to numerical precision issues (e.g., `Number.isInteger(Infinity)` returns `false`). * It requires the use of an external library or module (in this case, the built-in JavaScript standard library). **Other Alternatives:** If you wanted to avoid using the `Number.isInteger()` method, you could implement your own integer check using bitwise operations, such as: ```javascript function isInteger(n) { return n === Math.floor(n); } ``` Alternatively, you could use a library like `big-integer` or `decimal.js` to perform arithmetic operations on integers and avoid numerical precision issues.
Related benchmarks:
Count sort vs JS native sort
javascript count sort vs native sort
Javascript Array sorting performance with sort() and reduce() take2
boolean comparator
Comments
Confirm delete:
Do you really want to delete benchmark?