class member type hint (v2)

Revision 2 of this benchmark created on


Description

Can we hint strongly to the JS engine about a class member type?

Setup

var size = 16384;
var typed32 = new Int32Array(size);
crypto.getRandomValues(typed32);
var acc = 0;
function getVar(x) {
  return {
    a: typed32[x],
    b: typed32[x + 1],
    c: typed32[x + 2],
    d: typed32[x + 3],
  };
}
class GetCall {
  callback;
  constructor(callback) {
    this.callback = callback;
  }
  getThis(x) {
    return {
      a: this.callback(x),
      b: this.callback(x + 1),
      c: this.callback(x + 2),
      d: this.callback(x + 3),
    };
  }
}
var getCall = new GetCall(getVar);
class ArrayRef {
  array;
  constructor(typed) {
    this.array = typed;
  }
  getThis(x) {
    return {
      a: this.array[x],
      b: this.array[x + 1],
      c: this.array[x + 2],
      d: this.array[x + 3],
    };
  }
}
var arrayRef = new ArrayRef(typed32);
class TypeHint {
  typed;
  constructor(typed) {
    this.typed = typed ?? new Int32Array(size);
  }
  getThis(x) {
    return {
      a: this.typed[x],
      b: this.typed[x + 1],
      c: this.typed[x + 2],
      d: this.typed[x + 3],
    };
  }
}
var typeHint = new TypeHint(typed32);
class MakeTyped {
  typed;
  constructor() {
    this.typed = new Int32Array(size);
    crypto.getRandomValues(this.typed);
  }
  getThis(x) {
    return {
      a: this.typed[x],
      b: this.typed[x + 1],
      c: this.typed[x + 2],
      d: this.typed[x + 3],
    };
  }
}
var makeTyped = new MakeTyped(typed32);

Test runner

Ready to run.

Testing in
TestOps/sec
function getVar(x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = getVar(x);
  acc += a + b + c + d;
}
ready
getCall.getThis(x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = getCall.getThis(x);
  acc += a + b + c + d;
}

ready
arrayRef.getThis(x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = arrayRef.getThis(x);
  acc += a + b + c + d;
}
ready
typeHint.getThis(x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = typeHint.getThis(x);
  acc += a + b + c + d;
}
ready
makeTyped.getThis(x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = makeTyped.getThis(x);
  acc += a + b + c + d;
}
ready

Revisions

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