testing number hashing

Benchmark created on


Setup

window.MAX = Math.pow(2, 31) - 1;
window.buf = new ArrayBuffer(8);
window.i32 = new Int32Array(buf);
window.f64 = new Float64Array(buf);
window.advanceHash = function (cur) {
	hval ^= cur;
	hval = hval + (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
}
window.writeInt = function (num) {
	if (!Number.isSafeInteger(num)) {
		debugger;
		throw new Error('Invalid non uint value');
	}
	if (num < 0) {
		advanceHash(0);
		num = -num;
	} else {
		advanceHash(1);
	}
	let rem = num;
	if (num > MAX) {
		rem = num % MAX;
		advanceHash((num / MAX) | 0);
	}
	advanceHash(rem);
}
window.writeFloat = function (num) {
	num = num * 1.0; // coerse to float
	f64[0] = num;
	advanceHash(i32[0]);
	advanceHash(i32[1]);
}
window.hval = 0x811c9dc5;
window.arr = [];
for (let i = 0; i < 200; i++) {
	const rand = Math.random();
	const val = rand * MAX;
	arr.push(Math.floor(val)*0xFF);
}

Test runner

Ready to run.

Testing in
TestOps/sec
int
for (let i = 0; i < 200; i++) {
	const item = arr[i];
	window.writeInt(item);
}
ready
float
for (let i = 0; i < 200; i++) {
	const item = arr[i];
	window.writeFloat(item);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.