Skip to content

Commit

Permalink
fs: make stats date fields lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Nov 29, 2023
1 parent 7981e2e commit 9708adb
Showing 1 changed file with 62 additions and 8 deletions.
70 changes: 62 additions & 8 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const {
NumberIsFinite,
MathMin,
MathRound,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectIs,
ObjectSetPrototypeOf,
ReflectApply,
Expand Down Expand Up @@ -448,6 +450,62 @@ function dateFromMs(ms) {
return new Date(MathRound(Number(ms)));
}

const lazyDateFields = {
__proto__: null,
atime: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
const value = dateFromMs(this.atimeMs);
ObjectDefineProperty(this, 'atime', { __proto__: null, value });
return this.atime;
},
set(value) {
this.atime = value;
},
},
mtime: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
const value = dateFromMs(this.mtimeMs);
ObjectDefineProperty(this, 'mtime', { __proto__: null, value });
return this.mtime;
},
set(value) {
this.mtime = value;
},
},
ctime: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
const value = dateFromMs(this.ctimeMs);
ObjectDefineProperty(this, 'ctime', { __proto__: null, value });
return this.ctime;
},
set(value) {
this.ctime = value;
},
},
birthtime: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
const value = dateFromMs(this.birthtimeMs);
ObjectDefineProperty(this, 'birthtime', { __proto__: null, value });
return this.birthtime;
},
set(value) {
this.birthtime = value;
},
},
};

function BigIntStats(dev, mode, nlink, uid, gid, rdev, blksize,
ino, size, blocks,
atimeNs, mtimeNs, ctimeNs, birthtimeNs) {
Expand All @@ -462,10 +520,8 @@ function BigIntStats(dev, mode, nlink, uid, gid, rdev, blksize,
this.mtimeNs = mtimeNs;
this.ctimeNs = ctimeNs;
this.birthtimeNs = birthtimeNs;
this.atime = dateFromMs(this.atimeMs);
this.mtime = dateFromMs(this.mtimeMs);
this.ctime = dateFromMs(this.ctimeMs);
this.birthtime = dateFromMs(this.birthtimeMs);

ObjectDefineProperties(this, lazyDateFields);
}

ObjectSetPrototypeOf(BigIntStats.prototype, StatsBase.prototype);
Expand All @@ -488,10 +544,8 @@ function Stats(dev, mode, nlink, uid, gid, rdev, blksize,
this.mtimeMs = mtimeMs;
this.ctimeMs = ctimeMs;
this.birthtimeMs = birthtimeMs;
this.atime = dateFromMs(atimeMs);
this.mtime = dateFromMs(mtimeMs);
this.ctime = dateFromMs(ctimeMs);
this.birthtime = dateFromMs(birthtimeMs);

ObjectDefineProperties(this, lazyDateFields);
}

ObjectSetPrototypeOf(Stats.prototype, StatsBase.prototype);
Expand Down

0 comments on commit 9708adb

Please sign in to comment.