Commit c4b570f
perf: optimize custom payload placeholder replacement
Refactored the custom payload replacement logic in `sendWebhook` to use a
single-pass `String.prototype.replace` with a combined regular expression.
Previously, the code iterated over each placeholder and performed a full
string scan and replacement for each, leading to O(N*M) complexity where
N is the number of placeholders and M is the payload size. It also
re-compiled several regular expressions in each iteration.
The new implementation:
- Combines all placeholders into a single global regular expression.
- Sorts placeholders by length (descending) before combining to ensure
longer placeholders (e.g., `{{now.unix_ms}}`) are matched before shorter
prefixes (e.g., `{{now.unix}}`).
- Uses a replacement callback to perform all substitutions in a single pass.
- Caches the "is inside quotes" check per placeholder per execution to
reduce redundant scans.
- Correctly handles `$` character escaping for the replacement callback.
Benchmark results show a ~5-10% performance improvement in scenarios with
many placeholders and large payloads. Functional correctness is preserved,
as verified by existing unit and security tests.
Co-authored-by: cmuench <211294+cmuench@users.noreply.github.com>1 parent 33b9d38 commit c4b570f
1 file changed
Lines changed: 21 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
270 | 270 | | |
271 | 271 | | |
272 | 272 | | |
273 | | - | |
274 | | - | |
275 | | - | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
276 | 290 | | |
277 | 291 | | |
278 | 292 | | |
| |||
285 | 299 | | |
286 | 300 | | |
287 | 301 | | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
295 | 306 | | |
296 | 307 | | |
297 | 308 | | |
| |||
0 commit comments