creators (v4)

Revision 4 of this benchmark created on


Setup

function creator(type) {
  const Action = function Action(value) {
    return {
      type,
      value,
    };
  };
  // @ts-expect-error ignore this
  Action.inputs = [];
  Action.addInput = function addInput(input) {
    Action.inputs.push(input);
  };
  return Action;
}

class Traversable {
  inputs = [];
  
  constructor(type) {
    this.type = type;
  }
  value(val) {
    this.val = val;
  }
  addInput(input) {
    this.inputs.push(input);
  }
}

class TypedAction {
  inputs = [];

  constructor(value) {
    this.value = value;
  }
  addInput(input) {
    this.inputs.push(input);
  }
}

function classInFn(typed) {
  class MyAction extends TypedAction {
    type = typed;
  }
  return MyAction;
}

function classInFn2(typed){
	function MyAction(value){
		this.inputs = [];
		this.value = value;
	}
	MyAction.type = typed;
	MyAction.prototype.addInput = function addInput(input){
		this.inputs.push(input);
	}
	return MyAction;
}


class Creator {
	inputs = [];
	
	constructor(type){
		this.type = type;
	}

	addInput(input){
		this.inputs.push(input);
	}
	
}

class ActionBase {
	constructor(creator){
		this.creator = creator;
	}
	setValue(val){
		this.value = val;
		return this;
	}
}

class MyAction extends ActionBase {
	static type = 'test';
}

function mixed(classType){
	const creator = new Creator(classType.type);
	return function (){
		return new classType(creator);
	}
}


class MyActionSelf {
	static creator = new Creator('test');

	constructor(val){
		this.value = val;
	}
}

class ActionSelf {
	isAction = true;
}


class MyActionSelfSuper extends ActionSelf {
	static creator = new Creator('test');

	constructor(val){
		super();
		this.value = val;
	}
}


let result;

Test runner

Ready to run.

Testing in
TestOps/sec
fn
const Action = creator('test');
    const action = Action(10);
    Action.addInput('input');
    result = action;
ready
extend (class in fn)
const Action = classInFn('test');
    const action = new Action(10);
    action.addInput('input');
    result = action;
ready
class
const action = new Traversable('test');
    action.value(10);
    action.addInput('input');
    result = action;
ready
mixed classfn
const Action = mixed(MyAction);
const action = Action().setValue(10);
action.creator.addInput('input');
result = action;
ready
self
const action = new MyActionSelf(10);
MyActionSelf.creator.addInput('input');
result = action;
ready
self super
const action = new MyActionSelfSuper(10);
MyActionSelfSuper.creator.addInput('input');
result = action;
ready
class in fn 2
const Action = classInFn2('test');
    const action = new Action(10);
    action.addInput('input');
    result = action;
ready

Revisions

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