简体   繁体   English

JavaScript中是否有字符串池概念? 我们可以获取值/键来引用一个String对象吗?

[英]Is there a String pool concept in JavaScript? Can we get values/keys to refer to just one String object?

I have a large json map with around 1 million objects and each object with around 200 key-value pair. 我有一个大的json地图,大约有100万个对象,每个对象有大约200个键值对。 eg. 例如。 [{key1 : val1, key2 : val2, ...}, {key1 : val3, key2 : val4, ...}]

as you see the keys are getting duplicated here, with each key means a new String object. 如你所见,密钥在这里重复,每个密钥意味着一个新的String对象。 Is there any alternative way where I can say that all duplicate keys should point to same String object to reduce the memory size of map. 是否有任何替代方法,我可以说所有重复键应指向相同的String对象,以减少映射的内存大小。 With the mentioned stats the browser blows up with more than 1Gb of memory. 通过上面提到的统计数据,浏览器会耗尽超过1Gb的内存。

as you see the keys are getting duplicated here, with each key means a new String object. 如你所见,密钥在这里重复,每个密钥意味着一个新的String对象。

Well, no, they each get a string primitive . 嗯,不,他们每个都得到一个字符串原语 Granted it a subtle distinction, but JavaScript has both: 这是一个微妙的区别,但JavaScript同时具备:

var sp = "primitive";
var so = new String("object");

Is there a String pool concept in JavaScript? JavaScript中是否有字符串池概念?

Not in terms of anything external that you can intentionally invoke such as Java's intern . 不是你可以故意调用的任何外部的东西,比如Java的intern

A given JavaScript engine (V8, SpiderMonkey, etc.) may or may not reuse string primitives under the covers as an optimization; 给定的JavaScript引擎(V8,SpiderMonkey等)可能会或可能不会重复使用封面下的字符串原语作为优化; it can because strings are immutable in JavaScript, but whether it's ever made it to the top of a development priority list... 可以因为字符串在JavaScript中是不可变的,但它是否曾使它成为开发优先级列表的顶部...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript:如何获取以特定字符串开头的对象的所有键和值? - JavaScript: How can I get all the keys and values of an object that begin with a specific string? 我们可以在javascript中使用字符串获取类的对象吗? - Can we get an object of class using a string in javascript? 我们可以说String是Javascript中的一个对象吗? - Can we say that String is an object in Javascript? javascript通过字符串引用对象Google标记 - javascript refer to object by string google marker 我们如何从地图上显示一个键及其值 <String, List<Object> &gt;一次在JSP页面上? - How Can we show One Key and its Values from Map<String, List<Object>> at a time on a JSP page? 如何将对象的键和值转换为逗号分隔的字符串? - How to get keys and values of an object into a comma separated string? NodeJ用字符串引用对象 - NodeJs refer to object with string 如何从javascript中json字符串中的对象列表中获取键 - how to get keys from object list in json string in javascript 确定Javascript对象是“复杂”对象还是仅仅是字符串 - Determining if a Javascript object is a “complex” object or just a string 如何将对象分成2个字符串。 一个字符串中的键,另一个字符串中的值 - how to split object in 2 strings. keys in one string, values in other string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM