Skip to content

Commit

Permalink
ref: Avoid unnecessary cloning of objects or arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Oct 27, 2023
1 parent 67fc31d commit c9cde44
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 24 deletions.
27 changes: 6 additions & 21 deletions packages/rrweb/src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,6 @@ function initViewportResizeObserver(
return on('resize', updateDimension, win);
}

function wrapEventWithUserTriggeredFlag(
v: inputValue,
enable: boolean,
): inputValue {
const value = { ...v };
if (!enable) delete value.userTriggered;
return value;
}

export const INPUT_TAGS = ['INPUT', 'TEXTAREA', 'SELECT'];
const lastInputValueMap: WeakMap<EventTarget, inputValue> = new WeakMap();
function initInputObserver({
Expand Down Expand Up @@ -527,10 +518,9 @@ function initInputObserver({

cbWithDedup(
target,
callbackWrapper(wrapEventWithUserTriggeredFlag)(
{ text, isChecked, userTriggered },
userTriggeredOnInput,
),
userTriggeredOnInput
? { text, isChecked, userTriggered }
: { text, isChecked },
);
// if a radio was checked
// the other radios with the same name attribute will be unchecked.
Expand All @@ -549,14 +539,9 @@ function initInputObserver({
});
cbWithDedup(
el,
callbackWrapper(wrapEventWithUserTriggeredFlag)(
{
text,
isChecked: !isChecked,
userTriggered: false,
},
userTriggeredOnInput,
),
userTriggeredOnInput
? { text, isChecked: !isChecked, userTriggered: false }
: { text, isChecked: !isChecked },
);
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/rrweb/src/record/observers/canvas/2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function initCanvas2DMutationObserver(
// Using setTimeout as toDataURL can be heavy
// and we'd rather not block the main thread
setTimeout(() => {
const recordArgs = serializeArgs([...args], win, this);
const recordArgs = serializeArgs(args, win, this);
cb(this.canvas, {
type: CanvasContext['2D'],
property: prop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const serializeArgs = (
win: IWindow,
ctx: RenderingContext,
) => {
return [...args].map((arg) => serializeArg(arg, win, ctx));
return args.map((arg) => serializeArg(arg, win, ctx));
};

export const isInstanceOfWebGLObject = (
Expand Down
2 changes: 1 addition & 1 deletion packages/rrweb/src/record/observers/canvas/webgl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function patchGLPrototype(
true,
)
) {
const recordArgs = serializeArgs([...args], win, this);
const recordArgs = serializeArgs(args, win, this);
const mutation: canvasMutationWithType = {
type,
property: prop,
Expand Down

0 comments on commit c9cde44

Please sign in to comment.