Raw array vs TypedArray Bitops (v3)

Revision 3 of this benchmark created on


Description

Compare JS bit operation speed depending on whether the values are coming from a regular array or a typed array.

Expectations:

(1) TypedArray will be faster than raw array (since the engine can assume the value is 32-bits without checking)

(2) Raw arrays will take a performance hit for integers above 1e31 (since most engines can't use the small integer optimization on numbers that big)

(3) TypedArrays performance will not be affected by whether values are above 1e31 or not

Outcome: Both (2) and (3) seem correct across V8, SpiderMonkey, and JSC. However, (1) only seems to hold in Firefox (where typed array is twice as fast), as the raw array with small numbers performs slightly (10-20%) better on V8 and JSC.

Setup

COUNT = 1e6;

const makeRawArray = (offset) => {
	const array = [];
	for(i = 0; i<COUNT; i++) {
		array.push(i + offset)
	}
	return array;
}

const ra1 = makeRawArray(0);
const ra2 = makeRawArray(0);

const ra1big = makeRawArray(1e31);
const ra2big = makeRawArray(1e31);

const makeTypedArray = (offset) => {
	const array = new Uint32Array(COUNT);
	for (i = 0; i<COUNT; i++) {
		array[i] = i + offset;
	}
	return array;
}

const ta1 = makeTypedArray(0);
const ta2 = makeTypedArray(0);

const ta1big = makeTypedArray(1e31);
const ta2big = makeTypedArray(1e31);

Test runner

Ready to run.

Testing in
TestOps/sec
Raw Array
let a = 0;
for (i = 0; i<COUNT; i++) {
	a = a | (ra1[i] & ra2[i]);
}
a
ready
Typed array
let a = 0;
for (i = 0; i<COUNT; i++) {
	a = a | (ta1[i] & ta2[i]);
}
a
ready
Raw Array (>1e31 ints)
let a = 0;
for (i = 0; i<COUNT; i++) {
	a = a | (ra1big[i] & ra2big[i]);
}
a
ready
Typed Array (>1e31 ints)
let a = 0;
for (i = 0; i<COUNT; i++) {
	a = a | (ta1big[i] & ta2big[i]);
}
a
ready

Revisions

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