我有一个 JSON 对象,其中包含大约 150 个 [key] : [sentence]
形式的条目,其中句子通常一遍又一遍地包含相同的文本。例如:
var test = {
"abc" : "This is the first sentence that is in this JSON object. Hello! Blue."
"abd" : "Red. This is the second sentence that is in this JSON object. Hi!"
"abe" : "This is the third sentence that is in this JSON object. Hey! Yellow."
"abf" : "White. This is the fourte sentence that is in this JSON object. Hola!"
}
您可以看到字符串“此 JSON 对象中的句子”。经常重复。用一些 anchor 替换它,然后在从对象中获取条目后替换 anchor 会更有效吗?即:
var test = {
"abc" : "This is the first #anchor# Hello!"
"abd" : "This is the second #anchor# Hi!"
"abe" : "This is the third #anchor# Hey!"
"abf" : "This is the fourte #anchor# Hola!"
}
var anchor = "sentence that is in this JSON object."
从 JSON 中获取所需值后,将字符串替换为实际文本?这些句子没有特定的模式,但其中大量包含重复的文本字符串。感谢您的建议!
最佳答案
基本上,您正在谈论用带宽换取计算。在原始情况下,需要更多带宽并且不需要下载后计算(字符串替换)。在后一种情况下,带宽较少,但需要进行后处理。
对我来说,您要针对您的用例明确回答这个问题的唯一方法是测试这两种场景并确定最适合您的方案。您还必须决定是否需要在带宽与执行时间之间进行权衡。
关于javascript - JSON对象重复字符串效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15535111/