Skip to content

Commit 02604a6

Browse files
authored
perf(scripts): improve performance benchmarking accuracy (#36)
- Replaces deprecated [benchmark](https://github.com/bestiejs/benchmark.js) with [tinybench](https://github.com/tinylibs/tinybench) - Updates `scripts/perf.js` to use tinybench - Enforces warm-up and garbage collection (gc between runs may not be needed, but it should minimize interference) ```shell npm run perf > @jmespath-community/jmespath@1.1.4 perf > node --expose-gc scripts/perf.js ┌─────────┬─────────────────────────────────┬──────────────┬────────────────────┬──────────┬──────────┐ │ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │ ├─────────┼─────────────────────────────────┼──────────────┼────────────────────┼──────────┼──────────┤ │ 0 │ 'Parser#single_expr' │ '14,010,279' │ 71.37616207528444 │ '±0.74%' │ 14010280 │ │ 1 │ 'Parser#single_subexpr' │ '7,064,237' │ 141.55808538731404 │ '±0.06%' │ 7064238 │ │ 2 │ 'Parser#deeply_nested_50' │ '340,225' │ 2939.2295062691283 │ '±0.57%' │ 340226 │ │ 3 │ 'Parser#deeply_nested_50_index' │ '196,826' │ 5080.625386760879 │ '±0.52%' │ 196827 │ │ 4 │ 'Parser#basic_list_projection' │ '3,958,033' │ 252.6507331664748 │ '±0.61%' │ 3958034 │ └─────────┴─────────────────────────────────┴──────────────┴────────────────────┴──────────┴──────────┘ ``` <!-- ps-id: 5d145054-8cd2-42df-abf6-bd252e64d9ee -->
1 parent 716c0f0 commit 02604a6

File tree

3 files changed

+48
-50
lines changed

3 files changed

+48
-50
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"lint:fix": "npm run lint -- --fix",
4242
"prebuild": "npx rimraf dist",
4343
"build": "npx tsc --outDir dist/lib -d --module commonjs && npx rollup -c rollup.config.ts",
44-
"perf": "node scripts/perf.js",
44+
"perf": "node --expose-gc scripts/perf.js",
4545
"start": "npx rollup -c rollup.config.ts -w",
4646
"test": "npx jest --coverage",
4747
"test:watch": "npx jest --coverage --watch",
@@ -59,7 +59,6 @@
5959
"@types/jest": "^29.4.0",
6060
"@typescript-eslint/eslint-plugin": "^4.26.0",
6161
"@typescript-eslint/parser": "^4.26.0",
62-
"benchmark": "^2.1.4",
6362
"clean-publish": "^3.4.5",
6463
"coveralls-next": "^4.2.0",
6564
"eslint": "^7.27.0",
@@ -76,6 +75,7 @@
7675
"rollup": "^2.50.5",
7776
"rollup-plugin-typescript2": "^0.34.1",
7877
"shelljs": "^0.8.4",
78+
"tinybench": "^2.5.1",
7979
"ts-jest": "^29.0.5",
8080
"ts-node": "^10.9.1",
8181
"typescript": "^4.3.2"

scripts/perf.js

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
1-
const jmespath = require('../dist/lib')
2-
const Benchmark = require('benchmark');
3-
const suite = new Benchmark.Suite;
1+
const jmespath = require('../dist/lib');
2+
const { Bench } = require('tinybench');
43

5-
// add tests
6-
suite.add('Parser#single_expr', function() {
7-
jmespath.compile("foo");
8-
})
9-
.add('Parser#single_subexpr', function() {
10-
jmespath.compile("foo.bar");
11-
})
12-
.add('Parser#deeply_nested_50', function() {
13-
jmespath.compile("j49.j48.j47.j46.j45.j44.j43.j42.j41.j40.j39.j38.j37.j36.j35.j34.j33.j32.j31.j30.j29.j28.j27.j26.j25.j24.j23.j22.j21.j20.j19.j18.j17.j16.j15.j14.j13.j12.j11.j10.j9.j8.j7.j6.j5.j4.j3.j2.j1.j0");
4+
async function runBenchmarks() {
145

15-
})
16-
.add('Parser#deeply_nested_50_index', function() {
17-
jmespath.compile("[49][48][47][46][45][44][43][42][41][40][39][38][37][36][35][34][33][32][31][30][29][28][27][26][25][24][23][22][21][20][19][18][17][16][15][14][13][12][11][10][9][8][7][6][5][4][3][2][1][0]");
18-
})
19-
.add('Parser#basic_list_projection', function() {
20-
jmespath.compile("foo[*].bar");
21-
})
22-
.on('cycle', function(event) {
23-
const bench = event.target;
24-
const mean = bench.stats.mean * 1000;
25-
// const variance = bench.stats.variance * 1000000;
26-
let result = 'Mean time: ' + mean.toFixed(6) + 'msec ';
27-
result += event.target.toString();
28-
console.log(result);
29-
})
30-
.on('complete', function() {
31-
})
32-
// run async
33-
.run({ 'async': false });
6+
const bench = new Bench({
7+
name: 'typescript-jmespath benchmarks',
8+
warmupTime: 200,
9+
warmup: true,
10+
setup: (_task, mode) => {
11+
if (mode === 'warmup' && typeof
12+
global.gc === 'function') {
13+
global.gc();
14+
}
15+
},
16+
time: 1000,
17+
})
18+
19+
bench
20+
.add('Parser#single_expr', () => {
21+
jmespath.compile("foo");
22+
})
23+
.add('Parser#single_subexpr', () => {
24+
jmespath.compile("foo.bar");
25+
})
26+
.add('Parser#deeply_nested_50', () => {
27+
jmespath.compile("j49.j48.j47.j46.j45.j44.j43.j42.j41.j40.j39.j38.j37.j36.j35.j34.j33.j32.j31.j30.j29.j28.j27.j26.j25.j24.j23.j22.j21.j20.j19.j18.j17.j16.j15.j14.j13.j12.j11.j10.j9.j8.j7.j6.j5.j4.j3.j2.j1.j0");
28+
})
29+
.add('Parser#deeply_nested_50_index', () => {
30+
jmespath.compile("[49][48][47][46][45][44][43][42][41][40][39][38][37][36][35][34][33][32][31][30][29][28][27][26][25][24][23][22][21][20][19][18][17][16][15][14][13][12][11][10][9][8][7][6][5][4][3][2][1][0]");
31+
})
32+
.add('Parser#basic_list_projection', () => {
33+
jmespath.compile("foo[*].bar");
34+
});
35+
36+
await bench.run();
37+
console.table(bench.table());
38+
}
39+
40+
runBenchmarks().catch(console.error);

0 commit comments

Comments
 (0)