随手写了一个文件搜索,发现性能超过Windows自带的
find.js如下,直接运行:第一个参数是查找范围,须以/
结尾,第二个参数是用来匹配文件(夹)名的正则表达式,i
开头则不分大小写;Windows系统最好用管理员身份运行,否则find.js不能区分文件夹和它的链接,Linux不清楚;搜索结果直接log在console里。
'use strict';
const FS=require('fs');
const FS_readdirSync=FS.readdirSync;
const FS_lstatSync=FS.lstatSync;
const log1=console.log.bind(console);
const Date_now=Date.now;
const push=Function.prototype.apply.bind(Array.prototype.push);
const $=process.argv[3];
const test=
$.startsWith('i')
?RegExp.prototype.test.bind(new RegExp($.slice(1),'iu'))
:RegExp.prototype.test.bind(new RegExp($,'u'));
let time2;
const log=function(msg){
if(time2){
time2=Date_now();
}else{
time2=Date_now();
log1('找出第一条结果的用时:'+(time2-time1)+'ms');
}
log1(msg);
};
const list=[process.argv[2]];
const search1=function(path){
const list=[];
for(let name of FS_readdirSync(path)){
try{
const pathname=path+name;
if(FS_lstatSync(pathname).isDirectory()){
if(test(name)){
log(pathname+'/');
push(list,search1(pathname+'/'));
}else{
list.push(pathname+'/');
}
}else if(test(name))log(pathname);
}catch(error){}
}
return list;
};
const search2=function(path){
try{
const list1=search1(path);
if(list1.length<5){
for(let path of list1){
search2(path);
}
}else{
push(list,list1);
}
}catch(error){};
};
const time1=Date_now();
for(let i=0;list[i];i+=1){
search2(list[i]);
delete list[i];
}
log1('从开始搜索到找出最后一条结果的用时:'+(time2-time1)+'ms');
JS既然是最好的语言,超越Windows不是很正常嘛?