Unless you are benchmarking and actively measuring performance, then you almost certainly shouldn’t care.
Yeah! The only situations I know I am using mutability is when I have to process tens of thousands of items, like this:
const index = array.reduce( (result, x) => (result[x.prop] = x, result), {} )
If it is below tens of thousands I will write this:
const index = array.reduce( (result, x) => ({ ...result, [x.prop]: x }), {} )
My rule is: if mutation makes the code significantly better to read or actually helps with performance, you can apply it locally.
So in code reviews I ask people to move stuff into functions that, from the outside, seem to be immutable:
function replaceAt(array, index, x) { const copy = array.slice() array[index] = x return copy }
The alternative would be something like this (more likely to have errors):
function replaceAt(array, index, x) { return [...array.slice(0, index), x, ...array.slice(index + 1)] }
Side note: if a language has an immutable construct in it's standard library I would prefer that.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Yeah! The only situations I know I am using mutability is when I have to process tens of thousands of items, like this:
If it is below tens of thousands I will write this:
My rule is: if mutation makes the code significantly better to read or actually helps with performance, you can apply it locally.
So in code reviews I ask people to move stuff into functions that, from the outside, seem to be immutable:
The alternative would be something like this (more likely to have errors):
Side note: if a language has an immutable construct in it's standard library I would prefer that.