diff --git a/ecmascript/base/json_stringifier.h b/ecmascript/base/json_stringifier.h index ec71dc6976f2456a6be47d1768ac35950d7489fc..b99f6092f97f4a554a2afed08a133979f849c977 100644 --- a/ecmascript/base/json_stringifier.h +++ b/ecmascript/base/json_stringifier.h @@ -36,6 +36,7 @@ public: { encoding_ = Encoding::ONE_BYTE_ENCODING; indent_ = 0; + oneByteResult_.reserve(INIT_RESULT_LENGTH); } #else explicit JsonStringifier(JSThread *thread, TransformType transformType = TransformType::NORMAL) @@ -125,6 +126,9 @@ private: ONE_BYTE_ENCODING, TWO_BYTE_ENCODING }; + + static const uint32_t INIT_RESULT_LENGTH = 512; + Encoding encoding_; CString oneByteResult_; C16String twoBytesResult_; diff --git a/ecmascript/base/json_stringifier_optimized.cpp b/ecmascript/base/json_stringifier_optimized.cpp index 8a2f42c335981cfcca2b9c8bb7fd56a4a10448c9..75e48179f0f597a901037548d934cdb447d8d210 100644 --- a/ecmascript/base/json_stringifier_optimized.cpp +++ b/ecmascript/base/json_stringifier_optimized.cpp @@ -1147,10 +1147,12 @@ void JsonStringifier::ChangeEncoding() { encoding_ = Encoding::TWO_BYTE_ENCODING; size_t length = oneByteResult_.length(); + twoBytesResult_.reserve(oneByteResult_.capacity()); twoBytesResult_.resize(length); for (size_t i = 0; i < length; ++i) { twoBytesResult_[i] = oneByteResult_[i]; } + CString().swap(oneByteResult_); } void JsonStringifier::NewLine()