Javascript hashing function without floating point or allocations.

Status
Not open for further replies.
I've run into an interesting problem porting some of my code from C and C# to javascript. Specifically as part of it it hashes a portion of an array of 16bit numbers. The general shape is:

Code:
   function hash(array, start, length) {
      var h = ...

      // for each number in that segment
          h = lots *
              of <<
              math ^
              operations %
   }

This is normally blazingly fast as the operations pretty much boil down to straight CPU instructions. However, the problem i'm running into in javascript is that i can't seem to inform the JITs that this is all 32bit math that i'm trying to do. So, due to things like multiplication, the hash gets larger than than fixed size that the VM uses for in place integers and eventually becomes a float, causing both rejits and generally slower performance.

What i'm looking for then is a hashing function that works over a sequence of numbers that produces a reasonably well distributed hash while operating only within 31 bits (the max int size for VMs like V8 and chakra that use tagged integral values).

So fast and lean with reasonable distribution. Does anyone have anything like that that is available for use?

Thanks!
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Jenkins one at a time uses only bit shifting operators and is super quick in non script environments. Not sure if the use of the highest bit is easy to work around though (how fast is a bit mask test in the VM).

Alternatively accept that you use a good 16 bit hash function (perhaps using two independent variables on the odd/even elements) and then combine them at the end masking the high bit if required, I can't think of much for that apart from maybe fletcher.

If you go that route you could implement something like Pearson hashing with 4 separate variables each getting the n % {0,1,2,3} bit. Then stick them together at the end, again masking the high bit if required.

Optionally you could try to reclaim almost 1 bit of keyspace for some additional branches at the end by only masking the high bit if all individual sub states have it it set, else swap the first one not to have it set with the first one.

If using the Pearson route you could have a rotating xor operation on all possible pair combinations after each 4 bytes consumed from the source. This should result in a step towards "change a bit in the input, probability of change in any of the output bits is close to 50%", though obviously without an avalanching operation the last few bytes won't have as much impact.

Pearson has the advantage of being incredibly trivial to brute force tune if you know your source inputs have a bias. But that might not be relevant to you.

disclaimer: I really know stuff all about javascript implementations so there may be some huge gotchas that would prevent this from being useful/fast.
 

tinyMan

Ars Tribunus Angusticlavius
6,149
Subscriptor++
How much data are you trying to hash? I have used the Crypto-Js SHA1 (and HMAC) implementation on a number of projects, very successfully.

It is able to easily hash strings up to a Mb in size with no appreciable processing delay, even on very slow/ancient mobile devices. On a desktop, I'd think that you'd easily be able to do many megabytes in the blink of an eye. Importantly (for me at least) the Crypto-Js tools match equivalent calculations on other platforms and libraries.

Are you targetting desktop browser implementations, or are you deploying on something like node-js or rhino, where multiple Gb of data might be processed?
 
Status
Not open for further replies.