Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why not hash your objects yourself manually, and use the resulting strings as keys for a regular JavaScript dictionary? After all you are in the best position to know what makes your objects unique. That's what I do.</p> <p>Example:</p> <pre><code>var key = function(obj){ // some unique object-dependent key return obj.totallyUniqueEmployeeIdKey; // just an example }; var dict = {}; dict[key(obj1)] = obj1; dict[key(obj2)] = obj2; </code></pre> <p>This way you can control indexing done by JavaScript without heavy lifting of memory allocation, and overflow handling.</p> <p>Of course, if you truly want the "industrial-grade solution", you can build a class parameterized by the key function, and with all necessary API of the container, but &hellip; we use JavaScript, and trying to be simple and lightweight, so this functional solution is simple and fast.</p> <p>The key function can be as simple as selecting right attributes of the object, e.g., a key, or a set of keys, which are already unique, a combination of keys, which are unique together, or as complex as using some cryptographic hashes like in <a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/encoding/" rel="noreferrer">DojoX Encoding</a>, or <a href="http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/uuid/" rel="noreferrer">DojoX UUID</a>. While the latter solutions may produce unique keys, personally I try to avoid them at all costs, especially, if I know what makes my objects unique.</p> <p><strong>Update in 2014:</strong> Answered back in 2008 this simple solution still requires more explanations. Let me clarify the idea in a Q&amp;A form.</p> <p><em>Your solution doesn't have a real hash. Where is it???</em></p> <p>JavaScript is a high-level language. Its basic primitive (<a href="http://en.wikipedia.org/wiki/JavaScript_syntax#Objects" rel="noreferrer">Object</a>) includes a hash table to keep properties. This hash table is usually written in a low-level language for efficiency. Using a simple object with string keys we use an efficiently implemented hash table with no efforts on our part.</p> <p><em>How do you know they use hash?</em></p> <p>There are three major ways to keep a collection of objects addressable by a key:</p> <ul> <li>Unordered. In this case to retrieve an object by its key we have to go over all keys stopping when we find it. On average it will take n/2 comparisons.</li> <li>Ordered. <ul> <li>Example #1: a sorted array &mdash; doing a binary search we will find our key after ~log2(n) comparisons on average. Much better.</li> <li>Example #2: a tree. Again it'll be ~log(n) attempts.</li> </ul></li> <li>Hash table. On average it requires a constant time. Compare: O(n) vs. O(log n) vs. O(1). Boom.</li> </ul> <p>Obviously JavaScript objects use hash tables in some form to handle general cases.</p> <p><em>Do browser vendors really use hash tables???</em></p> <p>Really.</p> <ul> <li>Chrome/node.js/<a href="https://github.com/v8/v8/" rel="noreferrer">V8</a>: <a href="https://github.com/v8/v8/blob/master/src/objects.h#L1680" rel="noreferrer">JSObject</a>. Look for <a href="https://github.com/v8/v8/blob/master/src/objects.h#L3618" rel="noreferrer">NameDictionary</a> and <a href="https://github.com/v8/v8/blob/master/src/objects.h#L3606" rel="noreferrer">NameDictionaryShape</a> with pertinent details in <a href="https://github.com/v8/v8/blob/master/src/objects.cc" rel="noreferrer">objects.cc</a> and <a href="https://github.com/v8/v8/blob/master/src/objects-inl.h" rel="noreferrer">objects-inl.h</a>.</li> <li>Firefox/<a href="https://github.com/mozilla/gecko-dev" rel="noreferrer">Gecko</a>: <a href="https://github.com/mozilla/gecko-dev/blob/master/js/src/jsobj.h#L99" rel="noreferrer">JSObject</a>, <a href="https://github.com/mozilla/gecko-dev/blob/master/js/src/vm/NativeObject.h#L349" rel="noreferrer">NativeObject</a>, and <a href="https://github.com/mozilla/gecko-dev/blob/master/js/src/vm/NativeObject.h#L1238" rel="noreferrer">PlainObject</a> with pertinent details in <a href="https://github.com/mozilla/gecko-dev/blob/master/js/src/jsobj.cpp" rel="noreferrer">jsobj.cpp</a> and <a href="https://github.com/mozilla/gecko-dev/blob/master/js/src/vm/NativeObject.cpp" rel="noreferrer">vm/NativeObject.cpp</a>.</li> </ul> <p><em>Do they handle collisions?</em></p> <p>Yes. See above. If you found a collision on unequal strings, please do not hesitate to file a bug with a vendor.</p> <p><em>So what is your idea?</em></p> <p>If you want to hash an object, find what makes it unique and use it as a key. Do not try to calculate real hash or emulate hash tables &mdash; it is already efficiently handled by the underlying JavaScript object.</p> <p>Use this key with JavaScript <code>Object</code> to leverage its built-in hash table while steering clear of possible clashes with default properties.</p> <p>Examples to get you started:</p> <ul> <li>If your objects include a unique user name &mdash; use it as a key.</li> <li>If it includes a unique customer number &mdash; use it as a key. <ul> <li>If it includes unique government-issued numbers like an SSN, or a passport number, and your system doesn't allow duplicates &mdash; use it as a key.</li> </ul></li> <li>If a combination of fields is unique &mdash; use it as a key. <ul> <li>State abbreviation + driver license number makes an excellent key.</li> <li>Country abbreviation + passport number is an excellent key too.</li> </ul></li> <li>Some function on fields, or a whole object, can return a unique value &mdash; use it as a key.</li> </ul> <p><em>I used your suggestion and cached all objects using a user name. But some wise guy is named "toString", which is a built-in property! What should I do now?</em></p> <p>Obviously, if it is even remotely possible that the resulting key will exclusively consists of Latin characters, you should do something about it. For example, add any non-Latin Unicode character you like at the beginning or at the end to un-clash with default properties: "#toString", "#MarySmith". If a composite key is used, separate key components using some kind of non-Latin delimiter: "name,city,state".</p> <p>In general this is the place, where we have to be creative, and select the easiest keys with given limitations (uniqueness, potential clashes with default properties).</p> <p>Note: unique keys do not clash by definition, while potential hash clashes will be handled by the underlying <code>Object</code>.</p> <p><em>Why don't you like industrial solutions?</em></p> <p>IMHO, the best code is no code at all: it has no errors, requires no maintenance, easy to understand, and executes instantaneously. All "hash tables in JavaScript" I saw were >100 lines of code, and involved multiple objects. Compare it with: <code>dict[key] = value</code>.</p> <p>Another point: is it even possible to beat a performance of a primordial object written in a low-level language, using JavaScript and the very same primordial objects to implement what is already implemented?</p> <p><em>I still want to hash my objects without any keys!</em></p> <p>We are in luck: ECMAScript 6 (scheduled for mid 2015 release, give or take 1-2 years after that to become widespread) defines <a href="https://people.mozilla.org/~jorendorff/es6-draft.html#sec-map-objects" rel="noreferrer">map</a> and <a href="https://people.mozilla.org/~jorendorff/es6-draft.html#sec-set-objects" rel="noreferrer">set</a>.</p> <p>Judging by the definition they can use object's address as a key, which makes objects instantly distinct without artificial keys. OTOH, two different, yet identical objects, will be mapped as distinct.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload