Skip to content

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