Skip to content

Instantly share code, notes, and snippets.

@endiliey
Created May 19, 2019 08:51
Show Gist options
  • Save endiliey/9b0879291d3b9941a35e25b1fe47be5a to your computer and use it in GitHub Desktop.
Save endiliey/9b0879291d3b9941a35e25b1fe47be5a to your computer and use it in GitHub Desktop.
benchmark path.basename(file, path.extname(file)) vs path.parse(file).name
const path = require('path');
const Benchmark = require('benchmark');
const suite = new Benchmark.Suite();
const files = [
'C:\\temp\\myfile.html',
'a.md',
'hello/super.js',
'test.ts',
'superman/hello/haha.md',
];
// add tests
suite
.add('path parse', function() {
files.forEach(file => {
path.parse(file).name;
});
})
.add('path basename + path extname', function() {
files.forEach(file => {
path.basename(file, path.extname(file));
});
})
// 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: true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment