CheckedType

Benchmark publishedon

Setup

class CheckedType1 {
  private __types__: string[];
  static isOfType = <Type extends CheckedType>(type: new (...args: unknown[]) => Type) => (object: unknown): object is Type => {
    const typeName = type.name;
    return (object as CheckedType)?.__types__?.some?.(typ => typ === typeName) || false;
  };

  constructor() {
    this.__types__ = [];
    let curr = Object.getPrototypeOf(this).constructor;
    do {
      if (curr?.name) {
        this.__types__.unshift(curr.name);
      }
      curr = Object.getPrototypeOf(curr);
    } while (curr);
  }
}

class Page1 extends CheckedType1 {
  constructor()	{
  	super();
  	CheckedType1.isOfType(Page1)(this);
  }
}

class CheckedType1_1 {
  private __types__: string[];
  static isOfType = <Type extends CheckedType>(type: new (...args: unknown[]) => Type) => (object: unknown): object is Type => {
    const typeName = type.name;
    return (object as CheckedType)?.__types__?.some?.(typ => typ === typeName) || false;
  };

  constructor() {
    this.__types__ = [];
    let curr = Object.getPrototypeOf(this).constructor;
    do {
      if (curr?.name) {
        this.__types__.push(curr.name);
      }
      curr = Object.getPrototypeOf(curr);
    } while (curr);
  }
}

class Page1_1 extends CheckedType1_1 {
  constructor()	{
  	super();
  	CheckedType1_1.isOfType(Page1_1)(this);
  }
}

class CheckedType2 {
  private __types__: string;
  static isOfType = <Type extends CheckedType>(type: new (...args: unknown[]) => Type) => (object: unknown): object is Type => {
    const typeName = type.name;
    return (object as CheckedType)?.__types__ === typeName || false;
  };

  constructor() {
    this.__types__ = Object.getPrototypeOf(this).constructor.name;
  }
}

class Page2 extends CheckedType2 {
  constructor()	{
  	super();
  	CheckedType2.isOfType(Page2)(this);
  }
}

class CheckedType3 {
  private __types__: string;
  static isOfType = <Type extends CheckedType>(type: string) => (object: unknown): object is Type => {
    return (object as CheckedType)?.__types__ === type || false;
  };

  constructor(name: string) {
    this.__types__ = name;
  }
}

class Page3 extends CheckedType3 {
	constructor() {
		super("asd");
  		CheckedType3.isOfType("asd")(this);
	}
}

Test Runner

Initializing...

Testing in
Test CaseOps/sec
Current

new Page1();
ready
W/o Recursion
new Page2();
ready
Current w/ push
new Page1_1();
ready
W/o Recursion w/ String
new Page3();
ready

Revisions

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

Revision 1
publishedon