Skip to content

Commit

Permalink
chore: benchmarks for path2String & path2Absolute (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoiver authored Aug 1, 2024
1 parent c499a30 commit 43574e8
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,27 @@ isPointInPolygon(polygon, 0, 0); // true
isPolygonsIntersect(polygon1, polygon2);
```

## Benchmarks

Build first.

```bash
yarn run benchmarks
```

We can get the following output in the console, it can be seen that the same method from 5.0 is ~3 times faster than 4.0.

```js
// logs
// Path2String#4.0 x 14,795 ops/sec ±3.35% (79 runs sampled)
// Path2String#5.0 x 51,710 ops/sec ±2.05% (85 runs sampled)
// Fastest is Path2String#5.0

// Path2Absolute#4.0 x 14,524 ops/sec ±2.55% (80 runs sampled)
// Path2Absolute#5.0 x 35,120 ops/sec ±3.10% (81 runs sampled)
// Fastest is Path2Absolute#5.0
```

## License

MIT@[AntV](https://github.com/antvis).
32 changes: 32 additions & 0 deletions benchmarks/path-2-absolute.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite();

var { path2Absolute: path2Absolute40 } = require('@antv/path-util'); // 4.0
var { path2Absolute: path2Absolute50 } = require('../lib'); // 5.0

// add tests
suite
.add('Path2Absolute#4.0', function () {
path2Absolute40(
'M2 4a2 2 0 0 0 -2 2v6a2 2 0 0 0 2 2h12a2 2 0 0 0 2 -2v-6a2 2 0 0 0 -2 -2h-1.172a2 2 0 0 1 -1.414 -0.586l-0.828 -0.828a2 2 0 0 0 -1.414 -0.586h-2.344a2 2 0 0 0 -1.414 0.586l-0.828 0.828a2 2 0 0 1 -1.414 0.586h-1.172zM10.5 8.5a2.5 2.5 0 0 0 -5 0a2.5 2.5 0 1 0 5 0zM2.5 6a0.5 0.5 0 0 1 0 -1a0.5 0.5 0 1 1 0 1zM11.5 8.5a3.5 3.5 0 1 1 -7 0a3.5 3.5 0 0 1 7 0z',
);
})
.add('Path2Absolute#5.0', function () {
path2Absolute50(
'M2 4a2 2 0 0 0 -2 2v6a2 2 0 0 0 2 2h12a2 2 0 0 0 2 -2v-6a2 2 0 0 0 -2 -2h-1.172a2 2 0 0 1 -1.414 -0.586l-0.828 -0.828a2 2 0 0 0 -1.414 -0.586h-2.344a2 2 0 0 0 -1.414 0.586l-0.828 0.828a2 2 0 0 1 -1.414 0.586h-1.172zM10.5 8.5a2.5 2.5 0 0 0 -5 0a2.5 2.5 0 1 0 5 0zM2.5 6a0.5 0.5 0 0 1 0 -1a0.5 0.5 0 1 1 0 1zM11.5 8.5a3.5 3.5 0 1 1 -7 0a3.5 3.5 0 0 1 7 0z',
);
})
// add listeners
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ async: true });

// logs:
// Path2Absolute#4.0 x 14,524 ops/sec ±2.55% (80 runs sampled)
// Path2Absolute#5.0 x 35,120 ops/sec ±3.10% (81 runs sampled)
// Fastest is Path2Absolute#5.0
32 changes: 32 additions & 0 deletions benchmarks/path-2-string.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite();

var { parsePathString: parsePathString40 } = require('@antv/path-util'); // 4.0
var { parsePathString: parsePathString50 } = require('../lib/path/parser/parse-path-string'); // 5.0

// add tests
suite
.add('Path2String#4.0', function () {
parsePathString40(
'M2 4a2 2 0 0 0 -2 2v6a2 2 0 0 0 2 2h12a2 2 0 0 0 2 -2v-6a2 2 0 0 0 -2 -2h-1.172a2 2 0 0 1 -1.414 -0.586l-0.828 -0.828a2 2 0 0 0 -1.414 -0.586h-2.344a2 2 0 0 0 -1.414 0.586l-0.828 0.828a2 2 0 0 1 -1.414 0.586h-1.172zM10.5 8.5a2.5 2.5 0 0 0 -5 0a2.5 2.5 0 1 0 5 0zM2.5 6a0.5 0.5 0 0 1 0 -1a0.5 0.5 0 1 1 0 1zM11.5 8.5a3.5 3.5 0 1 1 -7 0a3.5 3.5 0 0 1 7 0z',
);
})
.add('Path2String#5.0', function () {
parsePathString50(
'M2 4a2 2 0 0 0 -2 2v6a2 2 0 0 0 2 2h12a2 2 0 0 0 2 -2v-6a2 2 0 0 0 -2 -2h-1.172a2 2 0 0 1 -1.414 -0.586l-0.828 -0.828a2 2 0 0 0 -1.414 -0.586h-2.344a2 2 0 0 0 -1.414 0.586l-0.828 0.828a2 2 0 0 1 -1.414 0.586h-1.172zM10.5 8.5a2.5 2.5 0 0 0 -5 0a2.5 2.5 0 1 0 5 0zM2.5 6a0.5 0.5 0 0 1 0 -1a0.5 0.5 0 1 1 0 1zM11.5 8.5a3.5 3.5 0 1 1 -7 0a3.5 3.5 0 0 1 7 0z',
);
})
// add listeners
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ async: true });

// logs:
// Path2String#4.0 x 14,795 ops/sec ±3.35% (79 runs sampled)
// Path2String#5.0 x 51,710 ops/sec ±2.05% (85 runs sampled)
// Fastest is Path2String#5.0
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@
"build:esm": "rimraf ./esm && tsc --module ESNext --outDir esm",
"build": "run-p build:*",
"ci": "run-s lint test build",
"prepublishOnly": "npm run ci"
"prepublishOnly": "npm run ci",
"benchmarks": "node benchmarks/path-2-string.test && node benchmarks/path-2-absolute.test"
},
"keywords": [
"util",
"antv"
],
"devDependencies": {
"@antv/path-util": "^2.0.15",
"@commitlint/cli": "^11.0.0",
"@rollup/plugin-commonjs": "^22.0.0",
"@rollup/plugin-node-resolve": "^13.3.0",
"@rollup/plugin-typescript": "^8.3.2",
"@types/jest": "^26.0.20",
"@typescript-eslint/eslint-plugin": "^4.18.0",
"@typescript-eslint/parser": "^4.18.0",
"benchmark": "^2.1.4",
"eslint": "^7.22.0",
"eslint-plugin-import": "^2.22.1",
"husky": "^5.0.9",
Expand Down

0 comments on commit 43574e8

Please sign in to comment.