Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Macro replacement
(version: 12)
Comparing performance of:
Regex replace vs String replace each macro separately vs Regex keys group vs Search for macro open and close delimiters vs Segments
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var html = ` <div> <div class="collapsed"> <div style="{{COLLAPSED_LAYOUT_1_BG_LAYER_STYLE}}"> </div> <div style="{{COLLAPSED_LAYOUT_1_LOGO_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_1_LOGO_IMG_SRC}}" style="{{COLLAPSED_LAYOUT_1_LOGO_IMG_STYLE}}" > </div> <div style="{{COLLAPSED_LAYOUT_1_P1_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_1_P1_IMG_SRC}}" style="{{COLLAPSED_LAYOUT_1_P1_IMG_STYLE}}" > </div> <div style="{{COLLAPSED_LAYOUT_1_P2_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_1_P2_IMG_SRC}}" style="{{COLLAPSED_LAYOUT_1_P2_IMG_STYLE}}" > </div> <div style="{{COLLAPSED_LAYOUT_1_BANNER_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_1_BANNER__SRC}}" style="{{COLLAPSED_LAYOUT_1_BANNER_IMG_STYLE}}" > </div> <div style="{{COLLAPSED_LAYOUT_1_MESSAGE_LAYER_STYLE}}"> </div> <div style="{{COLLAPSED_LAYOUT_1_PRICE_LAYER_STYLE}}"> </div> <div style="{{COLLAPSED_LAYOUT_1_BUTTON_LAYER_STYLE}}"> </div> </div> <div class="expanded"> <div style="{{EXPANDED_LAYOUT_1_BG_LAYER_STYLE}}"> </div> <div style="{{EXPANDED_LAYOUT_1_LOGO_LAYER_STYLE}}"> <img src="{{EXPANDED_LAYOUT_1_LOGO_IMG_SRC}}" style="{{EXPANDED_LAYOUT_1_LOGO_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_1_BANNER_LAYER_STYLE}}"> <img src="{{EXPANDED_LAYOUT_1_BANNER__SRC}}" style="{{EXPANDED_LAYOUT_1_BANNER_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_1_MESSAGE_1_LAYER_STYLE}}"> </div> <div style="{{EXPANDED_LAYOUT_1_MESSAGE_2_LAYER_STYLE}}"> </div> <div style="{{EXPANDED_LAYOUT_1_BUTTON_LAYER_STYLE}}"> </div> <div class="galleryController"> <button>PREV</button> </div> <div class="galleryContainer"> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G1_P1_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G1_P1_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G1_P1_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G1_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G2_P2_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G2_P2_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G2_P2_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G2_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G3_P3_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G3_P3_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G3_P3_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G3_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G4_P4_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G4_P4_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G4_P4_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G4_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G5_P5_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G5_P5_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G5_P5_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G5_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G6_P6_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G6_P6_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G6_P6_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G6_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G7_P7_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G7_P7_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G7_P7_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G7_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G8_P8_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G8_P8_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G8_P8_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G8_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G9_P9_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G9_P9_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G9_P9_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G9_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G10_P10_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G10_P10_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G10_P10_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G10_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G11_P11_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G11_P11_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G11_P11_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G11_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G12_P12_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G12_P12_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G12_P12_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G12_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G13_P13_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G13_P13_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G13_P13_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G13_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G14_P14_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G14_P14_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G14_P14_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G14_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G15_P15_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G15_P15_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G15_P15_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G15_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G16_P16_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G16_P16_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G16_P16_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G16_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G17_P17_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G17_P17_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G17_P17_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G17_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G18_P18_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G18_P18_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G18_P18_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G18_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G19_P19_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G19_P19_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G19_P19_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G19_MESSAGE_LAYER_STYLE}}"> </div> </div> <div class="galleryItem"> <div style="{{EXPANDED_LAYOUT_G20_P20_LAYER_STYLE}}"> <img src="{{COLLAPSED_LAYOUT_G20_P20_IMG_SRC}}" style="{{EXPANDED_LAYOUT_G20_P20_IMG_STYLE}}" > </div> <div style="{{EXPANDED_LAYOUT_G20_MESSAGE_LAYER_STYLE}}"> </div> </div> </div> <div class="galleryController"> <button>NEXT</button> </div> </div> </div> `; var keyList = html.match(/{{.*}}/g).map(s => s.replace(/({{|}})/g, "")); var data = keyList.reduce((acc, key) => { if (key.endsWith('STYLE')) { acc[key]= `width: 30px;height: 30px; position: absolute; top: 0px; left: 0px; border: none; padding: 0 0 0 0; margin: 0 0 0 0; backround: black;` } else if (key.endsWith('SRC')) { acc[key]='https://google.com.whocares.what?!.ok'; } return acc; }, {}); var segementList = html.split(/{{|}}/g).map((segment, index) => { if (index % 2 === 0) { return { type: 'string', value: segment, } } return { type: 'macro', value: segment, } })
Tests:
Regex replace
html.replace(/{{(.*?)}}/g, (_, key) => data[key]);
String replace each macro separately
let newHtml = html; Object.keys(data).forEach(key => newHtml = newHtml.replace(`{{${key}}}`, data[key]))
Regex keys group
let newHtml = html.replace(new RegExp(`{{(${Object.keys(data).join('|')})}}`, 'g'), (_, key, g) => { return data[key]; })
Search for macro open and close delimiters
let newHtml = ''; let originalHtml = html; while (originalHtml) { const indexUntilOpenMacro = originalHtml.search('{{'); if (indexUntilOpenMacro >= 0) { newHtml += originalHtml.substring(0, indexUntilOpenMacro); originalHtml = originalHtml.substring(indexUntilOpenMacro + 2); const indexUntilCloseMacro = originalHtml.search('}}'); newHtml += data[originalHtml.substring(0, indexUntilCloseMacro)]; originalHtml = originalHtml.substring(indexUntilCloseMacro + 2); } else { break; } }
Segments
segementList .map(segment => { if (segment.type === 'string') { return segment.value; } return data[segment.value]; }) .join('');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Regex replace
String replace each macro separately
Regex keys group
Search for macro open and close delimiters
Segments
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):
It looks like we have some benchmark results! To provide a helpful answer, I'll need to analyze the data and identify which benchmark performed best. The top-performing benchmark is: **Segments** ExecutionsPerSecond: 86198.53125 RawUAString: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" Browser: "Chrome 109" DevicePlatform: "Desktop" OperatingSystem: "Mac OS X 10.15.7" This benchmark is likely the fastest because it's most efficient in terms of execution time. The other benchmarks performed relatively poorly, but some showed promise: **Regex replace**: This benchmark was close to **Segments**, with an ExecutionPerSecond of 77833.671875. **Regex keys group**: This benchmark trailed behind the others, with an ExecutionPerSecond of 33043.62109375. **Search for macro open and close delimiters**: This benchmark performed poorly, with an ExecutionPerSecond of 14924.220703125. **String replace each macro separately**: This benchmark was slower than **Segments**, but faster than the last two, with an ExecutionPerSecond of 5809.52392578125. Keep in mind that these results are specific to this particular benchmarking setup and may not be representative of real-world performance. How can I assist you further?
Related benchmarks:
afdgdfdg2fsn1hjbzzz
querySelectorAll vs closest
querySelectorAll vs closest vs closest polyfill
querySelect speed 2
Comments
Confirm delete:
Do you really want to delete benchmark?