我想知道FileSystemManager.readFile的复杂度是什么样的
发布于 6 年前 作者 wenxiuying 14586 次浏览 来自 问答

假如我在第一次读取文件的时候将这些文件路径都存到一个csv下,第二次就从csv中读取这些文件路径,而不是通过

 FileSystemManager.readdirSync

或者

 FileSystemManager.readdir

来再次获取文件路径,效率会不会提高

1 回复
function cleanAllFiles(path, newAssets, finish) {
    fs.readdir({
        dirPath: path,
        success: function (res) {
            var files = res.files;
            (function next(idx) {
                if (idx < files.length) {
                    var dirPath = path + '/' + files[idx];
                    var stat = fs.statSync(dirPath);
                    if (stat.isDirectory()) {
                        cleanAllFiles(dirPath, newAssets, function () {
                            next(idx + 1);
                        });
                    } else {
                        // remove old assets
                        if (newAssets && newAssets.indexOf(dirPath) !== -1) {
                            next(idx + 1);
                            return;
                        }
                        fs.unlink({
                            filePath: dirPath,
                            success: function () {
                                cc.log('unlink local file ' + dirPath + ' successfully!');
                            },
                            fail: function (res) {
                                cc.warn('failed to unlink file(' + dirPath + '): ' + res ? res.errMsg : 'unknown error');
                            },
                            complete: function () {
                                next(idx + 1);
                            }
                        });
                    }
                } else {
                    finish && finish();
                }
 
            })(0);
        },
        fail: function (res) {
            finish && finish();
        },
    });
}

比如官方WX-Downloader.js里这个cleanAllFiles,假如我把这个readdir改成从预存的csv中读取,效率能提高吗

回到顶部