Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
isInteger: regex vs isInteger
(version: 0)
judge an object property key is a number
Comparing performance of:
isNumber vs regex
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.case1 = new Proxy({}, { get(t, p, r) { if (Number.isInteger(p) && parseInt(p) > 0) { return +p } else { return Reflect.get(t, p, r) } } }); window.case2 = new Proxy({}, { get(t, p, r) { if (/^\d+$/.test(p)) { return +p } else { return Reflect.get(t, p, r) } } }); window.gabage1 = 0; window.gabage2 = 0;
Tests:
isNumber
for (let i = 0; i < 1e6;i++) { gabage1 =+ case1[i]; }
regex
for (let i = 0; i < 1e6;i++) { gabage2 += case2[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
isNumber
regex
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/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
isNumber
4.7 Ops/sec
regex
4.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmarking test case. **Overview** The test case compares two approaches to check if an object property key is a number: using `Number.isInteger()` and regular expressions (`/^\\d+$/`). **Approaches Compared** 1. **`Number.isInteger()`**: This method checks if a value is an integer, i.e., the number part of a float, without any fractional part. It returns `true` for integers like 42, -34, and 0. 2. **Regular Expression (`/^\\d+$/`)**: This regular expression pattern matches one or more digits (`\\d+`) at the start (`^`) and end (`$`) of a string. If the entire string consists only of digits, the test returns `true`. **Pros and Cons** 1. **`Number.isInteger()`**: * Pros: + More intuitive for checking integer values. + Less memory-intensive due to fewer function calls. * Cons: + May not work correctly with negative integers or zero (returns `false`). 2. **Regular Expression (`/^\\d+$/`)**: * Pros: + Works correctly with negative integers and zero. + More flexible than `Number.isInteger()` for checking integer values. * Cons: + Less readable due to the use of regular expressions. + May be slower due to the overhead of regex processing. **Library Used** None in this case, as both approaches rely on built-in JavaScript methods and functions. **Special JS Features/Syntax** The test case uses a few modern features: 1. **Arrow Functions**: The `get()` method's callback function is defined using an arrow function (`(t, p, r) => ...`). 2. **Proxy Objects**: A proxy object is created to modify the behavior of property access. 3. **Template Literals**: Template literals are used in the HTML preparation code (`window.gabage1 = 0;`, `window.gabage2 = 0;`). **Other Alternatives** For this specific use case, other alternatives could be: 1. **`typeof` operator**: Checking if a value is an integer using `typeof p === 'number' && Number.isInteger(p)` would be equivalent to the `Number.isInteger()` method. 2. **Regular Expression (`/^[+-]?\d+$/`)`: This regular expression pattern matches one or more digits, optionally preceded by a sign (`+` or `-`).
Related benchmarks:
isNumber: regex vs isNaN
isNumber: regex vs isNaN with cached regex
isNumber: regex vs isNaN vs custom
Number vs Regex
Comments
Confirm delete:
Do you really want to delete benchmark?