No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Object copy Stringify/parse vs Quasar.extend

    Framework
    3
    7
    888
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      pintaf last edited by

      Hi there.

      Until now I’ve been using JSON.parse(JSON.Stringify(OBJ)) to deep copy.

      I recently found out the extend method of Quasar.

      One of the advantage I can see right now is the possibility to ask for deep copy or no (resulting in a shallow copy in that case I guess).

      I have several questions. For deep copy only, is it better to use the Quasar method, or the Parsing/Stringify is more efficient as Javascript native ?

      Second question. In the example the extend method returns the object, but there are several object parameters following.
      It is unclear. Maybe a little update of the documentation would help.

      qyloxe 1 Reply Last reply Reply Quote 0
      • qyloxe
        qyloxe @pintaf last edited by qyloxe

        @pintaf well, I’m not sure, that what I’ll write will be most helpful or right in every situation, but this object copying is a very interesting problem.

        In the context of Vue, the duplication of nested object has more caveats:

        • retaining reactivity
        • following all Vuex requirements (and not breaking Vuex and retaining this reactivity)

        My opinion as for now, is that deep cloning objects in js in the context of Vue/Vuex is strongly application specific. What that means? Well, some specific objects in specific context could be copied and for some, it is better to use something stronger, more complicated and in the bigger picture (at the end of the day 🙂 ) it will be better for a project as a whole.

        The solution with JSON - parse - stringify is nice, because it should work (almost) always. In my practice, it is the most straightforward, safe and PREDICTABLE solution.

        However, there is this extend function:
        https://github.com/quasarframework/quasar/blob/dev/ui/src/utils/extend.js
        Look at yourself if it will work in your situation. I have a tendency to not trust javascript completely, so honestly, I’m not sure if this implementation will handle all the cases in foreseable future. Probably yes, but I won’t give a guarantee. The conclusion is that, IF you want to use that, then you WILL need tests. Those tests will give you some predictability.

        Vuex - main main problem with deep objects is Vuex. This is where I need this mostly. There are tons of snippets/articles on google (“vuex object copy”). There are even helpers, repositories and libraries for that.

        However - there is one technique, which in my opinion, in bigger contexts, with more data, probably retrieved from API is the most beneficial. It is nested data normalization and querying. Please look at this library:

        https://vuex-orm.org/guide/prologue/what-is-vuex-orm.html

        If you have a deep nested object, this library is capable of normalizing it, putting the data into simple, related lists, and when you need something - you can query those nested data. You can safely remove them, insert, paginate and everything is reactive. This is my preferred solution for more complicated cases. In simple ones - I still like this JSON stringify.

        1 Reply Last reply Reply Quote 0
        • P
          pintaf last edited by

          Hello @qyloxe.

          Well, this is quite a long an complete answer.
          This has enlighten me a bit, and since I do not use VueX, I’ll stick with Parse/Stringify.

          Thanks a lot!

          1 Reply Last reply Reply Quote 0
          • rstoenescu
            rstoenescu Admin last edited by

            Multiple params because you can extend multiple objects in one go.

            1 Reply Last reply Reply Quote 0
            • P
              pintaf last edited by pintaf

              Okay that make sense, but then it is weird that a function that can extend multiple object returns only one object. Isn’t it ?

              let newObject = extend([Boolean deepCopy], targetObj, obj, ...)
              

              For example, how would it work for several objects ?

              something like that?

              extend(true, obj1, extendedObj1, obj2, extendedObj2)
              
              1 Reply Last reply Reply Quote 0
              • rstoenescu
                rstoenescu Admin last edited by

                Cause you want a resulting object composed of one or more objects “merged” together.

                1 Reply Last reply Reply Quote 0
                • P
                  pintaf last edited by

                  Okay that make sense.

                  I should have gone see jQuery.extend() for more info.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post