Skip to content

Instantly share code, notes, and snippets.

@hakatashi
Created December 17, 2018 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakatashi/8d7f724e2572a6e12cdbd3ec363c523f to your computer and use it in GitHub Desktop.
Save hakatashi/8d7f724e2572a6e12cdbd3ec363c523f to your computer and use it in GitHub Desktop.
RegExp benchmark of some cases in Node.js
const tests = Array(10000).fill().map(() => (
// Array(10).fill().map(() => sample(Array.from('アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン'))).join('')
Array(10).fill().map(() => sample(Array.from('0123456789'))).join('')
));
console.time('empty');
for (let i = 0; i < 1e8; i++) ;
console.timeEnd('empty');
console.time('case1');
for (let i = 0; i < 1e8; i++) {
tests[i % 10000].match(/^(?:1(?:1(?:1|2|3)|2(?:1|2|3)|3(?:1|2|3))|2(?:1(?:1|2|3)|2(?:1|2|3)|3(?:1|2|3))|3(?:1(?:1|2|3)|2(?:1|2|3)|3(?:1|2|3)))/);
}
console.timeEnd('case1');
console.time('case2');
for (let i = 0; i < 1e8; i++) {
tests[i % 10000].match(/^(1(1(1|2|3)|2(1|2|3)|3(1|2|3))|2(1(1|2|3)|2(1|2|3)|3(1|2|3))|3(1(1|2|3)|2(1|2|3)|3(1|2|3)))/);
}
console.timeEnd('case2');
console.time('case3');
for (let i = 0; i < 1e8; i++) {
tests[i % 10000].match(/^(?:111|112|113|121|122|123|131|132|133|211|212|213|221|222|223|231|232|233|311|312|313|321|322|323|331|332|333)/);
}
console.timeEnd('case3');
console.time('case4');
for (let i = 0; i < 1e8; i++) {
'314159265900000'.match(/^(3141592650|3141592651|3141592652|3141592653|3141592654|3141592655|3141592656|3141592657|3141592658|3141592659)/);
}
console.timeEnd('case4');
console.time('case5');
for (let i = 0; i < 1e8; i++) {
'314159265900000'.match(/^(314159265(?:0|1|2|3|4|5|6|7|8|9))/);
}
console.timeEnd('case5');
console.time('case6');
for (let i = 0; i < 1e8; i++) {
'314159265900000'.match(/^(3141592650|1414213560|3141592651|1414213561|3141592652|1414213562|3141592653|1414213563|3141592654|1414213564|3141592655|1414213565|3141592656|1414213566|3141592657|1414213567|3141592658|1414213568|3141592659|1414213569)/);
}
console.timeEnd('case6');
console.time('case7');
for (let i = 0; i < 1e8; i++) {
'314159265900000'.match(/^(314159265(?:0|1|2|3|4|5|6|7|8|9)|141421356(?:0|1|2|3|4|5|6|7|8|9))/);
}
console.timeEnd('case7');
empty: 131.988ms
case1: 3782.656ms
case2: 6568.509ms
case3: 3711.207ms
case4: 8385.808ms
case5: 8676.228ms
case6: 8623.019ms
case7: 8782.216ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment