Int8 divide by 2

Benchmark created on


Description

Is it faster to (unsigned) bitwise right shift once or divide by 2 directly using Int8Arrays?

Setup

const dim = 10_000

function rand () {
  const a = new Int8Array(dim)
  for (let i = 0; i < dim; i++) {
    a[i] = Math.random() > .5 ? 1 : -1;
  }
  return a
}

Test runner

Ready to run.

Testing in
TestOps/sec
Bitwise shift
let a = rand()
let b = new Int8Array(dim)

for (let i = 0; i < dim; i++) {
 b[i] = a[i] >> 1
}
ready
U-Bitwise shift
let a = rand()
let b = new Int8Array(dim)

for (let i = 0; i < dim; i++) {
 b[i] = a[i] >>> 1
}
ready
Divide by 2
let a = rand()
let b = new Int8Array(dim)

for (let i = 0; i < dim; i++) {
 b[i] = a[i] / 2
}
ready

Revisions

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