Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Field formatting
(version: 0)
Comparing performance of:
regex vs loops
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var field = "event_data.param.parms[0]['hello']"
Tests:
regex
// replaces the start of the string with "['", if it starts with "[" str = field.replace(/^(?!\[)/, "['") // replaces the end of the string with "']", if it does ends with "]" .replace(/^(?<!\])(.*)(?<!\])$/, "$1']") // replaces the middle dots with "']['" if there is not "]" before the dot and no "[" after the dot .replace(/(?<!\])(?<=[^\]])\.(?=[^\[])(?!\[)/g, "']['") // replaces all "]." with "]['" .replace(/\]\./g, "]['") // replaces all ".[" with "'][" .replace(/\.\[/g, "'][") // replaces all "[" that are in the middle of the string, if there is no "]" before it .replace(/(?<=[^\]]+)\[(?=[^\[]+)/g, "']["); return str;
loops
if (field.startsWith('[')) { return field; } else { let arr = field.split("."); let newStr = ""; for (let i = 0; i < arr.length; i++) { if ( arr[i].includes('\[') ) { let [first, ...rest] = arr[i].split('['); rest = rest.join(''); newStr += `[\'${first}\'][${rest}`; } else { newStr += `[\'${arr[i]}\']`; } } return newStr; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
regex
loops
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!
Related benchmarks:
Lodash cloneDeep vs JSON Clone 121212121
Object.assign vs. JSON String/Parse with large-ish data
speedtest
JSON.parse - aggregated events vs single events
Comments
Confirm delete:
Do you really want to delete benchmark?