darkskyapp / string-hash Goto Github PK
View Code? Open in Web Editor NEWFast string hashing function for Node.JS.
Fast string hashing function for Node.JS.
I was using this module to get a hash and mod by 16 to get a random-ish but constant index for a given string. I was about to ensure the string was lowercase so that the hash was case insensitive, when I noticed that all my indexes for a string were the same no matter what the case was!
Here's an example on tonic to show this: https://tonicdev.com/rahat/string-hash-case-insensitivity
I noticed that this property holds true if you mod a hash by a power of 2 up to 6432 (though 64 does have a lot of matching indexes, it will rarely have a different index for a different case).
I don't know if this a bug per-se, but it is a very interesting property. Any clue as to why it behaves like this?
I suspect that (x >>> 0) is a quicker way to convert the hash result to an unsigned integer.
Ie, replace:
hash >= 0 ? hash : (hash & 0x7FFFFFFF) + 0x80000000
with:
hash >>> 0
Differences include iterating over the string backwards (as that is faster in JavaScript)
This requires some proof, because for long strings backward iteration may break any data caching/prefetching, therefore access to string characters may be significantly slower.
https://jsperf.com/string-hash-speed forward direction is faster in Chrome
https://gist.github.com/victor-homyakov/bcb7d7911e4a388b1c810f8c3ce17bcf shows me that in Node 8.6.0 forward iteration is twice as fast.
The readme has you importing a stringHash function but the code clearly calls the function hash. either refactor or adjust the readme.
source code:
const glob = require('glob')
const hash = require('string-hash')
const names = glob.sync('**')
console.log('Size: %s', names.length)
const used = {}
for (const item of names) {
const value = hash(item)
if (used[value]) {
console.log('Found collision, hash: %s, a: %s, b: %s', value, used[value], item)
}
used[value] = item
}
The output is:
Size: 69791
Found collision, hash: 413020853, a: node_modules/@shimo/ui/icons/symbol/plus_circle.svg, b: node_modules/aliyun-sdk/apis/dm-2015-11-23.json
Thanks for creating this. I was curious, what is the chance of two hashes colliding within this algorithm. I was thinking of using it to hash emails.
so that your changes since 2012 will be available on npm =]
It is possible to unhash the resultet value?
I do some automated checks, which licenses my application is using. That is mainly based on the license field in package.json. If you put a CC0 there, it would be fine.
[user@likely html]$ sudo npm install string-hash -save
npm ERR! path /var/www/html/node_modules/npm/node_modules/dezalgo
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall rename
npm ERR! enoent ENOENT: no such file or directory, rename '/var/www/html/node_modules/npm/node_modules/dezalgo' -> '/var/www/html/node_modules/npm/node_modules/.dezalgo.DELETE'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2017-12-10T05_45_35_903Z-debug.log
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.