My goal is to enhance the efficiency of a slider that contains numerous conditional statements triggered during sliding/swiping actions.
Which approach yields better performance?
1- Utilizing a predefined object with key conditions
const controller = (config) => {
top: {
positionVertical: false,
orderAfter: false,
width: '100%',
height: config.height + 'px',
},
bottom: {
positionVertical: false,
orderAfter: true,
width: '100%',
height: config.height + 'px',
},
left: {
positionVertical: true,
orderAfter: false,
width: config.width + 'px',
height: '100%'
},
right: {
positionVertical: true,
orderAfter: true,
width: config.width + 'px',
height: '100%'
}
};
this.gallery.sub$.pipe(
map(({state, config}) => this.controller(config)[config.position])
);
2- Implementing standard if-else or switch case statements
this.gallery.sub$.pipe(
map(({state, config}) => {
switch(config.position) {
case 'top': return {
positionVertical: false,
orderAfter: false,
width: '100%',
height: config.height + 'px',
}
case 'left': return {
positionVertical: true,
orderAfter: false,
width: config.width + 'px',
height: '100%'
}
case 'right': return {
positionVertical: true,
orderAfter: true,
width: config.width + 'px',
height: '100%'
}
default: return {
positionVertical: false,
orderAfter: true,
width: '100%',
height: config.height + 'px',
}
}
})
);
Your insights and additional explanations are encouraged.