You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

copyFileSync与readFileSync->writeFileSync:哪种拷贝更高效?如何设置拷贝时间戳?

Great question! Let’s break this down into two clear parts: which method is more optimized, and how to tweak timestamps when using copyFileSync().

Performance: copyFileSync() is the Clear Choice

Here’s why it’s better than pairing readFileSync() with writeFileSync():

  • copyFileSync() uses the operating system’s built-in copy tools under the hood. That means the OS handles the copy directly—often by copying disk blocks or using optimized system calls—without loading the entire file into your Node.js app’s memory. This is way more efficient, especially for large files (think hundreds of MBs or GBs), because it skips the overhead of reading all data into your app’s memory and then writing it back out.
  • The readFileSync() → writeFileSync() approach, on the other hand, reads the whole file into a buffer (or string) in Node.js memory first, then dumps that buffer to the destination. For tiny files, you might not notice a difference, but for larger ones, this uses way more memory and is noticeably slower. It even risks hitting memory limits if the file is bigger than your Node.js process can handle.

Plus, copyFileSync() handles edge cases better out of the box—like copying symlinks or preserving file permissions (you can adjust these with options if needed).

Changing Timestamps with copyFileSync()

By default, copyFileSync() keeps the original file’s timestamps (creation, access, and modify times) because the preserveTimestamps option is set to true automatically. To use the current time (when you run the copy) instead, just set this option to false:

const fs = require('fs');

// Copy the file and use the current time for timestamps
fs.copyFileSync('source.txt', 'destination.txt', { preserveTimestamps: false });

If you need to set a specific custom timestamp (not just the current time), you can update the destination file’s times after copying with fs.utimesSync():

const fs = require('fs');

// First copy the file (this keeps original timestamps by default)
fs.copyFileSync('source.txt', 'destination.txt');

// Set custom access and modify times (example: using a specific date)
const customAccessTime = new Date('2024-01-01T12:00:00');
const customModifyTime = new Date('2024-01-01T12:00:00');

fs.utimesSync('destination.txt', customAccessTime, customModifyTime);

Note: utimesSync() accepts either Unix timestamps (in seconds) or Date objects, so you have flexibility here.

Quick Recap

  • Use copyFileSync() for better performance, lower memory usage, and more reliable OS-level copy handling.
  • To use copy-time timestamps: Add { preserveTimestamps: false } to the copyFileSync() options.
  • For custom timestamps: Copy first, then use fs.utimesSync() to adjust the destination file’s times.

内容的提问来源于stack exchange,提问作者kzaiwo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 14:07:37