Z
Z
Zerstoren2012-10-16 21:59:39
JavaScript
Zerstoren, 2012-10-16 21:59:39

How to find the hash sum of an object in JavaScript?

In general, there is such a task.
There is an object (dictionary) which contains arbitrary data, but not very large. I need to find out the hash sum of a given object.
Why do I need this:
I have a map that is drawn by Chunks (pieces of 2048 * 1024 pixels), the user can move the camera around the map and quite quickly. And it is not interesting to redraw the map every 5 seconds, you need to enable caching and determine if any objects and their positions have changed.
The thought suggests only a hash of the sum. If calculating the hash sum would be slower than drawing such a map, then it makes no sense to calculate the hash sum of the objects.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Assorium, 2012-10-18
@Assorium

hash = function (str) {
  var hash = 0;
  var str = String(str);
  if (str.length == 0) return hash;
  for (i = 0; i < str.length; i++) {
    char = str.charCodeAt(i);
    hash = ((hash<<5)-hash)+char;
    hash = hash & hash; // Convert to 32bit integer
  }
  return hash;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question