Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Cache js regex
(version: 0)
Comparing performance of:
Not Cached vs Cached
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var regex = /^\d+$/; var regularTest = value => { const pass = /^\d+$/.test(value); return !pass; }; var cachedTest = value => { const pass = regex.test(value); return !pass; };
Tests:
Not Cached
regularTest(555);
Cached
cachedTest(555);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Not Cached
Cached
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.5
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Not Cached
57120920.0 Ops/sec
Cached
59338216.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that tests the performance of two functions, `regularTest` and `cachedTest`, which are used to check if a given string matches a specific regular expression. The regular expression in question is `^\\d+$`, which matches one or more digits from start to end. **Regular Test (`regularTest`)** The `regularTest` function takes a string as input, tests it against the regular expression using the `test()` method, and returns a boolean indicating whether the string does not match the pattern. This approach relies on the interpreter's ability to compile and execute the regular expression. Pros: * Simple implementation * Easy to understand Cons: * May incur additional overhead due to the interpretation of the regular expression * Can be slower for large inputs or complex patterns **Cached Test (`cachedTest`)** The `cachedTest` function uses a cached version of the regular expression, which is created by compiling it once and storing it in a variable. The function then uses this pre-compiled expression to test the input string. Pros: * Can be faster for repeated tests, as the compilation step is only performed once * Reduces interpreter overhead Cons: * Requires more memory allocation due to the cached regex object * May not be suitable for small inputs or simple patterns **Library Used** The `test()` method and regular expression syntax are part of the JavaScript standard library. The `regex` variable in the `cachedTest` function is a local variable created using the `new RegExp()` constructor, which allows us to compile and store the regular expression. **Other Considerations** * **Performance Impact**: For small inputs or simple patterns, the difference between the two approaches may be negligible. However, for larger inputs or more complex patterns, the cached version of `cachedTest` is likely to perform better. * **Memory Usage**: The cached version of `cachedTest` uses additional memory to store the pre-compiled regex object. This may not be a concern for small inputs but could become significant for very large datasets. * **Code Readability and Maintainability**: While both implementations are straightforward, the cached version of `cachedTest` might be considered less readable due to its use of internal state. **Alternatives** Some alternative approaches that could be explored include: 1. Using a dedicated regex engine or library like [regexninja](https://github.com/masfenix/regexninja) for more complex patterns. 2. Implementing the regular expression using a just-in-time (JIT) compiler or a native code generator to achieve better performance. 3. Using a caching mechanism that does not rely on internal state, such as a WeakMap-based cache. Please note that these alternatives might come with additional complexity and dependencies, which may not be suitable for all use cases.
Related benchmarks:
className const/cached/new regex, classList contains, className split includes
new RegExp test
new RegExp test 2
isNaN vs regex test for stringify number check
Comments
Confirm delete:
Do you really want to delete benchmark?