diff --git a/immutable-data-cost/README.md b/immutable-data-cost/README.md new file mode 100644 index 0000000..6647544 --- /dev/null +++ b/immutable-data-cost/README.md @@ -0,0 +1,15 @@ +# Immutability cost + +## Immutable methods + +- Object.values/keys/entries +- concat +- rest/spread `...` +- slice +- filter/map + +## Mutable methods + +- Object.assign +- splice +- push diff --git a/immutable-data-cost/code-expamples.ts b/immutable-data-cost/code-expamples.ts new file mode 100644 index 0000000..1f6a35b --- /dev/null +++ b/immutable-data-cost/code-expamples.ts @@ -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(arrays: T[][]) { + return arrays.reduce((result, array) => result.concat(array), []); +} + +function flattenOneOptimal(arrays: T[][]) { + return arrays.reduce((result, array) => { + // return result.push(...array); + return result.push.apply(result, array); + }, []); +} + +function updateObjectValuesNotOptimal(object: Record) { + return Object.fromEntries( + Object.entries(object).map(([key, value]) => [key, value * 2]) + ); +} + +function updateObjectValuesOptimal(object: Record) { + const newObject: Record = {}; + + 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; +} diff --git a/immutable-data-cost/methods-examples.ts b/immutable-data-cost/methods-examples.ts new file mode 100644 index 0000000..fdf68f3 --- /dev/null +++ b/immutable-data-cost/methods-examples.ts @@ -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(array: T[]) { + const newArray: T[] = []; + + for (let i = 0, l = array.length; i < l; i++) { + newArray[i] = array[i]; + } + + return newArray; +} + +function immutableCopyObject(object: Record) { + 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); +}