Skip to content

Instantly share code, notes, and snippets.

@panzi

panzi/Results.txt

Last active Aug 3, 2018
Embed
What would you like to do?
Find maximum regular expression length. Note that I guess the maximum length could be different if other regular expression features are used.
var upper = 0x7fffffff; // maximum signed 32 bit integer
var lower = 0;
while (lower + 1 < upper) {
var index = ((lower + upper) / 2)|0;
try {
new RegExp(new Array(index + 1).join('A'));
lower = index;
console.log(index + ' is ok');
} catch(e) {
console.log(index + ' is too big (' + e.constructor.name + ')');
upper = index;
}
}
console.log('maximum regular expression size is ' + lower + ' ASCII character');
var script = '\n' +
'self.onmessage = function (event) {\n' +
'if (event.data === "start") {\n' +
'var upper = 0x7fffffff; // maximum signed 32 bit integer\n' +
'var lower = 0;\n' +
'\n' +
'while (lower + 1 < upper) {\n' +
' var index = ((lower + upper) / 2)|0;\n' +
' try {\n' +
' new RegExp(new Array(index + 1).join("A"));\n' +
' lower = index;\n' +
' self.postMessage({size:index, ok:true});\n' +
' } catch(e) {\n' +
' self.postMessage({size:index, ok:false, error: e.constructor.name});\n' +
' upper = index;\n' +
' }\n' +
'}\n' +
'self.postMessage({size:lower, ok:true, done:true});\n' +
'}};\n';
var blob = new Blob([script], {type:'text/javascript'});
var worker = new Worker(window.URL.createObjectURL(blob));
worker.onmessage = function(event) {
if (event.data.done) {
console.log('maximum regular expression size is ' + event.data.size + ' ASCII character');
} else if (event.data.ok) {
console.log(event.data.size + ' is ok');
} else {
console.log(event.data.size + ' is too big (' + event.data.error + ')');
}
};
worker.postMessage('start');
Google Chrome 68.0.3440.84 64 bit Linux: 41943040
NodeJS v8.11.1 64 bit Linux: 41943040
Mozilla Firefox 61.0.1 64 bit Linux: 268435455
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.