简体   繁体   中英

Javascript Hashing Two Numbers

I have two numbers in javascript.

They are integral coordinates.

I also have an object: var regions = {};

I'd like to be able to access specific objects as quickly as possible.

Eg there could be an object at (5,-2)

What's a fast way of creating unique hashes, that do not clash, for two numbers like this?

I'm assuming the "specific objects" you want to access are referenced by properties on regions . Object properties in JavaScript are named either by string names or by Symbol names. You'd be using strings in this case.

Since it's going to be by string, the simple solution is just create a string, and look it up with that:

var obj = regions[num1 + "," + num2];

That leaves it up to the JavaScript engine to do a good job looking up the property. JavaScript engines are very good (read: fast) at this, because they have to do it a lot .

How the engine does the lookup will depend in some measure on how the regions object is created. Wherever possible, modern engines will create objects that are effectively micro-classes and provide very quick resolution of property names. If you do certain things (like using delete on one of the properties), a modern engine may fall back to "dictionary" mode where it uses a hash lookup. This is still fast, just not as fast as the optimized form.

I doubt you'll find a quicker way that that. In theory, if region were a contiguous array where elements referenced contiguous arrays, the JavaScript engine could make those true arrays under the covers, and one of your example numbers is negative which would prevent a true array from being used. And you'd still be doing two lookups (the first number, then the second), and there's no guarantee it would be faster. It would certainly be more complicated.

As you've said you'll receive the information in dribs and drabs, I'd go with a simple compound string key until/unless you have a performance problem with that solution, then look at trying to get contiguous arrays happening on your target engines (converting those negative indexes into something else, which could be problematic). I doubt you'll find that the lookup is every the bottleneck.

Example:

var regions = {
    '5,-2': { ... },
    '1,3': { ... }
};

and then if you have the 2 numbers you could easily generate the key and access the corresponding object:

var x = 5;
var y = -2;
var obj = regions[x + ',' + y];

You can use array notation:

var regions = {}
regions[[5,-2]] = myObject;
regions[[5,-2]]; // myObject

Object keys can't be arrays, so under the hood [5,-2] will be stringified to "5,-2" . But I think that using array notation is more pretty than stringifying manually.

I used two function encode and decode .

var x = 10;
var y = -3.2;

function encode(x, y) {
  return x + ',' + y;
}

function decode(code) {
  var xy = code.split(',');
  return [+xy[0], +xy[1]];
}

var code = encode(x, y);
console.log(code);
var xy = decode(code);
console.log(xy);

The code can be used as dict key value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM