From a08087c5e8429cb4fdff4f34cfd31c960140c8bf Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 20 Nov 2020 01:32:32 +0100 Subject: [PATCH] Optimize escapeLabelValue for the common case Avoid an expensive string copy for the common case of there being no characters to escape in the label value. --- packages/lib/prometheus.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/lib/prometheus.js b/packages/lib/prometheus.js index cf25b6a3..9a9bda69 100644 --- a/packages/lib/prometheus.js +++ b/packages/lib/prometheus.js @@ -206,6 +206,7 @@ class Collector { async* collect() { } } +const labelEscapesChars = /[\\\n\"]/; /** * Escapes a label value in accordance with the text exposition format * @@ -214,6 +215,9 @@ class Collector { * @private */ function escapeLabelValue(value) { + if (!labelEscapesChars.test(value)) { + return value; + } return value .replace(/\\/g, "\\\\") .replace(/\n/g, "\\n")