Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
isNumber: regex vs isNaN (version: 1)
(version: 0)
Comparing performance of:
isNaN vs regex
Created:
6 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.gabage1 = 0; window.gabage2 = 0;
Tests:
isNaN
gabage1 =+ case1[_.random(0, 1e3, false)];
regex
gabage2 =+ case2[_.random(0, 1e3, false)];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
isNaN
regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 137 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
isNaN
4483242.0 Ops/sec
regex
4064367.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and its corresponding test cases. **Benchmark Definition JSON:** The provided JSON defines two benchmark tests: 1. `isNumber: regex vs isNaN (version: 1)` 2. Two individual test cases (`gabage1 =+ case1[_.random(0, 1e3, false)];` and `gabage2 =+ case2[_.random(0, 1e3, false)];`) **Benchmark Description:** The benchmark is testing the performance difference between using a regular expression (`regex`) and the built-in `isNaN()` function to check if a value is a number. **Script Preparation Code:** The script preparation code defines two proxies (`case1` and `case2`) that will be used in the benchmark tests. The proxies are created with a custom getter function that checks for specific conditions: * For `case1`, it uses a string manipulation to check if the property value is a number (i.e., `+p`). If the value is not a string, it returns the original value. * For `case2`, it uses a regular expression (`^\\d+$`) to check if the property value is a number. If the value matches the regex pattern, it returns the converted number. The code also defines two garbage variables (`gabage1` and `gabage2`) with initial values of 0. **Html Preparation Code:** The HTML preparation code includes a reference to Lodash library (version 4.17.5) which is used in the benchmark tests. **Individual Test Cases:** The two test cases use the custom proxies (`case1` and `case2`) to access properties with random values between 0 and 1000 (inclusive). The test cases simply assign the accessed value to a variable (`gabage1` or `gabage2`) using the unary plus operator (`=+`). This is essentially testing the performance of accessing numbers using each proxy. **Library:** The Lodash library is used in the benchmark tests. Specifically, it's used with the `_` function, which returns a random value between 0 and 1000 (inclusive). **Special JS Feature/Syntax:** There are no specific JavaScript features or syntax mentioned in this benchmark setup. The focus is on testing the performance of accessing numbers using regular expressions and built-in `isNaN()` functions. **Pros and Cons of Different Approaches:** * **Regular Expressions (`regex`):** + Pros: - More expressive for string manipulation - Can be used to validate a wider range of input values + Cons: - Performance overhead due to regular expression engine - May not be as efficient as built-in `isNaN()` function for simple numeric checks * **Built-in `isNaN()` Function:** + Pros: - High performance and low latency - Built-in optimized implementation for simple numeric checks + Cons: - Limited expressiveness compared to regular expressions **Other Alternatives:** If you want to explore alternative approaches, consider the following: * Using a library like `regex-test` or `regex-perf` to test regular expression performance * Implementing your own custom numeric checking function using bitwise operators (e.g., `!(isNaN(x) && x)` ) * Using a different built-in function, such as `Number.isFinite()` or `Number.isInteger()`, for numeric checks Keep in mind that these alternatives might not provide the same level of expressiveness as regular expressions, but can still offer performance benefits for specific use cases.
Related benchmarks:
lodash isNil vs native isNil
lodash isNil vs ! Operator
lodash isNil vs native isNil with if
lodash isNil vs === null || === undefined
lodash.get vs optional chaining v2
Comments
Confirm delete:
Do you really want to delete benchmark?