那里有没有好的JavaScript哈希(代码/表)实现? [英] Is there any good JavaScript hash(code/table) implementation out there?

查看:86
本文介绍了那里有没有好的JavaScript哈希(代码/表)实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道你可以在JavaScript中使用常规对象作为关联数组,但我想使用更接近Java的Map实现(HashMap,LinkedHashMap等)的东西。可能有任何类型的数据用作关键字的东西。在JavaScript实现中有没有好的散列(代码/表)?

在javascript中,对象是字面上的一个哈希实现。一个Java HashMap会有点虚假,所以我会挑战你重新考虑你的需求。



直接回答不是,我不相信JavaScript中有很好的Java HashMap实现。如果有的话,它肯定会成为你可能会或可能不想使用的库的一部分,而且你肯定不需要包含一个库只是为了拥有一个散列表。 p>

因此,让我们继续撰写一个,只是为了检查问题。你可以使用它,如果你喜欢。我们将从写一个构造函数开始,然后我们将从Array,这是Object,但有一些有用的方法可以避免这个例子太枯燥:

 函数HashMap(){
var obj = [];
return obj;
}

var myHashMap = HashMap();

我们将直接从Java世界中添加一些方法,但随着我们的进行转换为JavaScript。 ..

pre $ function HashMap(){
var obj = [];
obj.size = function(){
return this.length;
};
obj.isEmpty = function(){
return this.length === 0;
};
obj.containsKey = function(key){
for(var i = 0; i< this.length; i ++){
if(this [i] .key === key ){
return i;
}
}
返回-1;
};
obj.get = function(key){
var index = this.containsKey(key);
if(index> -1){
return this [index] .value;
}
};
obj.put = function(key,value){
if(this.containsKey(key)!== -1){
return this.get(key);
}
this.push({'key':key,'value':value});
};
obj.clear = function(){
this = null; //开玩笑...
};
return obj;
}

我们可以继续构建它,但我认为这是错误的方法。在这一天结束时,我们最终会使用JavaScript在幕后提供的内容,因为我们只是没有HashMap类型。在伪装的过程中,它适用于各种额外工作

有点讽刺的是,让javascript成为一种有趣且多样化的语言之一就是它轻松处理这种摔跤。我们可以从字面上做我们想做的任何事情,如果它没有说明语言的欺骗性力量,那么这里的快速示例什么都不做。然而,考虑到这种力量,最好不要使用它。



我只是觉得javascript想要更轻。我个人的建议是,在尝试实施适当的Java HashMap之前,您需要重新检查问题。 Javascript既不需要也不提供一个

记住本地备选

  var map = [{},'string',4,{}]; 

通过比较,这么快速简单。



另一方面,我不相信这里有任何硬性的答案。这个实现可能是一个完全可以接受的解决方案。如果你觉得你可以使用它,我会说旋转。但是如果我觉得我们拥有更合理更简单,更自然的方式,我就不会使用它了。我几乎可以肯定我们的确如此。



旁注:
效率是否与样式相关?注意性能受到影响 ..在HashMap.put()中我们面对着一个很大的O ......不太理想的性能可能不是一个表现障碍,而你在你注意到现代浏览器的性能问题之前,你可能需要做一些非常有雄心的事情或者拥有大量的数据。有意思的是,当你对付谷物时,操作往往会变得不那么有效率,就好像工作中存在自然熵一样。 Javascript是一种高级语言,当我们遵守约定时,它应该提供高效的解决方案,就像Java中的HashMap将是一个更加自然和高性能的选择一样。


Yes, I know you could use regular objects as associative arrays in JavaScript, but I'd like to use something closer to java's Map's implementation (HashMap, LinkedHashMap etc). Something that could have any kind of data used as key. Are there any good hash(code/table) in JavaScript implementation out there?

解决方案

In javascript, objects are literally a hash implementation. A Java HashMap will be a little bit of a fake-out, so I'd challenge you to re-think your needs.

The straight answer is no, I don't believe that there is a great implementation of Java's HashMap in javascript. If there is, it's bound to be part of a library that you may or may not want to use, and you certainly don't need to include a library just to have a little hash table.

So let's go ahead and write one, just to examine the problem. You can use it if you like. We'll just start by writing a constructor, and we'll piggyback off of Array, which is Object, but has some useful methods that will keep this example from getting too tedious:

function HashMap () {
    var obj = [];
    return obj;
}

var myHashMap = HashMap();

We'll add some methods straight from the world of Java, but translate into javascript as we go...

function HashMap() {
    var obj = [];
    obj.size = function () {
        return this.length;
    };
    obj.isEmpty = function () {
        return this.length === 0;
    };
    obj.containsKey = function (key) {
        for (var i = 0; i < this.length; i++) {
            if (this[i].key === key) {
                return i;
            }
        }
        return -1;
    };
    obj.get = function (key) {
        var index = this.containsKey(key);
        if (index > -1) {
            return this[index].value;
        }
    };
    obj.put = function (key, value) {
        if (this.containsKey(key) !== -1) {
            return this.get(key);
        }
        this.push({'key': key, 'value': value});
    };
    obj.clear = function () {
        this = null;  // Just kidding...
    };
    return obj;
}

We could continue to build it out, but I think it's the wrong approach. At the end of the day, we end up using what javascript provides behind the scenes, because we just simply don't have the HashMap type. In the process of pretending, it lends itself to all kinds of extra work.

It's a bit ironic that one of the things that makes javascript such an interesting and diverse language is the ease with which it handles this kind of wrestling. We can literally do anything we'd like, and the quick example here does nothing if it doesn't illustrate the deceptive power of the language. Yet given that power, it seems best not to use it.

I just think javascript wants to be lighter. My personal recommendation is that you re-examine the problem before you try implement a proper Java HashMap. Javascript neither wants nor affords for one.

Remember the native alternative:

var map = [{}, 'string', 4, {}];

..so fast and easy by comparison.

On the other hand, I don't believe that there are any hard-and-fast answers here. This implementation really may be a perfectly acceptable solution. If you feel you can use it, I'd say give it a whirl. But I'd never use it if I felt that we have reasonably simpler and more natural means at our disposal.. which I'm almost certain that we do.

Sidenote: Is efficiency related to style? Notice the performance hit.. there's a big O staring us in the face at HashMap.put()... The less-than-optimal performance probably isn't a show-stopper here, and you'd probably need to be doing something very ambitious or have a large set of data before you'd even notice a performance hickup a modern browser. It's just interesting to note that operations tend to become less efficient when you're working against the grain, almost as if there is a natural entropy at work. Javascript is a high level language, and should offer efficient solutions when we keep in line with its conventions, just as a HashMap in Java will be a much more natural and high performing choice.

这篇关于那里有没有好的JavaScript哈希(代码/表)实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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