te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>hash - How are hashtables implemented in JavaScript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hash - How are hashtables implemented in JavaScript - Stack Overflow

programmeradmin3浏览0评论

Some languages implement hash tables which can use anything, not just strings, as a key. In JavaScript, you are restricted to strings and numbers. Is the lookup for this kind of implementation still O(1)? Is there an implementation in JavaScript?

Some languages implement hash tables which can use anything, not just strings, as a key. In JavaScript, you are restricted to strings and numbers. Is the lookup for this kind of implementation still O(1)? Is there an implementation in JavaScript?

Share Improve this question edited Sep 2, 2012 at 19:07 Andreas Köberle 111k58 gold badges280 silver badges307 bronze badges asked Sep 2, 2012 at 19:05 MaiaVictorMaiaVictor 53k47 gold badges157 silver badges301 bronze badges 1
  • can you clarify what you meant by "Hash Objects"? Were you referring to the client side new Object(); or var foo = {}; type of object or the underlying data structure used by "the system" (e.g. C++)? – scunliffe Commented Sep 2, 2012 at 19:25
Add a ment  | 

2 Answers 2

Reset to default 14

There's obviously a lot of misunderstanding around this subject. In JavaScript, they keys of objects are actually only strings (see §8.10). Anything (including numbers) used as an object key is converted to string. Thus, obj[1] and obj["1"] are equivalent.

Internally, many JS implementations use some kind of hash table to enable quick lookup of object properties. V8 actually generates hidden C++ classes that allows lookups to execute in a single CPU instruction. Either way, it's safe to assume that object property access is fast and near O(1).

As a consequence of all object property keys being converted to string, you can only use things that convert to string as a key. Since numbers naturally convert to string, they work fine. Same for booleans. Dates work too, since Date instances convert to unique strings (although there are edge cases to be aware of).

However, objects do not convert into meaningful strings. For example:

var o = {};
o[{a:1}]='some value';

Actually results in:

o = { '[object Object]': 'some value' }

because of the string conversion rules. Since every object converts to [object Object], adding many objects as keys to another object will result in an object with only one property.

Of course, it's still possible to use an object as a key. You just have to override the default implementation of toString — in effect, creating your own hashing algorithm. For example,

function ComplexKey(a,b) {
    this.a = a;
    this.b = b;
}

ComplexKey.prototype.toString = function() {
    return this.a + ':' + this.b;
}

var o = {};
o[new ComplexKey(1,2)] = 'x';
o[new ComplexKey(3,4)] = 'y';

Results in:

o = {
    '1:2': 'x',
    '3:4': 'y'
}

JavaScript does not have "hashtables".

It has Dictionaries which are collections of Key/Value pairs, with the restriction that a Key must be unique. The difference is that the implementation for a Dictionary does not imply a hash, although a Hash table will likely be used internally by implementations for performance reasons.

All objects in JavaScript function as Dictionaries (this excluded primitive types such as string and number). Generally an empty object is used for a generic "hashtable":

var o = {};
o[propExpression] = valueExpression;

However, here is the important thing: all property/key values are first converted to strings.

Thus the following are identical:

o[true] = 1
o["true"] = 1

Following through the ECMAScript is a bit confusing, but the key part to "knowing" that property names are all strings is found in Property Descriptors:

The Property Identifier type is used to associate a property name with a Property Descriptor. Values of the Property Identifier type are pairs of the form (name, descriptor), where name is a String and descriptor is a Property Descriptor value.

发布评论

评论列表(0)

  1. 暂无评论