Skip to content

Commit af0b953

Browse files
authored
[DI] Reduce size of compiled code when comparing number literals (#5536)
1 parent 5573c12 commit af0b953

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

packages/dd-trace/src/debugger/devtools_client/condition.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ const PRIMITIVE_TYPES = new Set(['string', 'number', 'bigint', 'boolean', 'undef
3737

3838
// TODO: Consider storing some of these functions on `process` so they can be reused across probes
3939
function compile (node) {
40-
if (node === null || typeof node === 'number' || typeof node === 'boolean' || typeof node === 'string') {
40+
if (node === null || typeof node === 'number' || typeof node === 'boolean') {
41+
return node
42+
} else if (typeof node === 'string') {
4143
return JSON.stringify(node)
4244
}
4345

@@ -220,6 +222,9 @@ function guardAgainstPropertyAccessSideEffects (variable, propertyName) {
220222
}
221223

222224
function guardAgainstCoercionSideEffects (variable) {
225+
// shortcut if we're comparing number literals
226+
if (typeof variable === 'number') return variable
227+
223228
return `((val) => {
224229
if (
225230
typeof val === 'object' && val !== null && (

packages/dd-trace/test/debugger/devtools_client/condition-test-cases.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ const equality = [
280280
[{ gt: [{ ref: 'str' }, 'a'] }, { str: 'a' }, false],
281281
[{ gt: [{ ref: 'str' }, 'b'] }, { str: 'a' }, false],
282282
[{ gt: [{ or: [2, 0] }, { and: [1, 1] }] }, {}, true],
283+
{ ast: { gt: [1, 2] }, expected: '1 > 2', execute: false },
283284
[
284285
{ gt: [{ ref: 'obj' }, 5] },
285286
{ obj: objectWithToPrimitiveSymbol },
@@ -328,6 +329,7 @@ const equality = [
328329
[{ ge: [{ ref: 'str' }, 'a'] }, { str: 'a' }, true],
329330
[{ ge: [{ ref: 'str' }, 'b'] }, { str: 'a' }, false],
330331
[{ ge: [{ or: [1, 0] }, { and: [1, 2] }] }, {}, false],
332+
{ ast: { ge: [1, 2] }, expected: '1 >= 2', execute: false },
331333
[
332334
{ ge: [{ ref: 'obj' }, 5] },
333335
{ obj: objectWithToPrimitiveSymbol },
@@ -351,6 +353,7 @@ const equality = [
351353
[{ lt: [{ ref: 'str' }, 'a'] }, { str: 'a' }, false],
352354
[{ lt: [{ ref: 'str' }, 'b'] }, { str: 'a' }, true],
353355
[{ lt: [{ or: [1, 0] }, { and: [1, 0] }] }, {}, false],
356+
{ ast: { lt: [1, 2] }, expected: '1 < 2', execute: false },
354357
[
355358
{ lt: [{ ref: 'obj' }, 5] },
356359
{ obj: objectWithToPrimitiveSymbol },
@@ -374,6 +377,7 @@ const equality = [
374377
[{ le: [{ ref: 'str' }, 'a'] }, { str: 'a' }, true],
375378
[{ le: [{ ref: 'str' }, 'b'] }, { str: 'a' }, true],
376379
[{ le: [{ or: [2, 0] }, { and: [1, 1] }] }, {}, false],
380+
{ ast: { le: [1, 2] }, expected: '1 <= 2', execute: false },
377381
[
378382
{ le: [{ ref: 'obj' }, 5] },
379383
{ obj: objectWithToPrimitiveSymbol },

0 commit comments

Comments
 (0)