NodeJS 8.5 adds fast native copyFile

NodeJS is awesome, but every now and then you come across something that isn’t really easy. One of those things is copying a file. Seems simple, but a glance at StackOverflow proves otherwise. The answers basically consist of reading a stream from one file and writing it to another, then wire in your error handling.

Fortunately Node v8.5.0 adds the fs.copyFile() function, which makes a call down to the native OS filesystem’s “copy” command.

var fs = require('fs');

fs.copyFile('source.txt', 'destination.txt', err => {
    if (err) throw err;
    console.log('success');
});

Not only is this way easier to use, but it is MUCH FASTER!

How much faster? Let’s look at an application that has a ton of file IO: Yarn. Yarn needs to extract archives and copy the contents into node_modules. This involves moving around thousands of files. The next version of Yarn (v1.0.3 I believe) will now use fs.copyFile() when you are running it on NodeJS >=8.5.0.

So here are some numbers, for doing a “yarn install” in a project with >1200 dependency packages

on node 8.2.1 (without fs.copyFile)

$ time yarn install
yarn install v1.0.x
[1/5] 🔍  Validating package.json...
[2/5] 🔍  Resolving packages...
[3/5] 🚚  Fetching packages...
[4/5] 🔗  Linking dependencies...
[5/5] 📃 Building fresh packages...
✨  Done in 10.78s.

real 0m11.236s
user 0m9.543s
sys 0m8.387s

on node 8.5.0 (with fs.copyFile)

$ time yarn install
yarn install v1.0.x
[1/5] 🔍  Validating package.json...
[2/5] 🔍  Resolving packages...
[3/5] 🚚  Fetching packages...
[4/5] 🔗  Linking dependencies...
[5/5] 📃 Building fresh packages...
✨ Done in 7.08s.

real 0m7.413s
user 0m6.585s
sys 0m8.543s

WOW! Simply changing file copy use to the new fs.copyFile() cuts the yarn install time by >3 seconds!

 

It’s really great to see such a big improvement to NodeJS. I hadn’t seen much discussion about this new function, but it clearly makes a huge difference.

Advertisements
Tagged with: ,
Posted in JavaScript, Programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

CodingWithSpike is Jeff Valore. A professional software engineer, focused on JavaScript, Web Development, C# and the Microsoft stack. Jeff is currently a Software Engineer at Virtual Hold Technologies.


I am also a Pluralsight author. Check out my courses!

%d bloggers like this: