forked from Turfjs/turf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.ts
70 lines (59 loc) · 2.14 KB
/
bench.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { FeatureCollection, Polygon, MultiPolygon, Feature } from "geojson";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import Benchmark, { Event } from "benchmark";
import { mask as turfMask } from "./index.js";
import clone from "@turf/clone";
// type guard to narrow the type of the fixtures
const isPolygonFeature = (
feature: Feature<Polygon | MultiPolygon>
): feature is Feature<Polygon> => feature.geometry.type === "Polygon";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const SKIP = ["multi-polygon.geojson", "overlapping.geojson"];
const suite = new Benchmark.Suite("turf-mask");
const directories = {
in: path.join(__dirname, "test", "in") + path.sep,
out: path.join(__dirname, "test", "out") + path.sep,
};
let fixtures = fs.readdirSync(directories.in).map((filename) => {
return {
filename,
name: path.parse(filename).name,
geojson: loadJsonFileSync(
path.join(directories.in, filename)
) as FeatureCollection<Polygon | MultiPolygon>,
};
});
for (const { name, filename, geojson } of fixtures) {
if (SKIP.includes(filename)) continue;
const [polygon, masking] = geojson.features;
if (!masking || !isPolygonFeature(masking)) {
throw new Error(
"Fixtures should have two features: an input feature and a Polygon masking feature."
);
}
const getSuite = ({ mutate }: { mutate: boolean }) => ({
name: `${name} (mutate = ${mutate})`,
fn: () => {
// We clone the inputs to prevent tests from affecting each other
turfMask(clone(polygon), clone(masking), { mutate });
},
});
suite.add(getSuite({ mutate: false }));
suite.add(getSuite({ mutate: true }));
}
/**
* Benchmark Results:
*
* basic (mutate = false) x 294,373 ops/sec ±0.25% (95 runs sampled)
* basic (mutate = true) x 307,397 ops/sec ±0.13% (97 runs sampled)
* mask-outside (mutate = false) x 100,575 ops/sec ±0.55% (97 runs sampled)
* mask-outside (mutate = true) x 103,180 ops/sec ±0.40% (94 runs sampled)
*/
suite
.on("cycle", (event: Event) => {
console.log(String(event.target));
})
.run();