Skip to content

Commit 645312a

Browse files
committed
test: compare performance with previous version
1 parent 2317a85 commit 645312a

14 files changed

Lines changed: 131 additions & 49 deletions

benchmark/log-versions.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
const momentPkg = require('moment/package.json');
22
const dateFnsPkg = require('date-fns/package.json');
3+
const tsDatePrevPkg = require('ts-date/package.json');
34
const cpus = require('os').cpus();
5+
const options = require('./options');
46

57
console.log('%s x%s', cpus[0].model, cpus.length);
6-
console.log('>node -v\n' + process.version);
8+
console.log('>node -v\n' + process.version);
79
console.log();
8-
console.log('moment.js %s', momentPkg.version);
9-
console.log('date-fns %s', dateFnsPkg.version);
10+
if (options.compareWithPrevious) {
11+
console.log('ts-date@%s', tsDatePrevPkg.version);
12+
console.log('ts-date@next');
13+
} else {
14+
console.log('moment.js@%s', momentPkg.version);
15+
console.log('date-fns@%s', dateFnsPkg.version);
16+
}
1017
console.log('');

benchmark/names.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const tsDatePrevPkg = require('ts-date/package.json');
2+
3+
const names = {
4+
moment: 'moment',
5+
momentCached: 'moment cached',
6+
dateFns: 'date-fns',
7+
tsDatePrev: 'ts-date@' + tsDatePrevPkg.version,
8+
tsDate: 'ts-date',
9+
};
10+
11+
module.exports = names;

benchmark/options.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const options = {
2+
compareWithPrevious: process.argv.includes('--compare-with-previous'),
3+
isDebug: process.argv.includes('--debug'),
4+
};
5+
6+
module.exports = options;

benchmark/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benchmark/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
"dependencies": {
99
"benchmark": "^2.1.4",
1010
"date-fns": "^2.9.0",
11-
"moment": "^2.24.0"
11+
"moment": "^2.24.0",
12+
"ts-date": "^2.3.3"
1213
},
1314
"devDependencies": {},
1415
"scripts": {
1516
"update-readme": "node ./index.js > README.md",
17+
"compare-with-previous": "node ./index.js --compare-with-previous",
18+
"debug": "node ./index.js --compare-with-previous --debug",
1619
"start": "node ./index.js"
1720
}
1821
}

benchmark/runners.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1-
function runSuite(suite, isDebug) {
1+
const options = require('./options');
2+
const names = require('./names');
3+
4+
function runSuite(suiteOrig) {
5+
const suite = suiteOrig.filter(s => {
6+
if (options.compareWithPrevious) {
7+
return s.name === names.tsDatePrev || s.name === names.tsDate;
8+
} else {
9+
return s.name !== names.tsDatePrev;
10+
}
11+
});
12+
suite.name = suiteOrig.name;
13+
14+
suite.on('start', logStart);
15+
if (options.isDebug) {
16+
suite.on('start', logMethodResults);
17+
}
18+
suite.on('cycle', logResults);
19+
suite.on('complete', logComplete);
20+
suite.run();
221
return suite
3-
.on('start', logStart)
4-
.on('start', logMethodResults(isDebug))
5-
.on('cycle', logResults)
6-
.on('complete', logComplete)
7-
.run();
822
}
923
let firstCycle = null;
1024

@@ -33,13 +47,10 @@ function logStart() {
3347
firstCycle = null;
3448
}
3549

36-
function logMethodResults(isDebug) {
37-
return function() {
38-
if (!isDebug) return;
39-
this.forEach(s => {
40-
console.log(padl(32, `result for "${s.name}" is:`), s.fn());
41-
});
42-
};
50+
function logMethodResults() {
51+
this.forEach(s => {
52+
console.log(padl(32, `result for "${s.name}" is:`), s.fn());
53+
});
4354
}
4455

4556
function logComplete() {

benchmark/suites/add-days.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const Benchmark = require('benchmark');
22
const nextRandomDate = require('../next-random-date');
3+
const names = require('../names');
34
const tsDate = require('../../dist/locale/en/index');
5+
const tsDatePrev = require('ts-date');
46
const moment = require('moment');
57
const dateFns = require('date-fns');
68

7-
89
let date;
910
let dateMoment;
1011
function onCycle() {
@@ -16,16 +17,19 @@ onCycle();
1617

1718
const suite = new Benchmark.Suite('Adding fixed amount of days');
1819
suite
19-
.add('moment', function() {
20+
.add(names.moment, function() {
2021
return moment(date).add(2, 'd')
2122
}, {onCycle})
22-
.add('moment cached', function() {
23+
.add(names.momentCached, function() {
2324
return dateMoment.add(2, 'd')
2425
}, {onCycle})
25-
.add('date-fns', function() {
26+
.add(names.dateFns, function() {
2627
return dateFns.addDays(date, 2)
2728
}, {onCycle})
28-
.add('ts-date', function() {
29+
.add(names.tsDatePrev, function() {
30+
return tsDatePrev.addDate(date, 2)
31+
}, {onCycle})
32+
.add(names.tsDate, function() {
2933
return tsDate.addDate(date, 2)
3034
}, {onCycle})
3135
;

benchmark/suites/add-hours.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const Benchmark = require('benchmark');
22
const nextRandomDate = require('../next-random-date');
3+
const names = require('../names');
34
const tsDate = require('../../dist/locale/en/index');
5+
const tsDatePrev = require('ts-date');
46
const moment = require('moment');
57
const dateFns = require('date-fns');
68

@@ -16,16 +18,19 @@ onCycle();
1618

1719
const suite = new Benchmark.Suite('Adding fixed amount of hours');
1820
suite
19-
.add('moment', function() {
21+
.add(names.moment, function() {
2022
return moment(date).add(2, 'hours')
2123
}, {onCycle})
22-
.add('moment cached', function() {
24+
.add(names.momentCached, function() {
2325
return dateMoment.add(2, 'hours')
2426
}, {onCycle})
25-
.add('date-fns', function() {
27+
.add(names.dateFns, function() {
2628
return dateFns.addHours(date, 2)
2729
}, {onCycle})
28-
.add('ts-date', function() {
30+
.add(names.tsDatePrev, function() {
31+
return tsDatePrev.addHours(date, 2)
32+
}, {onCycle})
33+
.add(names.tsDate, function() {
2934
return tsDate.addHours(date, 2)
3035
}, {onCycle})
3136
;

benchmark/suites/diff-days.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const Benchmark = require('benchmark');
22
const nextRandomDate = require('../next-random-date');
3+
const names = require('../names');
34
const tsDate = require('../../dist/locale/en/index');
5+
const tsDatePrev = require('ts-date');
46
const moment = require('moment');
57
const dateFns = require('date-fns');
68

@@ -20,16 +22,19 @@ onCycle();
2022

2123
const suite = new Benchmark.Suite('Difference in days between two dates');
2224
suite
23-
.add('moment', function() {
25+
.add(names.moment, function() {
2426
return moment(d1).diff(d2, 'd')
2527
}, {onCycle})
26-
.add('moment cached', function() {
28+
.add(names.momentCached, function() {
2729
return d1Moment.diff(d2, 'd')
2830
}, {onCycle})
29-
.add('date-fns', function() {
31+
.add(names.dateFns, function() {
3032
return dateFns.differenceInDays(d1, d2)
3133
}, {onCycle})
32-
.add('ts-date', function() {
34+
.add(names.tsDatePrev, function() {
35+
return tsDatePrev.diffDate(d1, d2)
36+
}, {onCycle})
37+
.add(names.tsDate, function() {
3338
return tsDate.diffDate(d1, d2)
3439
}, {onCycle})
3540
;

benchmark/suites/diff-hours.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const Benchmark = require('benchmark');
22
const nextRandomDate = require('../next-random-date');
3+
const names = require('../names');
34
const tsDate = require('../../dist/locale/en/index');
5+
const tsDatePrev = require('ts-date');
46
const moment = require('moment');
57
const dateFns = require('date-fns');
68

@@ -20,16 +22,19 @@ onCycle();
2022

2123
const suite = new Benchmark.Suite('Difference in hours between two dates');
2224
suite
23-
.add('moment', function() {
25+
.add(names.moment, function() {
2426
return moment(d1).diff(d2, 'hours')
2527
}, {onCycle})
26-
.add('moment cached', function() {
28+
.add(names.momentCached, function() {
2729
return d1Moment.diff(d2, 'hours')
2830
}, {onCycle})
29-
.add('date-fns', function() {
31+
.add(names.dateFns, function() {
3032
return dateFns.differenceInHours(d1, d2)
3133
}, {onCycle})
32-
.add('ts-date', function() {
34+
.add(names.tsDatePrev, function() {
35+
return tsDatePrev.diffHours(d1, d2)
36+
}, {onCycle})
37+
.add(names.tsDate, function() {
3338
return tsDate.diffHours(d1, d2)
3439
}, {onCycle})
3540
;

0 commit comments

Comments
 (0)