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

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

问题描述

我目前使用 OpenLayers 并且有大量数据要绘制到矢量图层中(超过 100000 个矢量).

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

我现在正在尝试将所有这些向量放入 JavaScript 哈希映射中以分析性能.我想知道JavaScript中的hash map是如何实现的,是真正的hash函数还是只是一个使用简单数据结构和搜索算法的包装函数?

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 object 是一个简单的哈希图,它接受一个字符串或一个 Symbol 作为其键,因此您可以将代码编写为:

every javascript object is a simple hashmap which accepts a string or a Symbol 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对象在其实现上是一个真正的hashmap,所以搜索的复杂度是O(1),但是没有专门的用于javascript字符串的hashcode()函数,它是由javascript内部实现的引擎(V8、SpiderMonkey、JScript.dll 等...)

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

2020 年更新:

javascript 今天也支持其他数据类型:MapWeakMap.与传统对象相比,它们更接近于哈希映射.

javascript today supports other datatypes as well: Map and WeakMap. They behave more closely as hash maps than traditional objects.

这篇关于JavaScript 哈希映射是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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