Skip to content

Commit

Permalink
perf(utils): use WeakSet to track object references (#5791)
Browse files Browse the repository at this point in the history
This change removes `Set` in favour of `WeakSet` to track seen objects
while cloning for performance reasons

From MDN:
`
The number of objects or their traversal order is immaterial, so a
WeakSet is more suitable (and performant) than a Set for tracking object
references, especially if a very large number of objects is involved.
`

---------

Co-authored-by: Ivan Kohut <ivan.kohut@lamin.ar>
  • Loading branch information
2 people authored and joeyparrish committed Feb 17, 2024
1 parent a83182c commit 2cab9a2
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Gil Gonen <gil.gonen@gmail.com>
Giorgio Gamberoni <giorgio.gamberoni@gmail.com>
Giuseppe Samela <giuseppe.samela@gmail.com>
Google Inc. <*@google.com>
Ivan Kohut <i.kohut@yahoo.com>
Itay Kinnrot <Itay.Kinnrot@Kaltura.com>
Jaeseok Lee <devsunb@gmail.com>
Jason Palmer <jason@jason-palmer.com>
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Gil Gonen <gil.gonen@gmail.com>
Giorgio Gamberoni <giorgio.gamberoni@gmail.com>
Giuseppe Samela <giuseppe.samela@gmail.com>
Hichem Taoufik <hichem@code-it.fr>
Ivan Kohut <i.kohut@yahoo.com>
Itay Kinnrot <itay.kinnrot@kaltura.com>
Isaac Ramirez <isaac.ramirez.herrera@gmail.com>
Jacob Trimble <modmaker@google.com>
Expand Down
2 changes: 1 addition & 1 deletion lib/util/object_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ shaka.util.ObjectUtils = class {
* @return {T}
*/
static cloneObject(arg) {
const seenObjects = new Set();
const seenObjects = new WeakSet();
// This recursively clones the value |val|, using the captured variable
// |seenObjects| to track the objects we have already cloned.
/** @suppress {strictMissingProperties} */
Expand Down

0 comments on commit 2cab9a2

Please sign in to comment.