Skip to content

Commit

Permalink
add: do not use spread
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Dec 22, 2024
1 parent b9d905b commit 7288179
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* ## Recommendation
* - Use mutable operations when performance is critical and immutability is unnecessary.
* - Use immutable patterns for reliability and maintainability when working in state-heavy applications like React, but optimize with tools like Immer for large data.
*/
const recommendation = {};

// ------------------ Mutable

const mutable = { name: 'Alice', age: 25 };
mutable.age = 26;
console.log('mutable', mutable);

const mutable_arrays = [1, 2, 3];
mutable_arrays.push(4);
console.log('mutable_arrays', mutable_arrays);

const largeArray = new Array(1_000_000).fill(0);
largeArray[999_999] = 1;
console.log('largeArray', largeArray);

// ------------------ Immutable (Instead of mutating, create a new object or array)

// Immutable update of an object
const person = { name: 'Alice', age: 25 };
const updatedPerson = { ...person, age: 26 };
console.log('\nupdatedPerson', updatedPerson);

// Immutable update of an array
const numbers = [1, 2, 3];
const updatedNumbers = [...numbers, 4];
console.log('updatedNumbers', updatedNumbers);

0 comments on commit 7288179

Please sign in to comment.