-
-
Notifications
You must be signed in to change notification settings - Fork 24
Made tasks #4
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
base: master
Are you sure you want to change the base?
Made tasks #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| 'use strict'; | ||
|
|
||
| const argKey = x => (x.toString() + ':' + typeof(x)); | ||
| const generateKey = arg => arg.map(argKey).join('|'); | ||
|
|
||
| const timerMemoize = (fn, msec) => { | ||
| const cache = new Map(); | ||
| return (...args) => { | ||
| const key = generateKey(args); | ||
| const timeout = setTimeout(() => cache.delete(key), msec); | ||
| if (cache.has(key)) { | ||
| const value = cache.get(key); | ||
| console.log('From cache: ', value.res); | ||
| clearTimeout(value.timeout); | ||
| value.timeout = timeout; | ||
| return value.res; | ||
| } | ||
| const res = fn(...args); | ||
| cache.set(key, { timeout, res }); | ||
| console.log('Calculated: ', res); | ||
| return res; | ||
| }; | ||
| }; | ||
|
|
||
| const sum = (a, b) => a + b; | ||
| const f1 = timerMemoize(sum, 10000); | ||
| f1(1, 2); | ||
| f1(1, 2); | ||
| f1(1, 2); | ||
| f1(2, 4); | ||
| f1(2, 4); | ||
| setTimeout(() => f1(2, 4), 9000); | ||
| setTimeout(() => f1(2, 4), 12000); | ||
| setTimeout(() => f1(2, 4), 22500); | ||
| setTimeout(() => f1(2, 4), 23000); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add empty line at file end |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| 'use strict'; | ||
|
|
||
| const argKey = x => (x.toString() + ':' + typeof(x)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| const generateKey = arg => arg.map(argKey).join('|'); | ||
|
|
||
| const countMemoize = (fn, max) => { | ||
| const cache = new Map(); | ||
| return (...args) => { | ||
| const key = generateKey(args); | ||
| if (cache.has(key)) { | ||
| const value = cache.get(key); | ||
| console.log('from cache:', value.res); | ||
| value.count += 1; | ||
| return value.res; | ||
| } | ||
| const res = fn(...args); | ||
| console.log('Calculated:', res); | ||
| if (cache.size > max) { | ||
| let [deleted, { count: num }] = cache.entries().next().value; | ||
| cache.forEach((value, keys) => { | ||
| const count = value.count; | ||
| if (num >= count) { | ||
| num = count; | ||
| deleted = keys; | ||
| } | ||
| }); | ||
| console.log('deleted: ', deleted); | ||
| cache.delete(deleted); | ||
| } | ||
| cache.set(key, { res, count: 1 }); | ||
| return res; | ||
| }; | ||
| }; | ||
|
|
||
| const sum = (a, b) => a + b; | ||
| const f1 = countMemoize(sum, 3); | ||
| f1(1, 2); | ||
| f1(1, 2); | ||
| f1(1, 2); | ||
| f1(2, 4); | ||
| f1(2, 4); | ||
| f1(3, 4); | ||
| f1(4, 4); | ||
| f1(4, 4); | ||
| f1(3, 4); | ||
| f1(4, 4); | ||
| f1(4, 4); | ||
| f1(5, 4); | ||
| f1(5, 4); | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,93 @@ | ||||||||||
| 'use strict'; | ||||||||||
|
|
||||||||||
| const argKey = x => (x.toString() + ':' + typeof(x)); | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||||||||||
| const generateKey = arg => arg.map(argKey).join('|'); | ||||||||||
|
|
||||||||||
| const sizeMemoize = (fn, maxSize) => { | ||||||||||
| const cache = new Map(); | ||||||||||
| const sizeToBytes = (size) => { | ||||||||||
| if(/[TGMKBtgmkb]/.test(size)) { | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| const k = parseFloat(size.slice(0, size.length - 1)); | ||||||||||
| const char = size.slice(size.length - 1); | ||||||||||
| const tableOfSize = { | ||||||||||
| T: Math.pow(2, 40), | ||||||||||
| G: Math.pow(2, 30), | ||||||||||
| M: Math.pow(2, 20), | ||||||||||
| K: Math.pow(2, 10), | ||||||||||
| B: 1, | ||||||||||
| }; | ||||||||||
| return tableOfSize[char.toUpperCase()] * k; | ||||||||||
| } | ||||||||||
| else return parseFloat(size); | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| }; | ||||||||||
| const getSize = function(arg) { | ||||||||||
| if (!arg) return 0; | ||||||||||
| let countOfObj = 0; | ||||||||||
| const getSizeTable = { | ||||||||||
| number: 8, | ||||||||||
| boolean: 4, | ||||||||||
| string: arg.length * 2, | ||||||||||
| }; | ||||||||||
| if (typeof(arg) === 'object') { | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| for (const value of arg.values()) { | ||||||||||
| ({ res: arg } = value); | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| countOfObj += (getSizeTable[typeof(arg)] ? getSizeTable[typeof(arg)] : getSize(arg)); | ||||||||||
| } | ||||||||||
| return countOfObj; | ||||||||||
| } else { | ||||||||||
| const countOfArg = getSizeTable[typeof(arg)]; | ||||||||||
| return countOfArg; | ||||||||||
| } | ||||||||||
| }; | ||||||||||
| return (...args) => { | ||||||||||
| const key = generateKey(args); | ||||||||||
| if (cache.has(key)) { | ||||||||||
| const value = cache.get(key); | ||||||||||
| console.log('from cache:', value.res); | ||||||||||
| value.count += 1; | ||||||||||
| return value.res; | ||||||||||
| } | ||||||||||
| const res = fn(...args); | ||||||||||
| const inBytes = sizeToBytes(maxSize); | ||||||||||
| if (getSize(res) > inBytes) { | ||||||||||
| console.log('Size of result more than can hold cache!'); | ||||||||||
| console.log('Calculated:', res); | ||||||||||
| return res; | ||||||||||
| } | ||||||||||
| while (getSize(cache) + getSize(res) > inBytes) { | ||||||||||
| let [deleted, { count: num }] = cache.entries().next().value; | ||||||||||
| cache.forEach((value, key) => { | ||||||||||
| const count = value.count; | ||||||||||
| if (num >= count) { | ||||||||||
| num = count; | ||||||||||
| deleted = key; | ||||||||||
| } | ||||||||||
| }); | ||||||||||
| console.log('deleted:', deleted); | ||||||||||
| cache.delete(deleted); | ||||||||||
| } | ||||||||||
| console.log('Calculated:', res); | ||||||||||
| cache.set(key, { res, count: 1 }); | ||||||||||
| return res; | ||||||||||
| }; | ||||||||||
| }; | ||||||||||
| const sum = (a, b) => a + b; | ||||||||||
| const f1 = sizeMemoize(sum, '20'); | ||||||||||
| f1('s', 'qsssss'); | ||||||||||
| f1(1, 2); | ||||||||||
| f1(1, 2); | ||||||||||
| f1(true, false); | ||||||||||
| f1(2, 4); | ||||||||||
| f1(2, 4); | ||||||||||
| f1(2, 4); | ||||||||||
| f1(3, 4); | ||||||||||
| f1('22', 4); | ||||||||||
| f1('qqqqq', 'qqqqqqq'); | ||||||||||
| f1('qqqqq', 'qqqqq'); | ||||||||||
| f1(4, 4); | ||||||||||
| f1('q', 4); | ||||||||||
| f1(5, 4); | ||||||||||
| f1(5, 4); | ||||||||||
|
|
||||||||||
|
|
||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove 2 of 3 empty lines here |
||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||||
| 'use strict'; | ||||||||
|
|
||||||||
| const argKeySync = x => (x.toString() + ':' + typeof(x)); | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| const generateKeySync = arg => arg.map(argKeySync).join('|').concat('(Sync)'); | ||||||||
| const generateKeyAsync = arg => arg.slice().shift().concat('(Async)'); | ||||||||
| const fs = require('fs'); | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to the file top |
||||||||
|
|
||||||||
| const syncAsyncMemoize = function(fn) { | ||||||||
| const cache = new Map(); | ||||||||
| return (...args) => { | ||||||||
| if (typeof(args[args.length - 1]) === 'function') { | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| const callback = args.pop(); | ||||||||
| const key = generateKeyAsync(args); | ||||||||
| if (cache.has(key)) { | ||||||||
| const value = cache.get(key); | ||||||||
| console.log('Async cache'); | ||||||||
| callback(value.err, value.data); | ||||||||
| return; | ||||||||
| } | ||||||||
| fn(...args, (err, data) => { | ||||||||
| console.log('Async calculated'); | ||||||||
| cache.set(key, { err, data }); | ||||||||
| callback(err, data); | ||||||||
| }); | ||||||||
| } else { | ||||||||
| const key = generateKeySync(args); | ||||||||
| if (cache.has(key)) { | ||||||||
| const value = cache.get(key); | ||||||||
| console.log('from cache:', value); | ||||||||
| return value; | ||||||||
| } | ||||||||
| const res = fn(...args); | ||||||||
| cache.set(key, res); | ||||||||
| console.log('Calculated:', res); | ||||||||
| return res; | ||||||||
| } | ||||||||
| }; | ||||||||
| }; | ||||||||
| const sum = (a, b) => a + b; | ||||||||
| const f1 = syncAsyncMemoize(sum); | ||||||||
| f1(1, 2); | ||||||||
| f1(1, 2); | ||||||||
| f1(2, 2); | ||||||||
| f1(2, 2); | ||||||||
| f1(2, 2); | ||||||||
|
|
||||||||
| const readfile = syncAsyncMemoize(fs.readFile); | ||||||||
| readfile('sizeMemoize.js', 'utf8', (err, data) => { | ||||||||
| console.log(data.length); | ||||||||
| readfile('sizeMemoize.js', 'utf8', (err, data) => { | ||||||||
| console.log(data.length); | ||||||||
| }); | ||||||||
| }); | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.