perf: use + operator

instead of Array.push()
https://stackoverflow.com/a/55409094
This commit is contained in:
curbengh 2019-12-16 08:38:48 +00:00
parent 1fdac53793
commit 7eda55a1aa
1 changed files with 9 additions and 9 deletions

View File

@ -117,12 +117,12 @@ function minifySvg () {
return Promise.all((micromatch(routeList, include, globOptions)).map((path) => { return Promise.all((micromatch(routeList, include, globOptions)).map((path) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const assetPath = route.get(path) const assetPath = route.get(path)
const assetTxt = [] let assetTxt = ''
assetPath.on('data', (chunk) => (assetTxt.push(chunk))) assetPath.on('data', (chunk) => (assetTxt += chunk))
assetPath.on('end', async () => { assetPath.on('end', async () => {
if (assetTxt.length) { if (assetTxt.length) {
try { try {
const result = await new Svgo(options).optimize(assetTxt.join('')) const result = await new Svgo(options).optimize(assetTxt)
if (options.logger) verbose.call(this, assetTxt.join().toString(), result.data, path, 'svg') if (options.logger) verbose.call(this, assetTxt.join().toString(), result.data, path, 'svg')
resolve(route.set(path, result.data)) resolve(route.set(path, result.data))
} catch (err) { } catch (err) {
@ -151,13 +151,13 @@ function gzipFn () {
return Promise.all((micromatch(routeList, include, globOptions)).map((path) => { return Promise.all((micromatch(routeList, include, globOptions)).map((path) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const assetPath = route.get(path) const assetPath = route.get(path)
const assetTxt = [] let assetTxt = ''
assetPath.on('data', (chunk) => (assetTxt.push(chunk))) assetPath.on('data', (chunk) => (assetTxt += chunk))
assetPath.on('end', async () => { assetPath.on('end', async () => {
if (assetTxt.length) { if (assetTxt.length) {
try { try {
// TODO: Drop Buffer // TODO: Drop Buffer
const input = Buffer.from(assetTxt.join(''), 'utf-8') const input = Buffer.from(assetTxt, 'utf-8')
const result = await gzip(input, { level: zlib.constants.Z_BEST_COMPRESSION }) const result = await gzip(input, { level: zlib.constants.Z_BEST_COMPRESSION })
if (options.logger) verbose.call(this, input, result.toString(), path, 'gzip') if (options.logger) verbose.call(this, input, result.toString(), path, 'gzip')
resolve(route.set(path + '.gz', result)) resolve(route.set(path + '.gz', result))
@ -187,12 +187,12 @@ function brotliFn () {
return Promise.all((micromatch(routeList, include, globOptions)).map((path) => { return Promise.all((micromatch(routeList, include, globOptions)).map((path) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const assetPath = route.get(path) const assetPath = route.get(path)
const assetTxt = [] let assetTxt = ''
assetPath.on('data', (chunk) => (assetTxt.push(chunk))) assetPath.on('data', (chunk) => (assetTxt += chunk))
assetPath.on('end', async () => { assetPath.on('end', async () => {
if (assetTxt.length) { if (assetTxt.length) {
try { try {
const input = Buffer.from(assetTxt.join(''), 'utf-8') const input = Buffer.from(assetTxt, 'utf-8')
const result = await br(input) const result = await br(input)
if (options.logger) verbose.call(this, input, result.toString(), path, 'brotli') if (options.logger) verbose.call(this, input, result.toString(), path, 'brotli')
resolve(route.set(path + '.br', result)) resolve(route.set(path + '.br', result))