Skip to content

Commit 08afd57

Browse files
committed
perf: remove redundant temporary collections
Direct variable capture measured 91.5 to 71.5 ns for one variable and 2006 to 1544 ns for 20. Swapping drained arrays measured 31.8 to 12.5 ns at one item and 599 to 276 ns at 1000 items on Node 24.18.0 (7 trials, drop best and worst).
1 parent 80365fb commit 08afd57

4 files changed

Lines changed: 12 additions & 14 deletions

File tree

packages/datadog-plugin-child_process/src/scrub-cmd-params.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@ const envVarRegex = new RegExp(ENV_PATTERN)
1414
const REDACTED = '?'
1515

1616
function extractVarNames (expression) {
17-
const varNames = new Set()
17+
const varNames = {}
1818
let match
1919

2020
while ((match = VARNAMES_REGEX.exec(expression))) {
21-
varNames.add(match[1])
21+
const name = match[1]
22+
varNames[name] = `$${name}`
2223
}
2324

24-
const varNamesObject = {}
25-
for (const varName of varNames) {
26-
varNamesObject[varName] = `$${varName}`
27-
}
28-
return varNamesObject
25+
return varNames
2926
}
3027

3128
function getTokensByExpression (expressionTokens) {

packages/dd-trace/src/exporters/electron/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class ElectronExporter {
1616
}
1717

1818
export (spans) {
19+
if (!traceChannel.hasSubscribers) return
20+
1921
this.#traces.push(spans)
2022

2123
const { flushInterval } = this._config
@@ -35,9 +37,8 @@ class ElectronExporter {
3537
clearTimeout(this.#timer)
3638
this.#timer = undefined
3739

38-
// eslint-disable-next-line unicorn/prefer-spread -- Avoid invoking a user-overridden array iterator.
39-
const traces = this.#traces.slice()
40-
this.#traces.length = 0
40+
const traces = this.#traces
41+
this.#traces = []
4142

4243
if (traces.length > 0 && traceChannel.hasSubscribers) {
4344
const formattedTraces = traces.map(spans => spans.map(span => normalizeSpan(truncateSpan(span))))

packages/dd-trace/src/opentelemetry/metrics/periodic_metric_reader.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,8 @@ class PeriodicMetricReader {
253253
#collectAndExport (callback = () => {}) {
254254
// Atomically drain measurements for export. New measurements can be recorded
255255
// during export without interfering with this batch.
256-
// eslint-disable-next-line unicorn/prefer-spread -- Avoid invoking a user-overridden array iterator.
257-
const allMeasurements = this.#measurements.slice()
258-
this.#measurements.length = 0
256+
const allMeasurements = this.#measurements
257+
this.#measurements = []
259258

260259
for (const instrument of this.observableInstruments) {
261260
const observableMeasurements = instrument.collect()

packages/dd-trace/test/exporters/electron/exporter.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ describe('ElectronExporter', () => {
8080
sinon.assert.notCalled(traceChannel.publish)
8181
})
8282

83-
it('should not publish when there are no subscribers', () => {
83+
it('should not buffer when there are no subscribers', () => {
8484
traceChannel.hasSubscribers = false
8585

8686
exporter.export([span])
87+
traceChannel.hasSubscribers = true
8788
exporter.flush()
8889

8990
sinon.assert.notCalled(traceChannel.publish)

0 commit comments

Comments
 (0)