Skip to content

Commit 9708adb

Browse files
committed
fs: make stats date fields lazy
1 parent 7981e2e commit 9708adb

1 file changed

Lines changed: 62 additions & 8 deletions

File tree

lib/internal/fs/utils.js

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const {
1212
NumberIsFinite,
1313
MathMin,
1414
MathRound,
15+
ObjectDefineProperties,
16+
ObjectDefineProperty,
1517
ObjectIs,
1618
ObjectSetPrototypeOf,
1719
ReflectApply,
@@ -448,6 +450,62 @@ function dateFromMs(ms) {
448450
return new Date(MathRound(Number(ms)));
449451
}
450452

453+
const lazyDateFields = {
454+
__proto__: null,
455+
atime: {
456+
__proto__: null,
457+
enumerable: true,
458+
configurable: true,
459+
get() {
460+
const value = dateFromMs(this.atimeMs);
461+
ObjectDefineProperty(this, 'atime', { __proto__: null, value });
462+
return this.atime;
463+
},
464+
set(value) {
465+
this.atime = value;
466+
},
467+
},
468+
mtime: {
469+
__proto__: null,
470+
enumerable: true,
471+
configurable: true,
472+
get() {
473+
const value = dateFromMs(this.mtimeMs);
474+
ObjectDefineProperty(this, 'mtime', { __proto__: null, value });
475+
return this.mtime;
476+
},
477+
set(value) {
478+
this.mtime = value;
479+
},
480+
},
481+
ctime: {
482+
__proto__: null,
483+
enumerable: true,
484+
configurable: true,
485+
get() {
486+
const value = dateFromMs(this.ctimeMs);
487+
ObjectDefineProperty(this, 'ctime', { __proto__: null, value });
488+
return this.ctime;
489+
},
490+
set(value) {
491+
this.ctime = value;
492+
},
493+
},
494+
birthtime: {
495+
__proto__: null,
496+
enumerable: true,
497+
configurable: true,
498+
get() {
499+
const value = dateFromMs(this.birthtimeMs);
500+
ObjectDefineProperty(this, 'birthtime', { __proto__: null, value });
501+
return this.birthtime;
502+
},
503+
set(value) {
504+
this.birthtime = value;
505+
},
506+
},
507+
};
508+
451509
function BigIntStats(dev, mode, nlink, uid, gid, rdev, blksize,
452510
ino, size, blocks,
453511
atimeNs, mtimeNs, ctimeNs, birthtimeNs) {
@@ -462,10 +520,8 @@ function BigIntStats(dev, mode, nlink, uid, gid, rdev, blksize,
462520
this.mtimeNs = mtimeNs;
463521
this.ctimeNs = ctimeNs;
464522
this.birthtimeNs = birthtimeNs;
465-
this.atime = dateFromMs(this.atimeMs);
466-
this.mtime = dateFromMs(this.mtimeMs);
467-
this.ctime = dateFromMs(this.ctimeMs);
468-
this.birthtime = dateFromMs(this.birthtimeMs);
523+
524+
ObjectDefineProperties(this, lazyDateFields);
469525
}
470526

471527
ObjectSetPrototypeOf(BigIntStats.prototype, StatsBase.prototype);
@@ -488,10 +544,8 @@ function Stats(dev, mode, nlink, uid, gid, rdev, blksize,
488544
this.mtimeMs = mtimeMs;
489545
this.ctimeMs = ctimeMs;
490546
this.birthtimeMs = birthtimeMs;
491-
this.atime = dateFromMs(atimeMs);
492-
this.mtime = dateFromMs(mtimeMs);
493-
this.ctime = dateFromMs(ctimeMs);
494-
this.birthtime = dateFromMs(birthtimeMs);
547+
548+
ObjectDefineProperties(this, lazyDateFields);
495549
}
496550

497551
ObjectSetPrototypeOf(Stats.prototype, StatsBase.prototype);

0 commit comments

Comments
 (0)