JavaScript哈希映射如何实现? [英] How is a JavaScript hash map implemented?

查看:104
本文介绍了JavaScript哈希映射如何实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前与OpenLayers一起工作,并有大量数据绘制到矢量图层中(大于100000个矢量)。



我现在试图把所有这些向量转换为JavaScript哈希映射来分析性能。我想知道哈希映射在JavaScript中是如何实现的,它是一个真正的哈希函数还是只是一个使用简单数据结构和搜索算法的包装函数?

解决方案

每个javascript对象都是一个简单的hashmap,它只接受字符串值作为key,因此您可以将代码编写为:

  var map = {}; 
//添加一个项目
map [key1] = value1;
//或删除它
删除map [key1];
//或确定一个键是否存在
key1 in map;

javascript对象是一个真正的hashmap,因此搜索的复杂度为O(1),但是对于javascript字符串没有专用的 hashcode()函数,它由JavaScript引擎(V8,SpiderMonkey,JScript.dll等)内部实现。



但是,今天的JavaScript不支持其他数据类型,除了字符串作为它的关键字,ECMAv6(和谐)将引入一个WeakMap类,它接受任何对象作为关键字,但这会很长一段时间...

I currently work with OpenLayers and have a huge set of data to draw into a vector layer (greater than 100000 vectors).

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?

解决方案

every javascript object is a simple hashmap which only accepts string value as its key, so you could write your code as:

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...)

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哈希映射如何实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆