node能做的性能优化

开发中,我们就离不开性能优化,那么在使用node开发的时候,我们可以使用那些代码来优化性能呢

一.释放内存

当node运行检测到错误的时候,释放掉内存

http.get(str,(res)=>{
    if(...错误判断){
        error=new Error('数据异常')
    }
    if(error){
        console.log(error.message);
        //释放内存
        res.resume();
        return
    }
}

二.压缩

const fs=require('fs');
const zlib=require('zlib');
//createReadStream读取文件内容,zlib压缩文件内容,createWriteStream压缩内容导出文件
fs.createReadStream('test.txt')
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream('test.txt.gzip'))
原文地址:https://www.cnblogs.com/liuXiaoDi/p/12722682.html