-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
standalone-apis/pure-nodejs-concepts/concepts/make-js-fast/do-not-use-spread.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |