Skip to content
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

Merged
merged 1 commit into from
May 7, 2024
Merged

Conversation

bbaresic
Copy link
Contributor

Type of change

  • Refactoring (no functional changes, no api changes)

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 and JSON.parse/stringify)

This PR replaces just-clone, but the other way of deep-cloning JSON.parse(JSON.stringify(object)) stays unchanged.
That should be done in a different PR.

This partially addresses #11399.

@@ -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)
Copy link
Contributor Author

@bbaresic bbaresic Apr 29, 2024

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) || {};
Copy link
Contributor Author

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.

@patmmccann patmmccann merged commit 0b574b3 into prebid:master May 7, 2024
2 of 3 checks passed
Ticki84 pushed a commit to criteo-forks/Prebid.js that referenced this pull request May 14, 2024
mefjush pushed a commit to adhese/Prebid.js that referenced this pull request May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants