Skip to content

Commit e7ee700

Browse files
committed
Add prototype dynamic building implementation
1 parent 8dc043a commit e7ee700

7 files changed

Lines changed: 76 additions & 4 deletions

File tree

JavaScript/3-prototype-build.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
3+
const async = op => {
4+
switch (op) {
5+
case 'map': return api.metasync.map;
6+
case 'filter': return api.metasync.filter;
7+
case 'reduce': return api.metasync.reduce;
8+
case 'each': return api.metasync.each;
9+
case 'series': return api.metasync.series;
10+
case 'find': return api.metasync.find;
11+
}
12+
};
13+
14+
function ArrayChain(array) {
15+
this.array = array;
16+
this.chain = [];
17+
}
18+
19+
ArrayChain.prototype.execute = function(err) {
20+
const item = this.chain.shift() || {};
21+
if (err) {
22+
if (!item.op) throw err;
23+
if (item.op === 'catch') {
24+
item.fn(err);
25+
return this.execute();
26+
} else {
27+
return this.execute(err);
28+
}
29+
}
30+
if (!item.op) return;
31+
if (item.op === 'then') {
32+
item.fn(this.array);
33+
return this.execute();
34+
}
35+
const op = async(item.op);
36+
if (!op) return this.execute();
37+
op(this.array, item.fn, (err, data) => {
38+
if (err) return this.execute(err);
39+
this.array = data;
40+
this.execute();
41+
});
42+
};
43+
44+
['then', 'catch', 'map', 'filter', 'reduce', 'each', 'series', 'find']
45+
.map(op => {
46+
ArrayChain.prototype[op] = function(fn) {
47+
this.chain.push({ op, fn });
48+
return this;
49+
};
50+
});
51+
52+
ArrayChain.prototype.fetch = function(fn) {
53+
this.chain.push({ op: 'then', fn: res => fn(null, res) });
54+
this.chain.push({ op: 'catch', fn });
55+
this.execute();
56+
return this;
57+
};
58+
59+
module.exports = {
60+
for: array => new ArrayChain(array)
61+
};

JavaScript/5-compare.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ global.api = {
55
};
66

77
const implementations = (
8-
['1-promise', '2-functor', '3-prototype']
8+
['1-promise', '2-functor', '3-prototype', '3-prototype-build']
99
.map(name => './' + name + '.js')
1010
.map(require)
1111
);

JavaScript/7-compare.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
node --nouse-idle-notification --noconcurrent_sweeping --noconcurrent_recompilation --predictable 7-promise.js
44
node --nouse-idle-notification --noconcurrent_sweeping --noconcurrent_recompilation --predictable 7-functor.js
55
node --nouse-idle-notification --noconcurrent_sweeping --noconcurrent_recompilation --predictable 7-prototype.js
6+
node --nouse-idle-notification --noconcurrent_sweeping --noconcurrent_recompilation --predictable 7-prototype-build.js

JavaScript/7-functor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ global.api = {
77
const benchmark = require('./6-benchmark');
88
const chainFunctor = require('./2-functor');
99
const test = require('./4-test');
10-
benchmark.do(20000, 'Functor', (done) => test(chainFunctor, done));
10+
benchmark.do(40000, 'Functor', (done) => test(chainFunctor, done));

JavaScript/7-promise.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ global.api = {
77
const benchmark = require('./6-benchmark');
88
const chainPromise = require('./1-promise');
99
const test = require('./4-test');
10-
benchmark.do(20000, 'Promise', (done) => test(chainPromise, done));
10+
benchmark.do(40000, 'Promise', (done) => test(chainPromise, done));

JavaScript/7-prototype-build.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
global.api = {
4+
metasync: require('metasync')
5+
};
6+
7+
const benchmark = require('./6-benchmark');
8+
const chainPrototype = require('./3-prototype');
9+
const test = require('./4-test');
10+
benchmark.do(40000, 'ProtoBuild', (done) => test(chainPrototype, done));

JavaScript/7-prototype.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ global.api = {
77
const benchmark = require('./6-benchmark');
88
const chainPrototype = require('./3-prototype');
99
const test = require('./4-test');
10-
benchmark.do(20000, 'Prototype', (done) => test(chainPrototype, done));
10+
benchmark.do(40000, 'Prototype', (done) => test(chainPrototype, done));

0 commit comments

Comments
 (0)