Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
isNumber: regex vs isNaN vs string comparison (version: 1)
(version: 0)
Comparing performance of:
isNaN vs regex vs string comparison
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
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 (p >= '0' && p <= '9') { return +p } else { return Reflect.get(t, p, r) } } }); window.gabage1 = 0; window.gabage2 = 0; window.gabage3 = 0;
Tests:
isNaN
gabage1 =+ case1[_.random(0, 1e3, false)];
regex
gabage2 =+ case2[_.random(0, 1e3, false)];
string comparison
gabage3 =+ case3[_.random(0, 1e3, false)];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
isNaN
regex
string comparison
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 the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare three approaches for checking if a value is a number: 1. `regex` (Regular Expression) 2. `string comparison` 3. `isNaN` Each approach uses a proxy object (`window.case1`, `window.case2`, and `window.case3`) with a custom getter function that determines the truthiness of the value. **Approach 1: regex** * The `regex` approach uses a regular expression to check if the value is a number. The regex pattern `/^\\d+$/` matches any string that consists only of digits (`\d+`) from start to end (`^` and `$`). * The getter function returns the converted value as a number using `+p`, assuming the input value can be parsed as an integer. * Pros: Fast and efficient, especially for large numbers. * Cons: May not work correctly for very large or very small numbers due to precision issues in JavaScript. **Approach 2: string comparison** * The `string comparison` approach uses a simple string comparison to check if the value is a number. It checks if the input value is greater than or equal to '0' (inclusive) and less than or equal to '9' (inclusive). * The getter function returns the converted value as a number using `+p`. * Pros: Simple and easy to understand, but may be slower for very large numbers. * Cons: May not work correctly for very large or very small numbers due to precision issues in JavaScript. **Approach 3: isNaN** * The `isNaN` approach uses the built-in `isNaN()` function to check if the value is NaN (Not a Number). * The getter function returns the converted value as a number using `+p`. * Pros: Fast and efficient, especially for large numbers. * Cons: May not work correctly for very large or very small numbers due to precision issues in JavaScript. **Library** The benchmark uses Lodash.js library, which provides the `_` variable used in the getter function. `_random()` is a utility function that generates a random number between 0 and 1e3 (inclusive). **Special JS Feature/Syntax** This benchmark does not use any special JavaScript features or syntax. **Other Alternatives** If you wanted to implement this benchmark from scratch, you could use a different approach, such as: * Using `Number()` function to convert the value to a number. * Using `BigInt()` function for large numbers. * Implementing custom logic using bitwise operations or arithmetic manipulation. However, it's worth noting that Lodash.js provides optimized and tested implementations of these functions, making them a good choice for performance-critical code.
Related benchmarks:
isNumber: regex vs isNaN
isnan regex and typeof
isNumber: regex vs isNaN (version: 2)
isNumber: regex vs isNaN vs string comparison (version: 1) vs parseInt
Comments
Confirm delete:
Do you really want to delete benchmark?