JavaScript 哈希映射是如何实现的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8877666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How is a JavaScript hash map implemented?
提问by Patrick Hillert
I currently work with OpenLayers and have a huge set of data to draw into a vector layer (greater than 100000 vectors).
我目前使用 OpenLayers 并且有大量数据要绘制到矢量图层中(大于 100000 个矢量)。
I'm now trying to put all these vectors into a JavaScript hash map to analyze the performance. I want to know how is the hash map in JavaScript implemented, is it a real hash function or just a wrapped function that uses a simple data structure and a search algorithm?
我现在正在尝试将所有这些向量放入 JavaScript 哈希映射中以分析性能。我想知道 JavaScript 中的哈希映射是如何实现的,它是一个真正的哈希函数还是只是一个使用简单数据结构和搜索算法的包装函数?
回答by otakustay
every javascript object is a simple hashmap which only accepts string value as its key, so you could write your code as:
每个 javascript 对象都是一个简单的 hashmap,它只接受字符串值作为其键,因此您可以将代码编写为:
var map = {};
// add a item
map[key1] = value1;
// or remove it
delete map[key1];
// or determine whether a key exists
key1 in map;
javascript object is a real hashmap on its implementation, so the complexity on search is O(1), but there is no dedicated hashcode()
function for javascript strings, it is implemented internally by javascript engine (V8, SpiderMonkey, JScript.dll, etc...)
javascript对象在其实现上是一个真正的hashmap,所以搜索的复杂度是O(1),但是没有专门hashcode()
的javascript字符串函数,它是由javascript引擎(V8、SpiderMonkey、JScript.dll等)内部实现的。 .)
however, javascript today does not support other datatype except string as its key, ECMAv6 (harmony) would introduce a WeakMap class which accept any object as key, but it would be a long time...
但是,今天的 javascript 不支持除字符串以外的其他数据类型作为其键,ECMAv6(和谐)将引入一个接受任何对象作为键的 WeakMap 类,但这将是很长时间......
回答by Craig Barnes
JavaScript objects cannot be implemented purely on top of hash maps.
JavaScript 对象不能纯粹在哈希映射之上实现。
Try this in your browser console:
在浏览器控制台中试试这个:
var foo = {
a: true,
b: true,
z: true,
c: true
}
for (var i in foo) {
console.log(i);
}
...and you'll recieve them back in insertion order, which is de facto standardbehaviour.
...您将按插入顺序收到它们,这是事实上的标准行为。
Hash maps inherently do not maintain ordering, so JavaScript implementations may usehash maps somehow, but if they do, it'll require at least a separate index and some extra book-keeping for insertions.
哈希映射本质上不维护排序,因此 JavaScript 实现可能会以某种方式使用哈希映射,但如果使用,则至少需要一个单独的索引和一些额外的插入簿记。
Here's a video of Lars Bak explaining why v8 doesn't use hash maps to implement objects.
回答by Milo?
Here is an easy and convenient way of using something similar to the Java map:
这是使用类似于 Java地图的简单方便的方法:
var map= {
'map_name_1': map_value_1,
'map_name_2': map_value_2,
'map_name_3': map_value_3,
'map_name_4': map_value_4
}
And to get the value:
并获得价值:
alert( map['map_name_1'] ); // fives the value of map_value_1
...... etc .....
回答by Nguyen Tan Dat
Should you try this class Map
:
你应该试试这个类Map
:
var myMap = new Map();
// setting the values
myMap.set("1", 'value1');
myMap.set("2", 'value2');
myMap.set("3", 'value3');
myMap.size; // 3
// getting the values
myMap.get("1"); // "value associated with "value1"
myMap.get("2"); // "value associated with "value1"
myMap.get("3"); // "value associated with "value3"
Notice: key and value can be any type.
注意:key 和 value 可以是任何类型。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
回答by mb21
While plain old JavaScript objects can be used as maps, they are usually implemented in a way to preserve insertion-order for compatibility with most browsers (see Craig Barnes's answer) and are thus not simple hash maps.
虽然普通的旧 JavaScript 对象可以用作映射,但它们通常以保留插入顺序以与大多数浏览器兼容的方式实现(参见 Craig Barnes 的回答),因此不是简单的哈希映射。
ES6 introduces proper Maps (see MDN JavaScript Map) of which the standard says:
ES6 引入了正确的 Maps(参见MDN JavaScript Map),其中标准说:
Map object must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection.
Map 对象必须使用哈希表或其他机制来实现,平均而言,这些机制提供的访问时间与集合中的元素数量呈次线性关系。
回答by rajendra kumar
<html>
<head>
<script type="text/javascript">
function test(){
var map= {'m1': 12,'m2': 13,'m3': 14,'m4': 15}
alert(map['m3']);
}
</script>
</head>
<body>
<input type="button" value="click" onclick="test()"/>
</body>
</html>
回答by dd619
I was running into the problem where i had the json with some common keys. I wanted to group all the values having the same key. After some surfing I found hashmap package. Which is really helpful.
我遇到了问题,我有一些通用键的 json。我想对所有具有相同键的值进行分组。经过一番冲浪,我找到了hashmap 包。这真的很有帮助。
To group the element with the same key, I used multi(key:*, value:*, key2:*, value2:*, ...)
.
为了使用相同的键对元素进行分组,我使用了multi(key:*, value:*, key2:*, value2:*, ...)
.
This package is somewhat similar to Java Hashmap collection, but not as powerful as Java Hashmap.
这个包有点类似于Java Hashmap集合,但没有Java Hashmap那么强大。