-
-
Save jtyjty99999/44475bfbb3a74481a8834225d2db037c to your computer and use it in GitHub Desktop.
Revisions
-
jtyjty99999 created this gist
Jun 15, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ 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 });