๐ ๏ธ Under construction
dnd_dice_roller's Introduction
dnd_dice_roller's People
dnd_dice_roller's Issues
Optimize dice reducer
Hello,
I didn't know how to add feedback to your code so I thought issues might be a good place.
dnd_dice_roller/src/reducers/diceReducer.js
Lines 10 to 47 in b99796b
Instead of using an array that you have to iterate each time you dispatch an action (O(N) complexity, N being the number of rows)
// when you want to update the amount of a specific dice type, you iterate over the whole list
return state.map((die) =>
die.id === action.id ? { ...die, dieAmount: action.payload } : die
)
You could use an object, making the update O(1).
const initialState = {
0: {
dieAmount: 1,
dieType: 4,
modAmount: 0,
},
1: {
dieAmount: 1,
dieType: 6,
modAmount: 0,
},
2: {
dieAmount: 1,
dieType: 8,
modAmount: 0,
},
3: {
dieAmount: 1,
dieType: 10,
modAmount: 0,
},
4: {
dieAmount: 1,
dieType: 12,
modAmount: 0,
},
5: {
dieAmount: 1,
dieType: 20,
modAmount: 0,
},
}
// ...
export default (state = initialState, action) => {
switch (action.type) {
case UPDATE_DIE_AMOUNT:
return {
...state,
[action.id]: { ...state[action.id], dieAmount: action.payload },
};
case UPDATE_DIE_TYPE:
return {
...state,
[action.id]: { ...state[action.id], dieType: action.payload },
};
case UPDATE_MOD_AMOUNT:
return {
...state,
[action.id]: { ...state[action.id], modAmount: action.payload },
};
case RESET_ROW:
return { ...state, [action.id]: { ...resetDie } };
case REMOVE_ROW:
const { [action.id]: _, ...newState } = state;
return newState;
case ADD_ROW:
return { ...state, [nextId()]: { ...resetDie } };
default:
return state;
}
};
and the iteration in DiceContainer could look like this
{Object.entries(this.props.dice).map(([id, die]) => (
<DieRow die={die} id={id} key={id} />
))}
I know this is trivial given N, but might be helpful to know this in the future :)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.