Skip to content

Commit 1cd240a

Browse files
committed
Benchmark static methods
1 parent 85e9bd5 commit 1cd240a

File tree

1 file changed

+135
-16
lines changed

1 file changed

+135
-16
lines changed

benchmark.js

Lines changed: 135 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
"use strict";
2-
const inv = require("install-npm-version");
3-
const benchmarkVersion = "1.0.1";
2+
3+
const benchmarkVersion = [...process.argv].pop();
4+
5+
const noVersionMessage = `Aborted benchmark
6+
7+
Please specify a bench version. E.g: npm run benchmark -- 2.0.0
8+
`;
9+
10+
if (benchmarkVersion.match(/\d+\.\d+\.\d+/) === null) {
11+
console.log(noVersionMessage);
12+
return process.exit(1);
13+
}
14+
415
const benchmarkLib = `postcode@${benchmarkVersion}`;
516

617
const message = `
7-
Benchmarking master against ${benchmarkLib}
8-
18+
Benchmarking master against postcode@${benchmarkLib}
919
`;
1020

11-
inv
21+
console.log(message);
22+
23+
require("install-npm-version")
1224
.Install(benchmarkLib, { Verbosity: "debug" })
1325
.then(() => runBenchmarks())
1426
.catch(error => {
@@ -18,24 +30,131 @@ inv
1830

1931
const runBenchmarks = () => {
2032
const { Suite } = require("benchmark");
21-
const suite = new Suite();
33+
const newSuite = name => new Suite({ name });
2234

2335
const Postcode = require("./dist/index");
2436
const PreviousPostcode = require(benchmarkLib);
2537

2638
const extractInputs = ({ tests }) => tests.map(({ base }) => base);
2739

2840
const validation = extractInputs(require("./test/data/validation.json"));
29-
suite
30-
.add(`(version: ${benchmarkVersion}) Postcode#valid`, () => {
31-
validation.forEach(postcode => {
32-
const result = new PreviousPostcode(postcode).valid();
33-
});
34-
})
35-
.add("(version: master) Postcode.isValid", () => {
36-
validation.forEach(postcode => {
37-
const result = Postcode.isValid(postcode);
38-
});
41+
const areas = extractInputs(require("./test/data/areas.json"));
42+
const districts = extractInputs(require("./test/data/districts.json"));
43+
const incodes = extractInputs(require("./test/data/incodes.json"));
44+
const normalisation = extractInputs(
45+
require("./test/data/normalisation.json")
46+
);
47+
const outcodes = extractInputs(require("./test/data/outcodes.json"));
48+
const sectors = extractInputs(require("./test/data/sectors.json"));
49+
const subDistricts = extractInputs(require("./test/data/sub-districts.json"));
50+
const units = extractInputs(require("./test/data/units.json"));
51+
52+
newSuite(".isValid")
53+
.add(benchmarkVersion, () => {
54+
validation.forEach(postcode => PreviousPostcode.isValid(postcode));
55+
})
56+
.add("master", () => {
57+
validation.forEach(postcode => Postcode.isValid(postcode));
58+
})
59+
.on("cycle", event => {
60+
console.log(String(event.target));
61+
})
62+
.run({});
63+
64+
newSuite(".toArea")
65+
.add(benchmarkVersion, () => {
66+
areas.forEach(postcode => PreviousPostcode.toArea(postcode));
67+
})
68+
.add("master", () => {
69+
areas.forEach(postcode => Postcode.toArea(postcode));
70+
})
71+
.on("cycle", event => {
72+
console.log(String(event.target));
73+
})
74+
.run({});
75+
76+
newSuite(".toDistrict")
77+
.add(benchmarkVersion, () => {
78+
districts.forEach(postcode => PreviousPostcode.toDistrict(postcode));
79+
})
80+
.add("master", () => {
81+
districts.forEach(postcode => Postcode.toDistrict(postcode));
82+
})
83+
.on("cycle", event => {
84+
console.log(String(event.target));
85+
})
86+
.run({});
87+
88+
newSuite(".toNormalised")
89+
.add(benchmarkVersion, () => {
90+
incodes.forEach(postcode => PreviousPostcode.toIncode(postcode));
91+
})
92+
.add("master", () => {
93+
incodes.forEach(postcode => Postcode.toIncode(postcode));
94+
})
95+
.on("cycle", event => {
96+
console.log(String(event.target));
97+
})
98+
.run({});
99+
100+
newSuite(".toNormalised")
101+
.add(benchmarkVersion, () => {
102+
normalisation.forEach(postcode =>
103+
PreviousPostcode.toNormalised(postcode)
104+
);
105+
})
106+
.add("master", () => {
107+
normalisation.forEach(postcode => Postcode.toNormalised(postcode));
108+
})
109+
.on("cycle", event => {
110+
console.log(String(event.target));
111+
})
112+
.run({});
113+
114+
newSuite(".toOutcode")
115+
.add(benchmarkVersion, () => {
116+
outcodes.forEach(postcode => PreviousPostcode.toOutcode(postcode));
117+
})
118+
.add("master", () => {
119+
outcodes.forEach(postcode => Postcode.toOutcode(postcode));
120+
})
121+
.on("cycle", event => {
122+
console.log(String(event.target));
123+
})
124+
.run({});
125+
126+
newSuite(".toSector")
127+
.add(benchmarkVersion, () => {
128+
sectors.forEach(postcode => PreviousPostcode.toSector(postcode));
129+
})
130+
.add("master", () => {
131+
sectors.forEach(postcode => Postcode.toSector(postcode));
132+
})
133+
.on("cycle", event => {
134+
console.log(String(event.target));
135+
})
136+
.run({});
137+
138+
newSuite("toSubDistrict")
139+
.add(benchmarkVersion, () => {
140+
subDistricts.forEach(postcode =>
141+
PreviousPostcode.toSubDistrict(postcode)
142+
);
143+
})
144+
.add("master", () => {
145+
subDistricts.forEach(postcode => Postcode.toSubDistrict(postcode));
146+
})
147+
.on("cycle", event => {
148+
console.log(String(event.target));
149+
})
150+
.run({});
151+
152+
newSuite("toUnit")
153+
.add(benchmarkVersion, () => {
154+
units.forEach(postcode => PreviousPostcode.toUnit(postcode));
155+
})
156+
.add("master", () => {
157+
units.forEach(postcode => Postcode.toUnit(postcode));
39158
})
40159
.on("cycle", event => {
41160
console.log(String(event.target));

0 commit comments

Comments
 (0)