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

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

问题描述

是的,我知道您可以在 JavaScript 中使用常规对象作为关联数组,但我想使用更接近 java 的 Map 实现的东西(HashMap、LinkedHashMap 等).可以将任何类型的数据用作密钥的东西.JavaScript 实现中是否有任何好的哈希(代码/表)?

解决方案

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

直接的答案是否定的,我不相信在 javascript 中有很好的 Java HashMap 实现.如果有,它肯定会成为您可能想要或可能不想使用的库的一部分,并且您当然不需要包含库只是为了拥有一个小哈希表.>

所以让我们继续写一个,只是为了检查问题.如果您愿意,可以使用它.我们将首先编写一个构造函数,然后我们将搭载 Array,它是 Object,但有一些有用的方法可以防止这个例子变得太乏味:

function HashMap() {var obj = [];返回对象;}var myHashMap = HashMap();

我们将直接从 Java 世界中添加一些方法,但在我们进行时将其转换为 javascript...

function HashMap() {var obj = [];obj.size = 函数 () {返回 this.length;};obj.isEmpty = 函数 () {返回 this.length === 0;};obj.containsKey = 函数(键){for (var i = 0; i < this.length; i++) {if (this[i].key === key) {返回我;}}返回-1;};obj.get = 函数(键){var index = this.containsKey(key);如果(索引> -1){返回这个[索引].值;}};obj.put = 函数(键,值){如果 (this.containsKey(key) !== -1) {返回 this.get(key);}this.push({'key': key, 'value': value});};obj.clear = 函数 () {这 = 空;//只是在开玩笑...};返回对象;}

我们可以继续构建它,但我认为这是错误的方法.归根结底,我们最终会使用 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天全站免登陆