Skip to content

Homework tasks #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Homework tasks #2

wants to merge 6 commits into from

Conversation

ALEGATOR1209
Copy link

No description provided.

Copy link
Member

@tshemsedinov tshemsedinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 🥇

@@ -0,0 +1,47 @@
'use strict';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8-timeout.js


const setTimer = msec => {
timer = setTimeout(() => {
console.log('Cache cleaned');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove duplicate code

}, msec);
};

const generateKey = (...args) => args.join('|');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add types to cache key


return (...args) => {
if (!timer) setTimer(msec);
const key = generateKey(...args);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass array to generateKey to avoid arguments repack


console.log(`Calculate: ${args} = ${fn(...args)}`);
const res = fn(...args);
cache[key] = res;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not write undefined to cache

if (!timer) setTimer(msec);
const key = generateKey(...args);

if (cache[key]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check type, aware of 0, false and empty string

const length = size.length;
const data = parseFloat(size, 10);
let k = 1;
switch (size.charAt(length - 1)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use new Map or {} for large cases

let bytes = 0;
for (const i in obj) {
switch (typeof obj[i]) {
case 'number':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
  number: 8,
  boolean: 4
  ....
}


console.log(`Calculating: ${key}`);
cache[key] = fn(...args);
console.dir(`Cache size: ${getSize(cache)}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calculate getSize(cache) once

@ALEGATOR1209
Copy link
Author

Problems solved. @tshemsedinov review, please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants