joyent/node — pull request #6929 by joliss (2014-01-21 22:36:18+00:00)
Title: Improve POSIX path.join performance

Here is a simple benchmark:

./node -e "
var path = require('path');
var foo = 'foo', bar = 'bar';
function myJoin (foo, bar) {
// Naive implementation for comparison
return foo + '/' + bar;
}
console.time('join');
for (var i = 0; i < 1000000; i++) {
path.join(foo, bar);
}
console.timeEnd('join');
"

This commit reduces the time for path.join in the above benchmark from
2 us to .45 us. It is still much slower than the naive myJoin
implementation (.005 us), but it's progress at least.

There seems to be some duplicate work between join, normalize, and
normalizeArray, if people want to optimize it further.

Note that this only improves the POSIX implementation. The Windows
implementation uses .filter closures as well (and also regexes), so
there is much room for improvement, but I'm not trying to fix it here.

Thanks to @isaacs and @othiym23 for helping with this optimization.