Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regexp creation vs memoization
(version: 7)
I need to obtain and use regexps from a string. I'm testing wether I should cache regexps or just construct them on demand.
Comparing performance of:
constructor vs memoize vs common boilerplate
Created:
8 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var cache = {} const regexps = [ /^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))*$/, /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|622((12[6-9]|1[3-9][0-9])|([2-8][0-9][0-9])|(9(([0-1][0-9])|(2[0-5]))))[0-9]{10}|64[4-9][0-9]{13}|65[0-9]{14}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})*$/, /[^@/]+@[^@/]+/, /\\s+/, /[^a-zA-Z0-9]/, /^$/, /^[1-9]+[0-9]*$/, /(^\d*\.?\d*[0-9]+\d*$)|(^[0-9]+\d*\.\d*$)/, /^-?[0-9]{0,2}(\.[0-9]{1,2})?$|^-?(100)(\.[0]{1,2})?$/, ] const sources = regexps.map(r => r.source) regexps.forEach(r => { cache[r.source] = r }) const n = sources.length const floor = Math.floor const random = Math.random function getRandomSource () { return sources[floor(random() * n)] }
Tests:
constructor
window.result = new RegExp(getRandomSource())
memoize
window.result = cache[getRandomSource()]
common boilerplate
window.result = getRandomSource()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
constructor
memoize
common boilerplate
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 what's being tested in this benchmark. **Benchmark Definition** The test compares two approaches to creating regular expressions (regexps) in JavaScript: 1. **Constructor**: This approach creates a new regexp object every time it's needed, without caching the results. 2. **Memoization**: This approach caches the compiled regexps in an object (`cache`) and reuses them instead of recreating them from scratch. **Options Compared** The two options being compared are: * Creating a new regexp object from scratch (constructor) * Caching the compiled regexp objects (memoization) **Pros and Cons of Each Approach** ### Constructor Pros: * Easy to implement * No overhead in terms of memory usage or performance impact on first use Cons: * Creates a new regexp object every time it's needed, which can be inefficient if the same regexp is used multiple times. * Can lead to performance issues if the number of regexps grows large. ### Memoization Pros: * Reuses cached compiled regexps, reducing memory usage and improving performance for repeated use. * Reduces the overhead of creating a new regexp object from scratch. Cons: * Requires additional memory to store the cache. * May increase memory usage if the cache grows too large. **Library Used** In this benchmark, no external libraries are used. The regexps are defined directly in the JavaScript code. **Special JS Feature/Syntax (None)** There are no special JavaScript features or syntaxes being tested in this benchmark. **Other Considerations** When deciding between these two approaches, consider the following: * If you expect to use the same regexp multiple times, memoization might be a better choice. * If you're working with a large number of unique regexps, memoization could help reduce memory usage and improve performance. * If simplicity is more important than optimization, the constructor approach might be sufficient. **Alternatives** Other approaches to creating regexps in JavaScript include: * Using a library like RegExCache or RegExp- cache to cache compiled regexps. * Using a JavaScript engine that supports Just-In-Time (JIT) compilation of regexps, which can improve performance. * Using a different programming language or framework that provides built-in support for caching and reusing compiled regexps.
Related benchmarks:
regex 0001 + 4
split1 vs regex
RegExp constructor vs literal (re-do creation)
Regex tests Dani
Comments
Confirm delete:
Do you really want to delete benchmark?