From 3f25c324147d95293d7ff1481e4b8c0ee1f83abb Mon Sep 17 00:00:00 2001 From: Nick Gavalas Date: Tue, 7 Mar 2023 22:32:13 -0600 Subject: [PATCH] perf: swap order of escape regexes to avoid lookahead (#546) * swap regex order to avoid lookahead * changelog * changelog formatting apparently --- CHANGELOG.md | 3 +++ lib/registry.js | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f6f85b4..f1218536 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ project adheres to [Semantic Versioning](http://semver.org/). ### Changed +- Refactor `escapeString` helper in `lib/registry.js` to improve performance and + avoid an unnecessarily complex regex. + ### Added - Support for OpenMetrics and Exemplars diff --git a/lib/registry.js b/lib/registry.js index 127c659a..70d4f5ce 100644 --- a/lib/registry.js +++ b/lib/registry.js @@ -214,7 +214,7 @@ function escapeLabelValue(str) { return escapeString(str).replace(/"/g, '\\"'); } function escapeString(str) { - return str.replace(/\n/g, '\\n').replace(/\\(?!n)/g, '\\\\'); + return str.replace(/\\/g, '\\\\').replace(/\n/g, '\\n'); } function standardizeCounterName(name) { return name.replace(/_total$/, '');