Skip to content

Commit 8dc043a

Browse files
committed
Make benchmarks async
1 parent ed97be2 commit 8dc043a

5 files changed

Lines changed: 28 additions & 16 deletions

File tree

JavaScript/4-test.js

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

3-
module.exports = (chaining) => {
3+
module.exports = (chaining, done) => {
4+
let count = 0;
5+
const next = () => (++count === 3 && done ? done() : 0);
46

57
chaining
68
.for([1, 2, 3, 4])
@@ -9,7 +11,8 @@ module.exports = (chaining) => {
911
.reduce((a, b, cb) => cb(null, a + b))
1012
.fetch((err, result) => {
1113
if (err) throw err;
12-
console.dir(result);
14+
if (!done) console.dir(result);
15+
next();
1316
});
1417

1518
chaining
@@ -19,15 +22,19 @@ module.exports = (chaining) => {
1922
.reduce((a, b, cb) => process.nextTick(cb, null, a + b))
2023
.fetch((err, result) => {
2124
if (err) throw err;
22-
console.dir(result);
25+
if (!done) console.dir(result);
26+
next();
2327
});
2428

2529
chaining
2630
.for([1, 2, 3, 4])
2731
.map((item, cb) => cb(new Error('Something happened')))
2832
.fetch((err, result) => {
29-
if (err) console.log(err.message);
30-
else console.dir(result);
33+
if (!done) {
34+
if (err) console.log(err.message);
35+
else console.dir(result);
36+
}
37+
next();
3138
});
3239

3340
};

JavaScript/6-benchmark.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ const PRE_COUNT = 1000;
88
const rpad = (s, char, count) => (s + char.repeat(count - s.length));
99
const lpad = (s, char, count) => (char.repeat(count - s.length) + s);
1010

11-
benchmark.do = (count, name, fn) => {
11+
12+
benchmark.do = (num, name, fn) => {
13+
const done = () => {
14+
const end = process.hrtime(begin);
15+
const diff = end[0] * 1e9 + end[1];
16+
const time = lpad(diff.toString(), '.', 12);
17+
name = rpad(name, '.', 12);
18+
console.log(name + time + ' nanoseconds');
19+
};
20+
let count = 0;
21+
const next = () => (++count === num && done ? done() : 0);
1222
const result = [];
1323
let i;
14-
for (i = 0; i < PRE_COUNT; i++) result.push(fn());
24+
for (i = 0; i < PRE_COUNT; i++) result.push(fn(() => {}));
1525
const begin = process.hrtime();
16-
for (i = 0; i < count; i++) result.push(fn());
17-
const end = process.hrtime(begin);
18-
const diff = end[0] * 1e9 + end[1];
19-
const time = lpad(diff.toString(), '.', 12);
20-
name = rpad(name, '.', 12);
21-
console.log(name + time + ' nanoseconds');
26+
for (i = 0; i < num; i++) result.push(fn(next));
2227
};

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(10000, 'Functor', () => test(chainFunctor));
10+
benchmark.do(20000, '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(10000, 'Promise', () => test(chainPromise));
10+
benchmark.do(20000, 'Promise', (done) => test(chainPromise, 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(10000, 'Prototype', () => test(chainPrototype));
10+
benchmark.do(20000, 'Prototype', (done) => test(chainPrototype, done));

0 commit comments

Comments
 (0)