Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
isNumber: regex vs isNaN vs custom
(version: 0)
judge an object property key is a number
Comparing performance of:
isNaN vs regex vs custom
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.case1 = new Proxy({}, { get(t, p, r) { if (typeof p === 'string' && !isNaN(p)) { 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.case3 = new Proxy({}, { get(t, p, r) { if (typeof p === 'number' && !(!(p <= 0) && !(p > 0))) { return +p } else { return Reflect.get(t, p, r) } } }); window.gabage1 = 0; window.gabage2 = 0; window.gabage3 = 0;
Tests:
isNaN
for (let i = 0; i < 1e6;i++) { gabage1 =+ case1[i]; }
regex
for (let i = 0; i < 1e6;i++) { gabage2 += case2[i]; }
custom
for (let i = 0; i < 1e6;i++) { gabage3 += case3[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
isNaN
regex
custom
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):
Measuring the performance of different approaches to check if an object property key is a number is crucial, especially in JavaScript applications where this operation can be frequently encountered. The provided benchmark definition compares three approaches: 1. **`isNaN`**: The `isNaN` function checks if a value is not a number. It's implemented as a built-in JavaScript function and is likely to be optimized by the engine. 2. **Regular Expression (`regex`)**: A regular expression `/^\d+$/` tests if a string matches the pattern of one or more digits (`\d+`). The `+` operator converts the matched string to a number. 3. **Custom Approach (`custom`)**: This approach uses a Proxy (a technique for creating objects that can intercept and manipulate property access) to check if a property key is a number. It does this by checking the type of the property value using `typeof p === 'number'`, and then verifying that it's not a non-negative finite number (`!(p <= 0) && !(p > 0)`). **Pros and Cons:** * **`isNaN`**: * Pros: Lightweight, fast, and optimized by the engine. * Cons: May have issues with handling special numbers like NaN or Infinity. * **Regular Expression (`regex`)** * Pros: Flexible, can handle special characters, but might be slower due to regex engine overhead. * Cons: Can be slower than `isNaN`, and may require more resources for large inputs. * **Custom Approach (`custom`)**: * Pros: Highly customizable, can handle edge cases like special numbers, and potentially faster than `regex`. * Cons: Requires manual implementation of the Proxy setup, which might introduce overhead. **Special JavaScript Features and Syntax:** None mentioned in this benchmark. **Library Usage:** The `Proxy` object is used to create a custom approach that checks if a property key is a number. This is not a third-party library but rather a built-in JavaScript feature introduced in ECMAScript 2011 (ES5). In summary, the three approaches offer varying levels of customization and performance. The choice between them ultimately depends on the specific requirements of your application. **Alternative Approaches:** If you need more advanced handling or want to explore alternative methods, consider these options: * Using a library like `lodash.isNumber` (a lightweight utility function) or `moment.js` for date-related checks. * Implementing custom validation logic using regular expressions or other string manipulation techniques. * Utilizing engine-specific optimizations, such as the `Number()` constructor with a flag to prevent NaN values. Keep in mind that these alternatives might introduce additional overhead or require more resources. Be sure to benchmark and test them thoroughly before choosing an approach for your specific use case.
Related benchmarks:
Math.min vs if/else vs ternary operator
Math.min vs if/else vs ternary operator for Int
Math.min vs if/else vs ternary vs or operator
Math.min vs if/else vs ternary operator vs logical or
Math.min vs if/else vs ternary operator test 2
Comments
Confirm delete:
Do you really want to delete benchmark?