Skip to content

Commit

Permalink
immutable-ds-cost
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayub-Begimkulov committed Jan 7, 2023
1 parent 5b08077 commit 43d9cf1
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
15 changes: 15 additions & 0 deletions immutable-data-cost/README.md
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
86 changes: 86 additions & 0 deletions immutable-data-cost/code-expamples.ts
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;
}
63 changes: 63 additions & 0 deletions immutable-data-cost/methods-examples.ts
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);
}

0 comments on commit 43d9cf1

Please sign in to comment.