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.
This commit is contained in:
Hornwitser committed 2020-11-20 01:36:07 +01:00
1 parent cdb048741c
commit a08087c5e8
1 file changed
+4
+4
View File
@@ -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")