Skip to content

Commit

Permalink
Add benchmarks (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisblossom authored and sindresorhus committed Aug 23, 2019
1 parent 1299747 commit 9c72270
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
68 changes: 68 additions & 0 deletions benchmark.js
@@ -0,0 +1,68 @@
'use strict';
const path = require('path');
const Benchmark = require('benchmark');
const makeDir = require('make-dir');
const tempy = require('tempy');
const del = require('.');

const suite = new Benchmark.Suite('concurrency');

const tempDir = tempy.directory();

const fixtures = Array.from({length: 2000}, (x, index) => {
return path.resolve(tempDir, (index + 1).toString());
});

function createFixtures() {
for (const fixture of fixtures) {
makeDir.sync(path.resolve(tempDir, fixture));
}
}

const concurrencies = [1, 3, 5, 10, 15, 20, 50, 100, 200, 300, 400, 500, 1000, Infinity];

for (const concurrency of concurrencies) {
const name = `concurrency: ${concurrency.toString()}`;

suite.add({
name,
defer: true,
setup() {}, // This line breaks async await
async fn(deferred) {
// Can't use setup because it isn't called after every defer
// https://github.com/bestiejs/benchmark.js/issues/136
createFixtures();

const removedFiles = await del(['**/*'], {
cwd: tempDir,
concurrency
});

if (removedFiles.length !== fixtures.length) {
const error = new Error(
`"${name}": files removed: ${removedFiles.length}, expected: ${fixtures.length}`,
);

console.error(error);

del.sync(tempDir, {cwd: tempDir, force: true});

// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}

deferred.resolve();
}
});
}

suite
.on('cycle', event => {
console.log(String(event.target));
})
.on('complete', function () {
console.log(`Fastest is ${this.filter('fastest').map('name')}`);

del.sync(tempDir, {cwd: tempDir, force: true});
})
.run({async: true});
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -13,7 +13,8 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
"test": "xo && ava && tsd",
"bench": "node benchmark.js"
},
"files": [
"index.js",
Expand Down Expand Up @@ -53,6 +54,7 @@
},
"devDependencies": {
"ava": "^2.3.0",
"benchmark": "^2.1.4",
"make-dir": "^3.0.0",
"tempy": "^0.3.0",
"tsd": "^0.7.3",
Expand Down

0 comments on commit 9c72270

Please sign in to comment.