This solution uses the vm module in Node.js to run a script in a separate context, which allows for better performance and avoids some of the issues encountered with using worker threads. The vm module provides a built-in timeout option, which can be used to limit the execution time of the script. By creating a closure that captures the regex and tag variables, the script can be executed multiple times without having to recreate the context and script every time. The solution has been benchmarked and shows better performance than using worker threads or RE2 objects, with a significant improvement in throughput when a timeout is added.