regexp vs indexOf (v31)

Revision 31 of this benchmark created on


Description

Checking the performance cost of using a regexp instead of String#indexOf.

Preparation HTML

<div id="foo" class="a foo bar"></div>
\n `\n\n\tconst toString = JSON.string\n function dynamicRegExp(node) {\n return RegExp(/width\\s*:\\s*(\\d+)\\s*px/i).test(tag);\n }\n \n function inlineRegExp(node) {\n return /width\\s*:\\s*(\\d+)\\s*px/i.test(tag);\n }\n \n function storedRegExp(node) {\n return reContains.test(tag);\n }\n \n function stringIndexOf(node) {\n return (tag).indexOf(width) > -1;\n }","version":"31"}

Setup

var r;
    var element = document.getElementById('foo');
    var reContains = /width\s*:\s*(\d+)\s*px/i;

var tag = `<ins class='apsads'
   style='display:inline-block;width:300px;height:250px'
   data-aps-placement='YOUR_APS_PLACEMENT_ID'
   data-aps-rendering-mode='script'
   data-aps-click-tracker='\${CLICK_URL}'>
   <script id="foo" src='https://z-na.amazon-adsystem.com/widgets/dfp/q?placement=YOUR_APS_PLACEMENT_ID'></script>
  </ins>`

	const toString = JSON.string
    function dynamicRegExp(node) {
      return RegExp(/width\s*:\s*(\d+)\s*px/i).test(tag);
    }
    
    function inlineRegExp(node) {
      return /width\s*:\s*(\d+)\s*px/i.test(tag);
    }
    
    function storedRegExp(node) {
      return reContains.test(tag);
    }
    
    function stringIndexOf(node) {
      return (tag).indexOf(width) > -1;
    }
\n `\n\n\tconst toString = JSON.string\n function dynamicRegExp(node) {\n return RegExp(/width\\s*:\\s*(\\d+)\\s*px/i).test(tag);\n }\n \n function inlineRegExp(node) {\n return /width\\s*:\\s*(\\d+)\\s*px/i.test(tag);\n }\n \n function storedRegExp(node) {\n return reContains.test(tag);\n }\n \n function stringIndexOf(node) {\n return (tag).indexOf(width) > -1;\n }","version":"31"}

Test runner

Ready to run.

Testing in
TestOps/sec
dynamic regexp
r = dynamicRegExp(element);
ready
inline regexp
r = inlineRegExp(element);
ready
stored regexp
r = storedRegExp(element);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.