node/lib/internal/perf/performance_entry.js
Vinicius Lourenço a6ad048b89
perf_hooks: reduce overhead of new performance_entries
PR-URL: https://github.com/nodejs/node/pull/49803
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2023-09-27 01:19:26 +00:00

142 lines
3.3 KiB
JavaScript

'use strict';
const {
ObjectDefineProperties,
Symbol,
} = primordials;
const {
codes: {
ERR_ILLEGAL_CONSTRUCTOR,
},
} = require('internal/errors');
const {
customInspectSymbol: kInspect,
kEnumerableProperty,
} = require('internal/util');
const { validateInternalField } = require('internal/validators');
const { inspect } = require('util');
const kName = Symbol('PerformanceEntry.Name');
const kEntryType = Symbol('PerformanceEntry.EntryType');
const kStartTime = Symbol('PerformanceEntry.StartTime');
const kDuration = Symbol('PerformanceEntry.Duration');
const kDetail = Symbol('NodePerformanceEntry.Detail');
const kSkipThrow = Symbol('kSkipThrow');
function isPerformanceEntry(obj) {
return obj?.[kName] !== undefined;
}
class PerformanceEntry {
constructor(skipThrowSymbol = undefined) {
if (skipThrowSymbol !== kSkipThrow) {
throw new ERR_ILLEGAL_CONSTRUCTOR();
}
}
get name() {
validateInternalField(this, kName, 'PerformanceEntry');
return this[kName];
}
get entryType() {
validateInternalField(this, kEntryType, 'PerformanceEntry');
return this[kEntryType];
}
get startTime() {
validateInternalField(this, kStartTime, 'PerformanceEntry');
return this[kStartTime];
}
get duration() {
validateInternalField(this, kDuration, 'PerformanceEntry');
return this[kDuration];
}
[kInspect](depth, options) {
if (depth < 0) return this;
const opts = {
...options,
depth: options.depth == null ? null : options.depth - 1,
};
return `${this.constructor.name} ${inspect(this.toJSON(), opts)}`;
}
toJSON() {
validateInternalField(this, kName, 'PerformanceEntry');
return {
name: this[kName],
entryType: this[kEntryType],
startTime: this[kStartTime],
duration: this[kDuration],
};
}
}
ObjectDefineProperties(PerformanceEntry.prototype, {
name: kEnumerableProperty,
entryType: kEnumerableProperty,
startTime: kEnumerableProperty,
duration: kEnumerableProperty,
toJSON: kEnumerableProperty,
});
function initPerformanceEntry(entry, name, type, start, duration) {
entry[kName] = name;
entry[kEntryType] = type;
entry[kStartTime] = start;
entry[kDuration] = duration;
}
function createPerformanceEntry(name, type, start, duration) {
const entry = new PerformanceEntry(kSkipThrow);
initPerformanceEntry(entry, name, type, start, duration);
return entry;
}
/**
* Node.js specific extension to PerformanceEntry.
*/
class PerformanceNodeEntry extends PerformanceEntry {
get detail() {
validateInternalField(this, kDetail, 'NodePerformanceEntry');
return this[kDetail];
}
toJSON() {
validateInternalField(this, kName, 'PerformanceEntry');
return {
name: this[kName],
entryType: this[kEntryType],
startTime: this[kStartTime],
duration: this[kDuration],
detail: this[kDetail],
};
}
}
function createPerformanceNodeEntry(name, type, start, duration, detail) {
const entry = new PerformanceNodeEntry(kSkipThrow);
initPerformanceEntry(entry, name, type, start, duration);
entry[kDetail] = detail;
return entry;
}
module.exports = {
initPerformanceEntry,
createPerformanceEntry,
PerformanceEntry,
isPerformanceEntry,
PerformanceNodeEntry,
createPerformanceNodeEntry,
};