Raw array vs TypedArray Bitops

Benchmark created on


Description

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

Expectation:

  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 the small integer optimization cannot be used in most engines)
  3. TypedArrays performance will not be affected by whether values are above 1e31 or not

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.