isaacs/minimatch — pull request #32 by kevinsawicki (2014-04-11 22:23:57+00:00)
Title: Look up basename using already split segments

Previously path.basename was used in conjunction with joining and splitting the segments array which was much slower than just looking up the basename from the already split segments array.

## Results

Ran the following simple benchmark matching 6 patterns against 100k random strings that contains 5 segments each with the `matchBase` option set to true.

**Results using `path.basename`:** 6 seconds
**Results using lookup in array:** 500 milliseconds

```js
var paths = [];

for (var i = 0; i < 100000; i++) {
paths.push('/' + Math.random() + '/' + Math.random() + '/' + Math.random() + '/' + Math.random() + '/' + Math.random())
}

var Minimatch = require('./minimatch').Minimatch;

var patterns = ['a/b/c', 'd/e/f', 'g/e/h', 'i', 'j', 'k'].map(function(pattern) {
return new Minimatch(pattern, {matchBase: true})
});

var startTime = Date.now()
for (var i = 0 ; i < paths.length; i++) {
for (var j = 0; j < patterns.length; j++) {
patterns[j].match(paths[i])
}
}

console.log(Date.now() - startTime);

```