creators (v7)

Revision 7 of this benchmark created on


Setup

function creator(type) {
  function Action(value) {
    return {
      type,
      value,
      creator: Action,
    };
  };
  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;
	}
}

// --------------------
class ActionType {
	constructor(type){
		this.type = type;
		this.inputs = [];
	}
	
	addInput(input){
		this.inputs.push(input);
	}
}
class SetActionType {
	setActionType(actionType){
		this.actionType = actionType;
	}
}
// -user created
class MyActionTypeWC extends ActionType {
	static name = 'my-action';
}
class MyActionWC {
	static type = MyActionTypeWC;
	constructor(val){
		this.value = val;
	}
}
// lib created
function newAction(actionClass){
	const actionType = new actionClass.type(actionClass.type.name);
	return function (...args){
		const action = new actionClass(...args);
		action._type = actionType;
		return action;
	}
}

//----- object

function objAction(type){
	const inputs = [];
	const obj = {
		type,
		make(val){
			return {
				val,
				creator:obj
			}
		},
		addInput(input){
			inputs.push(input);
		}
	};
	return obj;
}
//----
class _Action {
	constructor(type){
		this.type = type;
		this.inputs = [];
	}
	addInput(input){
		this.inputs.push(input);
	}
}
function _creator(type) {
	const Action_ = new _Action(type);
  function Action(value){
  	return {
  		value,
  		creator: Action_
  	}
  }
  Action.creator = Action_
  return Action;
}

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
setActionType
const Action = newAction(MyActionWC);
const action = Action(10);
action._type.addInput('input');
result = action;
ready
obj
const Action = objAction('test');
const action = Action.make(10);
action.creator.addInput('input');
result = action;
ready
_creator
const Action = _creator('test');
const action = Action(10);
Action.creator.addInput('input');
result = action;
ready

Revisions

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