8000 Improve examples style and readability · HowProgrammingWorks/Memoization@22e2f7a · GitHub
[go: up one dir, main page]

Skip to content

Commit 22e2f7a

Browse files
committed
Improve examples style and readability
1 parent e308339 commit 22e2f7a

File tree

6 files changed

+38
-47
lines changed

6 files changed

+38
-47
lines changed

JavaScript/2-speed.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
22

3-
const generateKey = args => (
4-
args.map(x => x.toString() + ':' + typeof x).join('|')
5-
);
3+
const argKey = x => x.toString() + ':' + typeof x;
4+
const generateKey = args => args.map(argKey).join('|');
65

76
const memoize = fn => {
87
const cache = {};
@@ -33,11 +32,9 @@ const speedTest = (name, fn, args, count) => {
3332

3433
// Usage
3534

36-
let fib = n => (
37-
(n <= 2) ? 1 : fib(n - 1) + fib(n - 2)
38-
);
35+
const fib = n => (n <= 2 ? 1 : fib(n - 1) + fib(n - 2));
3936

4037
speedTest('fib(20)', fib, [20], LOOP_COUNT);
4138

42-
fib = memoize(fib);
43-
speedTest('memoized fib(20)', fib, [20], LOOP_COUNT);
39+
const mFib = memoize(fib);
40+
speedTest('memoized fib(20)', mFib, [20], LOOP_COUNT);

JavaScript/3-cacheSize.js

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
'use strict';
22

3-
const generateKey = args => (
4-
args.map(x => x + ':' + typeof x).join('|')
5-
);
3+
const argKey = x => x.toString() + ':' + typeof x;
4+
const generateKey = args => args.map(argKey).join('|');
65

76
const memoize = (fn, length) => {
87
const cache = new Map();
98
return (...args) => {
109
const key = generateKey(args);
10+
console.log(`${fn.name}(${key}) call`);
1111
if (cache.has(key)) return cache.get(key);
12+
console.log(`max(${key}) calculate`);
1213
const res = fn(...args);
1314
if (cache.size >= length) {
1415
const firstKey = cache.keys().next().value;
16+
console.log(`Delete key: ${firstKey}`);
1517
cache.delete(firstKey);
1618
}
1719
cache.set(key, res);
@@ -21,32 +23,16 @@ const memoize = (fn, length) => {
2123

2224
// Usage
2325

24-
const max = (a, b) => {
25-
console.log('Calculate: ' + a + '+' + b);
26-
return a > b ? a : b;
27-
};
26+
const max = (a, b) => (a > b ? a : b);
2827

2928
const mMax = memoize(max, 3);
3029

31-
console.log('mMax(10, 8)');
3230
mMax(10, 8);
33-
console.log('mMax(1, 15)');
31+
mMax(10, 8);
3432
mMax(1, 15);
35-
console.log('mMax(12, 3)');
3633
mMax(12, 3);
37-
console.log('mMax(15, 2)');
3834
mMax(15, 2);
39-
console.log('mMax(1, 15)');
4035
mMax(1, 15);
41-
console.log('mMax(0, 0)');
36+
mMax(10, 8);
4237
mMax(0, 0);
43-
console.log('mMax(0, 0)');
4438
mMax(0, 0);
45-
console.log('mMax(false, false)');
46-
mMax(false, false);
47-
console.log('mMax(false, false)');
48-
mMax(false, false);
49-
console.log('mMax(undefined, undefined)');
50-
mMax(undefined, undefined);
51-
console.log('mMax(undefined, undefined)');
52-
mMax(undefined, undefined);

JavaScript/4-async.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ const fs = require('fs');
77
const memoizeAsync = (lib, fnName) => {
88
const fn = lib[fnName];
99
const cache = {};
10-
console.log('override ' + fnName);
10+
console.log(`override ${fnName}`);
1111
lib[fnName] = (...args) => {
1212
console.dir({ call: fnName, args, cache });
1313
const cb = args.pop();
1414
const key = args[0];
1515
const record = cache[key];
16-
console.log('key: ' + key);
17-
console.log('cached: ' + record);
16+
console.log(`key: ${key}`);
17+
console.log(`cached: ${record}`);
1818
if (record) {
1919
console.log('from cache');
2020
cb(record.err, record.data);
2121
return;
2222
}
2323
fn(...args, (err, data) => {
2424
console.log('from file');
25-
console.log('Save key: ' + key);
25+
console.log(`Save key: ${key}`);
2626
cache[key] = { err, data };
2727
console.dir({ cache });
2828
cb(err, data);
@@ -35,8 +35,8 @@ const memoizeAsync = (lib, fnName) => {
3535
memoizeAsync(fs, 'readFile');
3636

3737
fs.readFile('4-async.js', 'utf8', (err, data) => {
38-
console.log('data length: ' + data.length);
38+
console.log(`data length: ${data.length}`);
3939
fs.readFile('4-async.js', 'utf8', (err, data) => {
40-
console.log('data length: ' + data.length);
40+
console.log(`data length: ${data.length}`);
4141
});
4242
});

JavaScript/5-sha.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
const crypto = require('crypto');
44

5+
const argKey = x => x.toString() + ':' + typeof x;
6+
57
const generateKey = args => {
6-
const key = args.map(x => x.toString() + ':' + typeof x).join('|');
8+
const key = args.map(argKey).join('|');
79
return crypto.createHash('sha256').update(key).digest('hex');
810
};
911

JavaScript/6-metasync.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
'use strict';
22

33
const fs = require('fs');
4-
const util = require('util');
54

65
// Production implementation from Metasync library
76
// See: https://github.com/metarhia/metasync
87

98
function Memoized() {}
109

11-
util.inherits(Memoized, Function);
12-
1310
const memoize = fn => {
1411
const cache = new Map();
1512

@@ -18,6 +15,7 @@ const memoize = fn => {
1815
const key = args[0];
1916
const record = cache.get(key);
2017
if (record) {
18+
console.log('Read from cache');
2119
callback(record.err, record.data);
2220
return;
2321
}
@@ -32,7 +30,7 @@ const memoize = fn => {
3230
timeout: 0,
3331
limit: 0,
3432
size: 0,
35-
maxSize: 0
33+
maxSize: 0,
3634
};
3735

3836
Object.setPrototypeOf(memoized, Memoized.prototype);
@@ -48,8 +46,12 @@ Memoized.prototype.clear = function() {
4846
fs.readFile = memoize(fs.readFile);
4947

5048
fs.readFile('6-metasync.js', 'utf8', (err, data) => {
51-
console.log('data length: ' + data.length);
49+
console.log(`data length: ${data.length}`);
5250
fs.readFile('6-metasync.js', 'utf8', (err, data) => {
53-
console.log('data length: ' + data.length);
51+
console.log(`data length: ${data.length}`);
52+
fs.readFile.clear();
53+
fs.readFile('6-metasync.js', 'utf8', (err, data) => {
54+
console.log(`data length: ${data.length}`);
55+
});
5456
});
5557
});

JavaScript/7-metasync.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ const metasync = require('metasync');
77

88
fs.readFile = metasync.memoize(fs.readFile);
99

10-
fs.readFile('6-metasync.js', 'utf8', (err, data) => {
11-
console.log('data length: ' + data.length);
12-
fs.readFile('6-metasync.js', 'utf8', (err, data) => {
13-
console.log('data length: ' + data.length);
10+
fs.readFile('7-metasync.js', 'utf8', (err, data) => {
11+
console.log(`data length: ${data.length}`);
12+
fs.readFile('7-metasync.js', 'utf8', (err, data) => {
13+
console.log(`data length: ${data.length}`);
14+
fs.readFile.clear();
15+
fs.readFile('7-metasync.js', 'utf8', (err, data) => {
16+
console.log(`data length: ${data.length}`);
17+
});
1418
});
1519
});

0 commit comments

Comments
 (0)
0