Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
replaceAll vs regex replace vs neu parser
Totally curious on this.
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
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:
Chrome 129
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
replace regex
709733.8 Ops/sec
replace All
251032.7 Ops/sec
Neu parser
255316.7 Ops/sec
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', });