Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
isNumber: regex vs isNaN with cached regex
(version: 0)
judge an object property key is a number
Comparing performance of:
isNaN vs regex
Created:
4 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) } } }); const regex = /^\d+$/ window.case2 = new Proxy({}, { get(t, p, r) { if (regex.test(p)) { return +p } else { return Reflect.get(t, p, r) } } }); window.gabage1 = 0; window.gabage2 = 0;
Tests:
isNaN
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
isNaN
regex
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 its components. **Overview** The provided JSON represents a JavaScript microbenchmarking test case on MeasureThat.net. The test compares two approaches to determine if an object property key is a number: using the `isNaN` function versus using a regular expression (`regex`) with caching. **Script Preparation Code** In this code, we create two Proxies: 1. `window.case1`: A Proxy that returns the value of a string property only if it's a number (using `+p`). Otherwise, it delegates to `Reflect.get(t, p, r)`. 2. `window.case2`: Another Proxy that uses a cached regular expression (`regex = /^\\d+$/`) to test if a string property is a number. If the regex matches, it returns the value of the property; otherwise, it delegates to `Reflect.get(t, p, r)`. The code also initializes two variables, `gabage1` and `gabage2`, which are used as counters in the benchmark tests. **Individual Test Cases** There are two test cases: 1. `"isNaN"`: This test uses a simple loop that iterates from 0 to 1 million (`i = 0; i < 1e6`) and assigns the value of `case1[i]` to `gabage1`. The test measures the time it takes to execute this loop. 2. `"regex"`: Similar to the first test, but uses `case2[i]` instead of `case1[i]`, which is cached with a regular expression. **Library and Purpose** In both tests, we use the built-in `Reflect` object, which provides methods for working with objects, such as `get`. We also use the `Proxy` constructor to create custom access controllers for our properties. The purpose of using these libraries is to provide a way to dynamically check if an object property key is a number. **Special JS Feature or Syntax** This benchmark does not explicitly use any special JavaScript features or syntax, such as async/await, promises, or ES6 classes. **Pros and Cons of Different Approaches** Using `isNaN`: Pros: * Simple and straightforward * Fast execution (since it's a built-in function) Cons: * May be slower for non-numeric strings due to the check Using regular expressions (`regex`): Pros: * Can handle numeric strings more efficiently than `isNaN` * Can be faster since it's just a string test Cons: * May require more memory and processing power * Less straightforward implementation compared to `isNaN` **Other Alternatives** If you want to implement this benchmark yourself, here are some alternatives: 1. Use a more efficient way to check if a value is a number, such as using `Number.isFinite` or `typeof`. 2. Experiment with different caching strategies for the regular expression (`regex`). 3. Measure the performance of both approaches on different devices and browsers. Keep in mind that these alternatives might not provide exactly the same results as the original benchmark, but they can help you understand the trade-offs involved in using `isNaN` versus a regular expression for this specific use case.
Related benchmarks:
isNumber: regex vs isNaN
isNumber: regex vs isNaN (version: 2)
isInteger: regex vs isInteger
isNumber: regex vs isNaN vs custom
Comments
Confirm delete:
Do you really want to delete benchmark?