Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
replaceAll vs regex replace vs neu parser
(version: 0)
Totally curious on this.
Comparing performance of:
replace regex vs replace All vs Neu parser
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
String.prototype.replaceAll = function(search, replacement) { var target = this; return target.replace(new RegExp(search, 'g'), replacement); }; function parse(str) { const result = []; let buf = ''; for (let i = 0; i < str.length; i++) { const c = str.charAt(i); switch (c) { case '{': if (buf.length === 0 || buf === '{') { buf += c; } else { throw new Error(`invalid '{' at ${i}`); } break; case '}': if (buf.length > 3) { buf += c; if (buf[buf.length - 2] === '}') { result.push([buf.substr(2, buf.length - 4), i - buf.length + 1, i + 1]); buf = ''; } } else { throw new Error(`invalid '}' at ${i}`); } break; case ' ': if (buf.length > 0) { throw new Error(`invalid ' ' at ${i}`); } break; default: if (buf.length > 1) { buf += c; } } } if (buf.length && buf[0] === '{') { throw new Error('unmatched brace'); } return result; } function printf(str, varMap, vars) { let result = '' + str; let i = varMap.length; while (i--) { const [key, start, end] = varMap[i]; // eslint-disable-next-line no-prototype-builtins if (vars.hasOwnProperty(key)) { result = result.slice(0, start) + vars[key] + result.slice(end); } } return result; }
Tests:
replace regex
const template = '/nns/V1/event/offeringid/{{offeringid}}?division={{division}}&vodId={{vodId}}&lineup={{lineupId}}&profile={{nnsProfile}}&cacheID={{cacheID}}'; template.replace(/{{offeringid}}/g, "1"); template.replace(/{{division}}/g, "2"); template.replace(/{{vodId}}/g, "3"); template.replace(/{{lineupId}}/g, "4"); template.replace(/{{nnsProfile}}/g, "5"); template.replace(/{{cacheID}}/g, "6");
replace All
const template = '/nns/V1/event/offeringid/{{offeringid}}?division={{division}}&vodId={{vodId}}&lineup={{lineupId}}&profile={{nnsProfile}}&cacheID={{cacheID}}'; template.replaceAll("{{offeringid}}", "1"); template.replaceAll("{{division}}", "2"); template.replaceAll("{{vodId}}", "3"); template.replaceAll("{{lineupId}}", "4"); template.replaceAll("{{nnsProfile}}", "5"); template.replaceAll("{{cacheID}}", "6");
Neu parser
const template = '/nns/V1/event/offeringid/{{offeringid}}?division={{division}}&vodId={{vodId}}&lineup={{lineupId}}&profile={{nnsProfile}}&cacheID={{cacheID}}'; const varmap = parse(template); const result = printf(template, varmap, { offeringid: '1', division: '2', vodId: '3', lineupId: '4', nnsProfile: '5', cacheID: '6', });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
replace regex
replace All
Neu parser
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
replace regex
709733.8 Ops/sec
replace All
251032.7 Ops/sec
Neu parser
255316.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark and its various components. **Benchmark Definition:** The provided JSON defines a benchmark named "replaceAll vs regex replace vs neu parser". The benchmark has three test cases, each comparing different approaches for replacing substrings in a string: 1. **Regex Replace**: Uses the `String.prototype.replace()` method with a regular expression. 2. **All**: Uses the `String.prototype.replaceAll()` method (not a standard JavaScript method). 3. **Neu Parser**: Uses a custom parser function (`parse`) to replace substrings. **Script Preparation Code:** The benchmark defines two functions: 1. **`parse(str)`**: A custom parser function that takes a string as input and returns an array of objects containing the parsed substring, start index, and end index. 2. **`printf(template, varMap, vars)`**: A function that replaces placeholders in a template string with values from `varMap`. **Individual Test Cases:** Each test case has a unique benchmark definition that uses one of the three replacement approaches: 1. **"replace regex"`**: Uses the `String.prototype.replace()` method with a regular expression to replace substrings. 2. **"replace All"`**: Uses the non-standard `String.prototype.replaceAll()` method (not available in all browsers) to replace substrings. 3. **"Neu parser"`**: Uses the custom `parse` function and `printf` function to replace substrings. **Pros and Cons:** * **Regex Replace**: + Pros: Widely supported, efficient, and flexible. + Cons: Can be slower than other approaches for large strings. * **All** (non-standard method): + Pros: Simple implementation, potentially faster than regex replace. + Cons: Not widely supported, may not work in all browsers or environments. * **Neu Parser**: + Pros: Customizable, can handle complex replacements. + Cons: More complex implementation, may be slower than other approaches. **Other Considerations:** * **Performance:** The benchmark measures the execution speed of each replacement approach. A faster approach is generally preferred. * **Browser Support:** The non-standard `String.prototype.replaceAll()` method may not work in all browsers or environments. In summary, the benchmark compares three different replacement approaches for substrings in a string: regex replace, an alternative method (non-standard), and a custom parser function (Neu Parser). Each approach has its pros and cons, and the best choice depends on the specific use case and requirements.
Related benchmarks:
replaceAll vs regex replace . with ,
regex replaceAll vs regex replace
replaceAll vs regex global replace
replaceAll vs regex replace 1:1
Comments
Confirm delete:
Do you really want to delete benchmark?