Skip to content

Commit

Permalink
⚡ Performance Friday Reduce Bundle Size (#2875)
Browse files Browse the repository at this point in the history
* test reduce bundle size

* more changes about ?.

* delete more ?.

* fix build

* update viewCollection

* update other ?.

* test to see bundle reduction

* revert last commit

* change an other ?.

* fix

* update number of runs

* don't use JSON.stringify twice
  • Loading branch information
RomanGaignault authored Jul 24, 2024
1 parent e524b92 commit 159a305
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/domain/error/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function computeRawError({
? toStackTraceString(stackTrace)
: NO_ERROR_STACK_PRESENT_MESSAGE
const causes = isErrorInstance ? flattenErrorCauses(originalError as ErrorWithCause, source) : undefined
const type = stackTrace?.name
const type = stackTrace ? stackTrace.name : undefined
const fingerprint = tryToGetFingerprint(originalError)

return {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/tools/serialisation/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export function sanitize(source: unknown, maxCharacterCount = SANITIZE_DEFAULT_M
containerQueue,
visitedObjectsWithPath
)
let accumulatedCharacterCount = JSON.stringify(sanitizedData)?.length || 0
const serializedSanitizedData = JSON.stringify(sanitizedData)
let accumulatedCharacterCount = serializedSanitizedData ? serializedSanitizedData.length : 0

if (accumulatedCharacterCount > maxCharacterCount) {
warnOverCharacterLimit(maxCharacterCount, 'discarded', source)
return undefined
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/transport/startBatchWithReplica.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export function startBatchWithReplica<T extends Context>(

stop: () => {
primaryBatch.stop()
replicaBatch?.stop()
if (replicaBatch) {
replicaBatch.stop()
}
},
}
}
4 changes: 3 additions & 1 deletion packages/core/test/interceptRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ export function interceptRequests() {
if (originalFetch) {
window.fetch = originalFetch
}
xhrManager?.reset()
if (xhrManager) {
xhrManager.reset()
}
MockXhr.onSend = noop
},
}
Expand Down
2 changes: 1 addition & 1 deletion packages/logs/src/domain/assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function startLogsAssembly(
const log = combine(
{
service: configuration.service,
session_id: session?.id,
session_id: session ? session.id : undefined,
// Insert user first to allow overrides from global context
usr: !isEmptyObject(commonContext.user) ? commonContext.user : undefined,
view: commonContext.view,
Expand Down
10 changes: 5 additions & 5 deletions packages/rum-core/src/boot/rumPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ export function makeRumPublicApi(
addTelemetryUsage({ feature: 'start-duration-vital' })
return strategy.startDurationVital({
name: sanitize(name)!,
context: sanitize(options?.context) as Context,
details: sanitize(options?.details) as string | undefined,
context: sanitize(options && options.context) as Context,
details: sanitize(options && options.details) as string | undefined,
})
}
)
Expand All @@ -355,8 +355,8 @@ export function makeRumPublicApi(
type: VitalType.DURATION,
startClocks: timeStampToClocks(options.startTime as TimeStamp),
duration: options.duration as Duration,
context: sanitize(options?.context) as Context,
details: sanitize(options?.details) as string | undefined,
context: sanitize(options && options.context) as Context,
details: sanitize(options && options.details) as string | undefined,
})
}
)
Expand Down Expand Up @@ -524,7 +524,7 @@ export function makeRumPublicApi(
getSessionReplayLink: monitor(() => recorderApi.getSessionReplayLink()),
startSessionReplayRecording: monitor((options?: StartRecordingOptions) => {
recorderApi.start(options)
addTelemetryUsage({ feature: 'start-session-replay-recording', force: options?.force })
addTelemetryUsage({ feature: 'start-session-replay-recording', force: options && options.force })
}),

stopSessionReplayRecording: monitor(() => recorderApi.stop()),
Expand Down
4 changes: 3 additions & 1 deletion packages/rum-core/src/browser/performanceObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ export function createPerformanceObservable<T extends RumPerformanceEntryType>(
manageResourceTimingBufferFull(configuration)

return () => {
observer?.disconnect()
if (observer) {
observer.disconnect()
}
if (timeoutId) {
clearTimeout(timeoutId)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rum-core/src/browser/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function getClassList(element: Element): DOMTokenList | string[] {
return element.classList
}

const classes = element.getAttribute('class')?.trim()
const classes = (element.getAttribute('class') || '').trim()
return classes ? classes.split(/\s+/) : []
}

Expand Down
1 change: 1 addition & 0 deletions packages/rum-core/src/domain/assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ function shouldSend(
}

const rateLimitReached = eventRateLimiters[event.type]?.isLimitReached()

return !rateLimitReached
}

Expand Down
4 changes: 3 additions & 1 deletion packages/rum-core/src/domain/view/trackViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ export function trackViews(
},

stop: () => {
locationChangeSubscription?.unsubscribe()
if (locationChangeSubscription) {
locationChangeSubscription.unsubscribe()
}
currentView.end()
activeViews.forEach((view) => view.stop())
},
Expand Down
4 changes: 3 additions & 1 deletion packages/rum-core/test/testSetupBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ export function setup(): TestSetupBuilder {
registerCleanupTask(() => {
cleanupTasks.forEach((task) => task())
// perform these steps at the end to generate correct events in cleanup and validate them
clock?.cleanup()
if (clock) {
clock.cleanup()
}
rawRumEventsCollected.unsubscribe()
})
return setupBuilder
Expand Down
4 changes: 2 additions & 2 deletions packages/rum/src/boot/recorderApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function makeRecorderApi(

startStrategy = (options?: StartRecordingOptions) => {
const session = sessionManager.findTrackedSession()
if (!session || (session.sessionReplay === SessionReplayState.OFF && !options?.force)) {
if (!session || (session.sessionReplay === SessionReplayState.OFF && (!options || !options.force))) {
state = { status: RecorderStatus.IntentToStart }
return
}
Expand Down Expand Up @@ -181,7 +181,7 @@ export function makeRecorderApi(
}
})

if (options?.force && session.sessionReplay === SessionReplayState.OFF) {
if (options && options.force && session.sessionReplay === SessionReplayState.OFF) {
sessionManager.setForcedReplay()
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/rum/src/domain/record/trackers/trackFrustration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export function trackFrustration(
data.rawRumEvent.action.type === ActionType.CLICK &&
data.rawRumEvent.action.frustration?.type?.length &&
'events' in data.domainContext &&
data.domainContext.events?.length
data.domainContext.events &&
data.domainContext.events.length
) {
frustrationCb({
timestamp: data.rawRumEvent.date,
Expand Down
2 changes: 1 addition & 1 deletion packages/rum/src/domain/record/trackers/trackMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ function processAttributesMutations(
const handledElements = new Map<Element, Set<string>>()
const filteredMutations = mutations.filter((mutation) => {
const handledAttributes = handledElements.get(mutation.target)
if (handledAttributes?.has(mutation.attributeName!)) {
if (handledAttributes && handledAttributes.has(mutation.attributeName!)) {
return false
}
if (!handledAttributes) {
Expand Down

0 comments on commit 159a305

Please sign in to comment.