-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PBjs Utils: Faster Deep Clone #11418
Conversation
@@ -57,7 +57,7 @@ export function addBrowsiTag(data) { | |||
script.setAttribute('prebidbpt', 'true'); | |||
script.setAttribute('id', 'browsi-tag'); | |||
script.setAttribute('src', data.u); | |||
script.prebidData = deepClone(data); | |||
script.prebidData = deepClone(typeof data === 'string' ? Object(data) : data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Version 1 of just-clone casts everything to an object, even though in this case it was a string (I am not sure if this was wanted behavior, but it resulted in a failed test).
The newer versions don't do that, just like klona.
This is here to preserve that behavior.
@@ -609,7 +609,7 @@ export function shuffle(array) { | |||
} | |||
|
|||
export function deepClone(obj) { | |||
return clone(obj); | |||
return klona(obj) || {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Version 1 of just-clone gives back an empty object when the cloning fails.
The newer versions don't do that, just like klona.
This is here to preserve that behavior.
c753d85
to
3f049c7
Compare
Type of change
Description of change
Prebid currently uses an outdated version of just-clone for deep cloning objects.
After some benchmarking, klona seems much more efficient even compared to other libraries or even native JS (
structuredClone
andJSON.parse/stringify
)This PR replaces
just-clone
, but the other way of deep-cloningJSON.parse(JSON.stringify(object))
stays unchanged.That should be done in a different PR.
This partially addresses #11399.