File: bench.js

package info (click to toggle)
node-globby 13.1.3%2B~cs16.25.40-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,624 kB
  • sloc: javascript: 3,628; makefile: 3
file content (142 lines) | stat: -rw-r--r-- 2,801 bytes parent folder | download | duplicates (2)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import process from 'node:process';
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import Benchmark from 'benchmark';
import rimraf from 'rimraf';
import * as globbyMainBranch from '@globby/main-branch';
import gs from 'glob-stream';
import fastGlob from 'fast-glob';
import {globby, globbySync, globbyStream} from './index.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const BENCH_DIR = 'bench';

const runners = [
	{
		name: 'globby async (working directory)',
		run: globby,
	},
	{
		name: 'globby async (upstream/main)',
		run: globbyMainBranch.globby,
	},
	{
		name: 'globby sync (working directory)',
		run: globbySync,
	},
	{
		name: 'globby sync (upstream/main)',
		run: globbyMainBranch.globbySync,
	},
	{
		name: 'globby stream (working directory)',
		run: patterns => new Promise(resolve => {
			globbyStream(patterns).on('data', () => {}).on('end', resolve);
		}),
	},
	{
		name: 'globby stream (upstream/main)',
		run: patterns => new Promise(resolve => {
			globbyMainBranch.globbyStream(patterns).on('data', () => {}).on('end', resolve);
		}),
	},
	{
		name: 'glob-stream',
		run: patterns => new Promise(resolve => {
			gs(patterns).on('data', () => {}).on('end', resolve);
		}),
	},
	{
		name: 'fast-glob async',
		run: fastGlob,
	},
	{
		name: 'fast-glob sync',
		run: fastGlob.sync,
	},
];

const benchs = [
	{
		name: 'negative globs (some files inside dir)',
		patterns: [
			'a/*',
			'!a/c*',
		],
	},
	{
		name: 'negative globs (whole dir)',
		patterns: [
			'a/*',
			'!a/**',
		],
	},
	{
		name: 'multiple positive globs',
		patterns: [
			'a/*',
			'b/*',
		],
	},
];

const before = () => {
	process.chdir(__dirname);
	rimraf.sync(BENCH_DIR);
	fs.mkdirSync(BENCH_DIR);
	process.chdir(BENCH_DIR);

	const directories = ['a', 'b']
		.map(directory => `${directory}/`);

	for (const directory of directories) {
		fs.mkdirSync(directory);
		for (let i = 0; i < 500; i++) {
			fs.writeFileSync(directory + (i < 100 ? 'c' : 'd') + i, '');
		}
	}
};

const after = () => {
	process.chdir(__dirname);
	rimraf.sync(BENCH_DIR);
};

const suites = [];
for (const {name, patterns} of benchs) {
	const suite = new Benchmark.Suite(name, {
		onStart() {
			before();

			console.log(`[*] Started Benchmarks "${this.name}"`);
		},
		onCycle(event) {
			console.log(`[+] ${String(event.target)}`);
		},
		onComplete() {
			after();

			console.log(`\nFastest is ${this.filter('fastest').map('name')} \n`);
		},
	});

	for (const {name, run} of runners) {
		suite.add(name, run.bind(undefined, patterns));
	}

	suites.push(suite);
}

let index = 0;
const run = suite => {
	suite.on('complete', () => {
		const next = suites[++index];
		if (next) {
			run(next);
		}
	});
	suite.run({async: true});
};

run(suites[0]);