Skip to content

Instantly share code, notes, and snippets.

@jtyjty99999
Created June 15, 2017 02:09
Show Gist options
  • Save jtyjty99999/44475bfbb3a74481a8834225d2db037c to your computer and use it in GitHub Desktop.
Save jtyjty99999/44475bfbb3a74481a8834225d2db037c to your computer and use it in GitHub Desktop.
join with normalize
const benchmark = require('benchmark');
const suite = new benchmark.Suite;
const join = require('path').join;
const normalize = require('path').normalize;
const p = '/ddd/../../../aaa/bbb/ccc';
function getRandomStr() {
return '__' + Math.floor(Math.random() * 16777215).toString(16) + '__';
}
const judgeString = getRandomStr();
function isSafePath(path, ctx) {
if (join(judgeString, path).indexOf(judgeString) === -1) {
return false;
}
return true;
};
function isSafePath2(path, ctx) {
path = path.slice(1);
return normalize(path).startsWith('../');
};
// add tests
suite.add('join#test', function() {
isSafePath(p);
})
.add('normalize#test', function() {
isSafePath2(p);
})
// 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
.run({ 'async': true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment