Global Store Reducer vs Multiple Store Reducer

JavaScript performance comparison

Test case created by venkatloganathan

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const defaultTicketState = {
    	mouseDirection: 'buy',
    	isActive: true,
      isStale: false,
      isTradingAllowed: true,
      isSalesTrader: false,
    	gsloEnabledByDefault: true,
  	order: {
        direction: 'buy',
  		levelPrice: 1.43433,
  		oppositeLevelPrice: 1.5343,
  		stopLossPrice: NaN,
  		TakeProfitPrice: NaN,
  		designator: 'UNITS',
  		unitsLocked: true,
  		amountLocked: false
      },
    	price: {
  		bid: 1.2422,
  		offer: 1.442234,
  		ladder: {}
  	},
  	validation: {
  		priceValid: true,
  		stopLossValid: true,
  		amountValid: true
  	}
    }
  
  const state = {};
    state.a = {...defaultTicketState};
    state.b = {...defaultTicketState};
    state.c = {...defaultTicketState};
    state.d = {...defaultTicketState};
    state.e = {...defaultTicketState};
    
    state.f = {...defaultTicketState};
    state.g = {...defaultTicketState};
    state.h = {...defaultTicketState};
    state.i = {...defaultTicketState};
    state.j = {...defaultTicketState};
    
    const a = {...defaultTicketState};
    const b = {...defaultTicketState};
    const c = {...defaultTicketState};
    const d = {...defaultTicketState};
    const e = {...defaultTicketState};
    
    const f = {...defaultTicketState};
    const g = {...defaultTicketState};
    const h = {...defaultTicketState};
    const i = {...defaultTicketState};
    const j = {...defaultTicketState};
    
    const IDs = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
    
    const TOGGLE_UNITS_LOCK = 'toggle_units_lock';
  
    const unitsLockedToggleAction = (id = '') =>{
      return {
        type: TOGGLE_UNITS_LOCK,
        id
      }
    }
    
  const getUnitsLocked = (order) => {
      order.unitsLocked = !order.unitsLocked
      return order;
    }
    
    let newState = ''
  let ticket
    let order = '';
  
  const globalReducer = (state, action) => {
    
    switch (action.type) {
       case TOGGLE_UNITS_LOCK:
        newState = {...state};
        ticket = newState[action.id];
        order = ticket.order;
        newState.order = getUnitsLocked(order);
        return newState;
       default:
        return state;
    }
    }
    
  const multipleReducer = (state, action) => {
      switch (action.type) {
       case TOGGLE_UNITS_LOCK:
        return {...state, order: getUnitsLocked(order)};
       default:
        return state;
    }
    }
  
  const updateUnitsToggleState = (state, action) => {
      switch (action.type) {
       case TOGGLE_UNITS_LOCK:
         getUnitsLocked(state.order);
       break;
    }
    }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Global Store Reducer
globalReducer(state, unitsLockedToggleAction('a'));
globalReducer(state, unitsLockedToggleAction('b'));
globalReducer(state, unitsLockedToggleAction('d'));
globalReducer(state, unitsLockedToggleAction('e'));

globalReducer(state, unitsLockedToggleAction('f'));
globalReducer(state, unitsLockedToggleAction('g'));
globalReducer(state, unitsLockedToggleAction('h'));
globalReducer(state, unitsLockedToggleAction('i'));
globalReducer(state, unitsLockedToggleAction('j'));
pending…
Multiple Store Reducer
multipleReducer(a, unitsLockedToggleAction());
multipleReducer(b, unitsLockedToggleAction());
multipleReducer(c, unitsLockedToggleAction());
multipleReducer(d, unitsLockedToggleAction());
multipleReducer(e, unitsLockedToggleAction());

multipleReducer(f, unitsLockedToggleAction());
multipleReducer(g, unitsLockedToggleAction());
multipleReducer(h, unitsLockedToggleAction());
multipleReducer(i, unitsLockedToggleAction());
multipleReducer(j, unitsLockedToggleAction());
pending…
No Reducer
updateUnitsToggleState(a, unitsLockedToggleAction());
updateUnitsToggleState(b, unitsLockedToggleAction());
updateUnitsToggleState(c, unitsLockedToggleAction());
updateUnitsToggleState(d, unitsLockedToggleAction());
updateUnitsToggleState(e, unitsLockedToggleAction());

updateUnitsToggleState(f, unitsLockedToggleAction());
updateUnitsToggleState(g, unitsLockedToggleAction());
updateUnitsToggleState(h, unitsLockedToggleAction());
updateUnitsToggleState(i, unitsLockedToggleAction());
updateUnitsToggleState(j, unitsLockedToggleAction());
pending…

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

0 Comments