perf: 60ms improvement: Replace fs-extra with mkdirp/rimraf/cp-file
In Electron apps, the cost of large modules is very real. fs-extra is very convenient, but removing it caused 50 fewer unique files to be required(), resultin in 60ms faster startup! Before: 557 unique requires (1330-1340ms) After: 507 unique requires (1270-1280ms)
This commit is contained in:
@@ -273,7 +273,7 @@ function commandToArgs (command) {
|
||||
}
|
||||
|
||||
function installLinux () {
|
||||
const fs = require('fs-extra')
|
||||
const fs = require('fs')
|
||||
const os = require('os')
|
||||
const path = require('path')
|
||||
|
||||
@@ -326,6 +326,8 @@ function installLinux () {
|
||||
function writeIconFile (err, iconFile) {
|
||||
if (err) return log.error(err.message)
|
||||
|
||||
const mkdirp = require('mkdirp')
|
||||
|
||||
const iconFilePath = path.join(
|
||||
os.homedir(),
|
||||
'.local',
|
||||
@@ -333,9 +335,11 @@ function installLinux () {
|
||||
'icons',
|
||||
'webtorrent-desktop.png'
|
||||
)
|
||||
fs.mkdirp(path.dirname(iconFilePath))
|
||||
fs.writeFile(iconFilePath, iconFile, function (err) {
|
||||
mkdirp(path.dirname(iconFilePath), (err) => {
|
||||
if (err) return log.error(err.message)
|
||||
fs.writeFile(iconFilePath, iconFile, (err) => {
|
||||
if (err) log.error(err.message)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -343,7 +347,7 @@ function installLinux () {
|
||||
function uninstallLinux () {
|
||||
const os = require('os')
|
||||
const path = require('path')
|
||||
const fs = require('fs-extra')
|
||||
const rimraf = require('rimraf')
|
||||
|
||||
const desktopFilePath = path.join(
|
||||
os.homedir(),
|
||||
@@ -352,7 +356,7 @@ function uninstallLinux () {
|
||||
'applications',
|
||||
'webtorrent-desktop.desktop'
|
||||
)
|
||||
fs.removeSync(desktopFilePath)
|
||||
rimraf(desktopFilePath)
|
||||
|
||||
const iconFilePath = path.join(
|
||||
os.homedir(),
|
||||
@@ -361,5 +365,5 @@ function uninstallLinux () {
|
||||
'icons',
|
||||
'webtorrent-desktop.png'
|
||||
)
|
||||
fs.removeSync(iconFilePath)
|
||||
rimraf(iconFilePath)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const electron = require('electron')
|
||||
const fs = require('fs-extra')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const parallel = require('run-parallel')
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ function run (state) {
|
||||
}
|
||||
|
||||
function migrate_0_7_0 (saved) {
|
||||
const fs = require('fs-extra')
|
||||
const cpFile = require('cp-file')
|
||||
const path = require('path')
|
||||
|
||||
saved.torrents.forEach(function (ts) {
|
||||
@@ -70,7 +70,7 @@ function migrate_0_7_0 (saved) {
|
||||
dst = path.join(config.TORRENT_PATH, infoHash + '.torrent')
|
||||
// Synchronous FS calls aren't ideal, but probably OK in a migration
|
||||
// that only runs once
|
||||
if (src !== dst) fs.copySync(src, dst)
|
||||
if (src !== dst) cpFile.sync(src, dst)
|
||||
|
||||
delete ts.torrentPath
|
||||
ts.torrentFileName = infoHash + '.torrent'
|
||||
@@ -85,7 +85,7 @@ function migrate_0_7_0 (saved) {
|
||||
dst = path.join(config.POSTER_PATH, infoHash + extension)
|
||||
// Synchronous FS calls aren't ideal, but probably OK in a migration
|
||||
// that only runs once
|
||||
if (src !== dst) fs.copySync(src, dst)
|
||||
if (src !== dst) cpFile.sync(src, dst)
|
||||
|
||||
delete ts.posterURL
|
||||
ts.posterFileName = infoHash + extension
|
||||
@@ -139,7 +139,7 @@ function migrate_0_12_0 (saved) {
|
||||
if (!fileOrFolder) return
|
||||
try {
|
||||
fs.statSync(fileOrFolder)
|
||||
} catch (e) {
|
||||
} catch (err) {
|
||||
// Default torrent with "missing path" error. Clear path.
|
||||
delete torrentSummary.path
|
||||
}
|
||||
|
||||
@@ -109,7 +109,8 @@ function getDefaultPlayState () {
|
||||
|
||||
/* If the saved state file doesn't exist yet, here's what we use instead */
|
||||
function setupStateSaved (cb) {
|
||||
const fs = require('fs-extra')
|
||||
const cpFile = require('cp-file')
|
||||
const fs = require('fs')
|
||||
const parseTorrent = require('parse-torrent')
|
||||
const parallel = require('run-parallel')
|
||||
|
||||
@@ -130,18 +131,16 @@ function setupStateSaved (cb) {
|
||||
config.DEFAULT_TORRENTS.map(function (t, i) {
|
||||
const infoHash = saved.torrents[i].infoHash
|
||||
tasks.push(function (cb) {
|
||||
fs.copy(
|
||||
cpFile(
|
||||
path.join(config.STATIC_PATH, t.posterFileName),
|
||||
path.join(config.POSTER_PATH, infoHash + path.extname(t.posterFileName)),
|
||||
cb
|
||||
)
|
||||
path.join(config.POSTER_PATH, infoHash + path.extname(t.posterFileName))
|
||||
).then(cb).catch(cb)
|
||||
})
|
||||
tasks.push(function (cb) {
|
||||
fs.copy(
|
||||
cpFile(
|
||||
path.join(config.STATIC_PATH, t.torrentFileName),
|
||||
path.join(config.TORRENT_PATH, infoHash + '.torrent'),
|
||||
cb
|
||||
)
|
||||
path.join(config.TORRENT_PATH, infoHash + '.torrent')
|
||||
).then(cb).catch(cb)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -151,6 +150,7 @@ function setupStateSaved (cb) {
|
||||
})
|
||||
|
||||
function createTorrentObject (t) {
|
||||
// TODO: Doing several fs.readFileSync calls during first startup is not ideal
|
||||
const torrent = fs.readFileSync(path.join(config.STATIC_PATH, t.torrentFileName))
|
||||
const parsedTorrent = parseTorrent(torrent)
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ const crypto = require('crypto')
|
||||
const deepEqual = require('deep-equal')
|
||||
const defaultAnnounceList = require('create-torrent').announceList
|
||||
const electron = require('electron')
|
||||
const fs = require('fs-extra')
|
||||
const fs = require('fs')
|
||||
const mkdirp = require('mkdirp')
|
||||
const musicmetadata = require('musicmetadata')
|
||||
const networkAddress = require('network-address')
|
||||
const path = require('path')
|
||||
@@ -203,19 +204,22 @@ function getTorrentFileInfo (file) {
|
||||
}
|
||||
}
|
||||
|
||||
// Every time we resolve a magnet URI, save the torrent file so that we never
|
||||
// have to download it again. Never ask the DHT the same question twice.
|
||||
// Every time we resolve a magnet URI, save the torrent file so that we can use
|
||||
// it on next startup. Starting with the full torrent metadata will be faster
|
||||
// than re-fetching it from peers using ut_metadata.
|
||||
function saveTorrentFile (torrentKey) {
|
||||
const torrent = getTorrent(torrentKey)
|
||||
checkIfTorrentFileExists(torrent.infoHash, function (torrentPath, exists) {
|
||||
const torrentPath = path.join(config.TORRENT_PATH, torrent.infoHash + '.torrent')
|
||||
|
||||
fs.access(torrentPath, fs.constants.R_OK, function (err) {
|
||||
const fileName = torrent.infoHash + '.torrent'
|
||||
if (exists) {
|
||||
if (!err) {
|
||||
// We've already saved the file
|
||||
return ipc.send('wt-file-saved', torrentKey, fileName)
|
||||
}
|
||||
|
||||
// Otherwise, save the .torrent file, under the app config folder
|
||||
fs.mkdir(config.TORRENT_PATH, function (_) {
|
||||
mkdirp(config.TORRENT_PATH, function (_) {
|
||||
fs.writeFile(torrentPath, torrent.torrentFile, function (err) {
|
||||
if (err) return console.log('error saving torrent file %s: %o', torrentPath, err)
|
||||
console.log('saved torrent file %s', torrentPath)
|
||||
@@ -225,15 +229,6 @@ function saveTorrentFile (torrentKey) {
|
||||
})
|
||||
}
|
||||
|
||||
// Checks whether we've already resolved a given infohash to a torrent file
|
||||
// Calls back with (torrentPath, exists). Logs, does not call back on error
|
||||
function checkIfTorrentFileExists (infoHash, cb) {
|
||||
const torrentPath = path.join(config.TORRENT_PATH, infoHash + '.torrent')
|
||||
fs.exists(torrentPath, function (exists) {
|
||||
cb(torrentPath, exists)
|
||||
})
|
||||
}
|
||||
|
||||
// Save a JPG that represents a torrent.
|
||||
// Auto chooses either a frame from a video file, an image, etc
|
||||
function generateTorrentPoster (torrentKey) {
|
||||
@@ -241,7 +236,7 @@ function generateTorrentPoster (torrentKey) {
|
||||
torrentPoster(torrent, function (err, buf, extension) {
|
||||
if (err) return console.log('error generating poster: %o', err)
|
||||
// save it for next time
|
||||
fs.mkdirp(config.POSTER_PATH, function (err) {
|
||||
mkdirp(config.POSTER_PATH, function (err) {
|
||||
if (err) return console.log('error creating poster dir: %o', err)
|
||||
const posterFileName = torrent.infoHash + extension
|
||||
const posterFilePath = path.join(config.POSTER_PATH, posterFileName)
|
||||
|
||||
Reference in New Issue
Block a user