From ff50987f2815d9c1b280999ce321dfed3ac9fcb6 Mon Sep 17 00:00:00 2001 From: Keyv Chan Date: Tue, 20 May 2025 17:19:25 +0800 Subject: [PATCH] add caching for JSON.stringify(String) Issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/IC969V?from=project-issue Signed-off-by: chenkeyu --- static_core/plugins/ets/stdlib/escompat/json.ets | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/static_core/plugins/ets/stdlib/escompat/json.ets b/static_core/plugins/ets/stdlib/escompat/json.ets index dfdd97669c..66c598c550 100644 --- a/static_core/plugins/ets/stdlib/escompat/json.ets +++ b/static_core/plugins/ets/stdlib/escompat/json.ets @@ -592,6 +592,7 @@ class JSONWriter { private readonly path = new Set() private buffer = new StringBuilder() + private cachedStrings = new Map() constructor(replacer?: (key: String, value: NullishType) => NullishType, space?: String) { this.replacer = replacer @@ -629,7 +630,13 @@ class JSONWriter { } else if (obj === undefined) { this.buffer.append("undefined") } else if (obj instanceof String) { - this.buffer.append(JSON.stringify(obj as String)) + let key = obj as String + let value: String | undefined = this.cachedStrings.get(key) + if (value == undefined) { + value = JSON.stringify(key) + this.cachedStrings.set(key, value) + } + this.buffer.append(value) } else if (obj instanceof RegExpExecArray) { this.buffer.append(JSON.stringify((obj as RegExpExecArray).result)) } else if(obj instanceof Tuple) { -- Gitee