-
Notifications
You must be signed in to change notification settings - Fork 31
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
1 parent
5b08077
commit 43d9cf1
Showing
3 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
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,15 @@ | ||
# Immutability cost | ||
|
||
## Immutable methods | ||
|
||
- Object.values/keys/entries | ||
- concat | ||
- rest/spread `...` | ||
- slice | ||
- filter/map | ||
|
||
## Mutable methods | ||
|
||
- Object.assign | ||
- splice | ||
- push |
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,86 @@ | ||
function filterNumbersNotOptimal(array: number[]) { | ||
return array | ||
.filter((item) => item % 2 === 0) // O(N) cpu + memory | ||
.map((item) => item * 2) // O(N) cpu + memory | ||
.filter((item) => item >= 1_000); // O(N) cpu + memory | ||
} | ||
|
||
function filterNumbersOptimal(array: number[]) { | ||
const filteredArray: number[] = []; | ||
|
||
array.forEach((item) => { | ||
if (item % 2 === 1) { | ||
return; | ||
} | ||
|
||
const multiplied = item * 2; | ||
|
||
if (multiplied < 1_000) { | ||
return; | ||
} | ||
|
||
filteredArray.push(item); | ||
}); | ||
|
||
return filteredArray; | ||
} | ||
|
||
function flattenOneNotOptimal<T>(arrays: T[][]) { | ||
return arrays.reduce((result, array) => result.concat(array), []); | ||
} | ||
|
||
function flattenOneOptimal<T>(arrays: T[][]) { | ||
return arrays.reduce((result, array) => { | ||
// return result.push(...array); | ||
return result.push.apply(result, array); | ||
}, []); | ||
} | ||
|
||
function updateObjectValuesNotOptimal(object: Record<string, number>) { | ||
return Object.fromEntries( | ||
Object.entries(object).map(([key, value]) => [key, value * 2]) | ||
); | ||
} | ||
|
||
function updateObjectValuesOptimal(object: Record<string, number>) { | ||
const newObject: Record<string, number> = {}; | ||
|
||
for (const key in object) { | ||
newObject[key] = object[key] * 2; | ||
} | ||
|
||
return newObject; | ||
} | ||
|
||
function sumTwoNotOptimal(array: number[], sum: number) { | ||
// const set = new Set([...array]); | ||
const set = new Set(array); | ||
|
||
for (let i = 0, l = array.length; i < l; i++) { | ||
const num = array[i]; | ||
const difference = sum - num; | ||
|
||
if (set.has(difference)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function sumTwoOptimal(array: number[], sum: number) { | ||
const set = new Set(); | ||
|
||
for (let i = 0, l = array.length; i < l; i++) { | ||
const num = array[i]; | ||
const difference = sum - num; | ||
|
||
if (set.has(difference)) { | ||
return true; | ||
} | ||
|
||
set.add(num); | ||
} | ||
|
||
return false; | ||
} |
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,63 @@ | ||
const array = [1, 2, 3, 4]; | ||
const object = { | ||
a: 1, | ||
b: 2, | ||
c: "asdf", | ||
}; | ||
|
||
// ========================== | ||
// COPY | ||
// ========================== | ||
const copy1 = { | ||
...object, | ||
}; | ||
|
||
// mutates 1st object | ||
const copy2 = Object.assign({}, object); | ||
|
||
const copy3 = [...array]; | ||
const copy4 = array.slice(); | ||
const copy5 = array.concat([]); | ||
|
||
function immutableCopyArray<T>(array: T[]) { | ||
const newArray: T[] = []; | ||
|
||
for (let i = 0, l = array.length; i < l; i++) { | ||
newArray[i] = array[i]; | ||
} | ||
|
||
return newArray; | ||
} | ||
|
||
function immutableCopyObject<T>(object: Record<string, T>) { | ||
const newRecord = {}; | ||
|
||
for (const key in object) { | ||
newRecord[key] = object[key]; | ||
} | ||
|
||
return newRecord; | ||
} | ||
|
||
// ========================== | ||
// iterate | ||
// ========================== | ||
const filtered = array.filter((item) => item > 0); | ||
const mapped = array.map((item) => item * 1); | ||
|
||
Object.keys(object).forEach((key) => { | ||
console.log(key, object[key]); | ||
}); | ||
|
||
Object.values(object).forEach((value) => { | ||
console.log(value); | ||
}); | ||
|
||
Object.entries(object).forEach(([key, value]) => { | ||
console.log(key, value); | ||
}); | ||
|
||
for (const key in object) { | ||
const value = object[key]; | ||
console.log(key, value); | ||
} |